easypoi动态设置列宽
- 解决方案
esaypoi内部使用的createSheetForMap 方法固定了列宽,数据会折叠显示
解决:项目下建立同名包,重写关键的setCellWith方法,把easypoi包下ExcelExportUtil类相关的都拷贝过来,在cn.afterturn.easypoi.excel下,保证jar包路径相同
public void setCellWith(List<ExcelExportEntity> excelParams, Sheet sheet) {
for (int i = 0; i < excelParams.size(); i++) {
sheet.autoSizeColumn(i);
sheet.setColumnWidth(i, sheet.getColumnWidth(i) * 17 / 10); // 中文宽度
//设置最小宽度,防止title字符过短导致内容折叠显示,体验不佳
int minWidth = excelParams.get(i).getName().length() * 1600;
if (sheet.getColumnWidth(i) < minWidth)
sheet.setColumnWidth(i, minWidth); // 无数据额外处理
}
}
- 依赖版本
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>3.3.0</version>
</dependency>
3.excel工具类
/**
* @author rjq
* @version 1.0
* 2021-9-11 10:26
**/
public class ExcelUtils {
private static final int size=5000;
/**
* excel 导出
*
* @param list 数据列表
* @param fileName 导出时的excel名称
* @param response
*/
public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws IOException {
defaultExport(list, fileName, response);
}
/**
* 默认的 excel 导出
*
* @param list 数据列表
* @param fileName 导出时的excel名称
* @param response
*/
private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws IOException {
//把数据添加到excel表格中
Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
downLoadExcel(fileName, response, workbook);
}
/**
* excel 导出
*
* @param list 数据列表
* @param pojoClass pojo类型
* @param fileName 导出时的excel名称
* @param response
* @param exportParams 导出参数(标题、sheet名称、是否创建表头,表格类型)
*/
private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) throws IOException {
//把数据添加到excel表格中
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
downLoadExcel(fileName, response, workbook);
}
/**
* excel 导出
*
* @param list 数据列表
* @param pojoClass pojo类型
* @param fileName 导出时的excel名称
* @param exportParams 导出参数(标题、sheet名称、是否创建表头,表格类型)
* @param response
*/
public static void exportExcel(List<?> list, Class<?> pojoClass, String fileName, ExportParams exportParams, HttpServletResponse response) throws IOException {
defaultExport(list, pojoClass, fileName, response, exportParams);
}
/**
* excel 导出
*
* @param list 数据列表
* @param title 表格内数据标题
* @param sheetName sheet名称
* @param pojoClass pojo类型
* @param fileName 导出时的excel名称
* @param response
*/
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, HttpServletResponse response) throws IOException {
defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName, ExcelType.XSSF));
}
/**
* excel 导出
*
* @param list 数据列表
* @param title 表格内数据标题
* @param sheetName sheet名称
* @param pojoClass pojo类型
* @param fileName 导出时的excel名称
* @param isCreateHeader 是否创建表头
* @param response
*/
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response) throws IOException {
ExportParams exportParams = new ExportParams(title, sheetName, ExcelType.XSSF);
exportParams.setCreateHeadRows(isCreateHeader);
defaultExport(list, pojoClass, fileName, response, exportParams);
}
/**
* excel 导出多sheet
*
* @param list 数据列表
* @param title 表格内数据标题
* @param sheetName sheet名称
* @param pojoClass pojo类型
* @param fileName 导出时的excel名称
* @param response
*/
public static void exportExcelSheet(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, HttpServletResponse response) throws IOException {
int num = list.size()/size;
int last = list.size()%size;
if (last!=0){
num+=1;
}
List<Map<String, Object>> lists = new ArrayList<>();
for (int i = 0; i <num ; i++) {
if (i==num-1){
Set<?> subList=new HashSet<>(list.subList(size*i,list.size()));
Map<String, Object> oneSheet = createOneSheet(sheetName + i, title, pojoClass, subList);
lists.add(oneSheet);
}else {
Set<?> subList=new HashSet<>(list.subList(size*i,size*(i+1)));
Map<String, Object> oneSheet = createOneSheet(sheetName + (i+1), title, pojoClass, subList);
lists.add(oneSheet);
}
}
Workbook sheetWorkBook = sheetWorkBook(lists);
downLoadExcel(fileName,response,sheetWorkBook);
}
/**
* 创建workbook,
* 通过maplist填充Excel内容
* 返回workbook
*
* 进一步使用可以写入流,e.g.
* FileOutputStream fos = new FileOutputStream(file);
* workbook.write(fos);
* */
private static Workbook sheetWorkBook(List<Map<String, Object>> mapListList){
Workbook workbook = null;
workbook = ExcelExportUtil.exportExcel(mapListList, ExcelType.XSSF);
return workbook;
}
/**
*
* @param exportParams
* @param clazz
* @param data
* @return
*/
private static Map<String, Object> createOneSheet(ExportParams exportParams, Class<?> clazz, Set<?> data){
Map<String, Object> map = new HashMap<>();
map.put("title",exportParams);
map.put("entity", clazz);
map.put("data",data);
return map;
}
/***
*
* @param sheetName
* @param title
* @param clazz
* @param data
* @return
*/
private static Map<String, Object> createOneSheet(String sheetName, String title, Class<?> clazz, Set<?> data){
ExportParams exportParams = new ExportParams(title,sheetName, ExcelType.XSSF);
return createOneSheet(exportParams,clazz,data);
}
/**
* excel下载
*
* @param fileName 下载时的文件名称
* @param response
* @param workbook excel数据
*/
private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) throws IOException {
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xlsx", "UTF-8"));
workbook.write(response.getOutputStream());
} catch (Exception e) {
throw new IOException(e.getMessage());
}
}
/**
* excel 导入
*
* @param file excel文件
* @param pojoClass pojo类型
* @param <T>
* @return
*/
public static <T> List<T> importExcel(MultipartFile file, Class<T> pojoClass) throws IOException {
return importExcel(file, 1, 1, pojoClass);
}
/**
* excel 导入
*
* @param filePath excel文件路径
* @param titleRows 表格内数据标题行
* @param headerRows 表头行
* @param pojoClass pojo类型
* @param <T>
* @return
*/
public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws IOException {
if (StringUtils.isBlank(filePath)) {
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
params.setNeedSave(true);
params.setSaveUrl("/excel/");
try {
return ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
} catch (NoSuchElementException e) {
throw new IOException("模板不能为空");
} catch (Exception e) {
throw new IOException(e.getMessage());
}
}
/**
* excel 导入
*
* @param file 上传的文件
* @param titleRows 表格内数据标题行
* @param headerRows 表头行
* @param pojoClass pojo类型
* @param <T>
* @return
*/
public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws IOException {
if (file == null) {
return null;
}
try {
return importExcel(file.getInputStream(), titleRows, headerRows, pojoClass);
} catch (Exception e) {
throw new IOException(e.getMessage());
}
}
/**
* excel 导入
*
* @param inputStream 文件输入流
* @param titleRows 表格内数据标题行
* @param headerRows 表头行
* @param pojoClass pojo类型
* @param <T>
* @return
*/
public static <T> List<T> importExcel(InputStream inputStream, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws IOException {
if (inputStream == null) {
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
params.setSaveUrl("/excel/");
params.setNeedSave(true);
try {
return ExcelImportUtil.importExcel(inputStream, pojoClass, params);
} catch (NoSuchElementException e) {
throw new IOException("excel文件不能为空");
} catch (Exception e) {
throw new IOException(e.getMessage());
}
}
}