fixed(Excel工具类重载):

1.调整Excel工具类的重构逻辑
This commit is contained in:
Emil.Zhang 2023-05-11 09:34:24 +08:00
parent b0781f9abe
commit 6ef008e718

View File

@ -91,6 +91,25 @@ public class ExcelUtil {
} }
} }
/**
* 导出excel
*
* @param list 导出数据集合
* @param sheetName 工作表的名称
* @param clazz 实体类
* @param response 响应体
* @param options 级联下拉选
*/
public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, HttpServletResponse response, List<DropDownOptions> options) {
try {
resetResponse(sheetName, response);
ServletOutputStream os = response.getOutputStream();
exportExcel(list, sheetName, clazz, false, os, options);
} catch (IOException e) {
throw new RuntimeException("导出Excel异常");
}
}
/** /**
* 导出excel * 导出excel
* *
@ -111,25 +130,20 @@ public class ExcelUtil {
} }
/** /**
* 导出带有下拉框的Excel表格 * 导出excel
* *
* @param options 下拉框数据 * @param list 导出数据集合
* @param sheetName 工作表的名称
* @param clazz 实体类
* @param merge 是否合并单元格
* @param response 响应体
* @param options 级联下拉选
*/ */
public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, boolean merge, HttpServletResponse response, List<DropDownOptions> options) {
HttpServletResponse response, List<DropDownOptions> options) {
try { try {
resetResponse(sheetName, response); resetResponse(sheetName, response);
ServletOutputStream os = response.getOutputStream(); ServletOutputStream os = response.getOutputStream();
ExcelWriterSheetBuilder builder = EasyExcel.write(os, clazz) exportExcel(list, sheetName, clazz, merge, os, options);
.autoCloseStream(false)
// 自动适配
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
// 大数值自动转换 防止失真
.registerConverter(new ExcelBigNumberConvert())
// 添加下拉框操作
.registerWriteHandler(new ExcelDownHandler(options))
.sheet(sheetName);
builder.doWrite(list);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("导出Excel异常"); throw new RuntimeException("导出Excel异常");
} }
@ -147,6 +161,19 @@ public class ExcelUtil {
exportExcel(list, sheetName, clazz, false, os); exportExcel(list, sheetName, clazz, false, os);
} }
/**
* 导出excel
*
* @param list 导出数据集合
* @param sheetName 工作表的名称
* @param clazz 实体类
* @param os 输出流
* @param options 级联下拉选内容
*/
public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, OutputStream os, List<DropDownOptions> options) {
exportExcel(list, sheetName, clazz, false, os, options);
}
/** /**
* 导出excel * 导出excel
* *
@ -177,7 +204,7 @@ public class ExcelUtil {
* @param options 下拉框数据 * @param options 下拉框数据
*/ */
public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz,
OutputStream os, List<DropDownOptions> options) { boolean merge, OutputStream os, List<DropDownOptions> options) {
ExcelWriterSheetBuilder builder = EasyExcel.write(os, clazz) ExcelWriterSheetBuilder builder = EasyExcel.write(os, clazz)
.autoCloseStream(false) .autoCloseStream(false)
// 自动适配 // 自动适配
@ -187,6 +214,10 @@ public class ExcelUtil {
// 添加下拉框操作 // 添加下拉框操作
.registerWriteHandler(new ExcelDownHandler(options)) .registerWriteHandler(new ExcelDownHandler(options))
.sheet(sheetName); .sheet(sheetName);
if (merge) {
// 合并处理器
builder.registerWriteHandler(new CellMergeStrategy(list, true));
}
builder.doWrite(list); builder.doWrite(list);
} }