工作中遇到一个需求,一次导出多个Excel 文件,并且每个excel中可能存在1到多个sheet页。
好在没有那种单元格合并的要求。
总体的思路是,设计两个实体,一个表示表格,一个表示sheet 数据。并且表格包含一个list 类型的sheet对象。
然后再使用ZipOutputStream
、ExcelWriterBuilder
、EasyExcel#writerSheet(...)
等类和方法去组装表格,最终进行压缩。
项目整体使用 java 8 和 阿里的easyexcel工具包。
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>2.2.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.2version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>easyexcelartifactId>
<version>2.2.11version>
<exclusions>
<exclusion>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
exclusion>
exclusions>
dependency>
dependencies>
/**
* excel数据
*/
@Data
public static class ExcelData {
// sheet的列表
private final List<ExcelShellData<?>> shellDataList = new ArrayList<>();
// 表格文件名
private String filename;
public void addShellData(ExcelShellData<?> excelShellData) {
this.shellDataList.add(excelShellData);
}
}
/**
* sheet数据
*/
@Data
@AllArgsConstructor
public static class ExcelShellData<T> {
// 数据
private List<T> list;
// sheet名
private String sheetName;
// 数据实体类型
private Class<T> clazz;
}
这一步主要组装表格数据,以及生成sheet。最终将数据放到输出流outputStream
中 。
private static void exportZipStream(List<ExcelData> excelDataList, OutputStream outputStream) {
try {
// 开始存入
try (ZipOutputStream zipOut = new ZipOutputStream(outputStream)) {
try {
for (ExcelData excelData : excelDataList) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
com.alibaba.excel.ExcelWriter excelWriter = null;
try {
ExcelWriterBuilder builder = EasyExcel.write(byteArrayOutputStream).autoCloseStream(false)
// 自动适配
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy());
excelWriter = builder.build();
zipOut.putNextEntry(new ZipEntry(excelData.getFilename()));
// 开始写入excel
for (ExcelShellData<?> shellData : excelData.getShellDataList()) {
WriteSheet writeSheet = EasyExcel.writerSheet(shellData.getSheetName()).head(shellData.getClazz()).build();
excelWriter.write(shellData.getList(), writeSheet);
}
} catch (Exception e) {
throw new RuntimeException("导出Excel异常", e);
} finally {
if (excelWriter != null) {
excelWriter.finish();
}
}
byteArrayOutputStream.writeTo(zipOut);
zipOut.closeEntry();
}
} catch (Exception e) {
throw new RuntimeException("导出Excel异常", e);
}
}
} catch (IOException e) {
throw new RuntimeException("导出Excel异常", e);
}
}
可以调用本方法,直接在Controller中调用之后,当访问对应url,会直接下载到浏览器。
/**
* 导出多个sheet到多个excel文件,并压缩到一个zip文件
*/
public static void exportZip(String zipFilename, List<ExcelData> excelDataList, HttpServletResponse response) {
try {
// 这里URLEncoder.encode可以防止中文乱码
zipFilename = URLEncoder.encode(zipFilename, "utf-8");
// 指定文件名
response.setHeader("Content-disposition", "attachment;filename=" + zipFilename);
response.setContentType("application/x-msdownload");
response.setCharacterEncoding("utf-8");
exportZipStream(excelDataList, response.getOutputStream());
} catch (IOException e) {
throw new RuntimeException("导出Excel异常", e);
}
}
当我们需要导出为字节数组时,可以调用本方法。之后随你怎么加工。
/**
* 导出多个sheet到多个excel文件,并压缩到一个zip文件。最终得到一个字节数组。
*/
public static byte[] exportZip(List<ExcelData> excelDataList) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
exportZipStream(excelDataList, byteArrayOutputStream);
return byteArrayOutputStream.toByteArray();
}
指定简单的几个字段作为导出数据。
@Data
@AllArgsConstructor
public static class DemoData {
@ExcelProperty("字符串标题")
private String string;
@ExcelProperty("日期标题")
private Date date;
@ExcelProperty("数字标题")
private Double doubleData;
}
组装假数据,进行导出。
@Controller
@RequestMapping("/excel")
public class ExcelDemoController {
@GetMapping("/exportTransportDetail")
public String exportTransportDetail(HttpServletResponse response) throws IOException {
// 压缩包文件名
String fileName = "结算单运单明细-" + System.currentTimeMillis() + ".zip";
List<ExcelData> excelDataList = new ArrayList<>();
// 第一个Excel
ExcelData excelData1 = new ExcelData();
excelData1.setFilename("结算单运单明细-1-" + System.currentTimeMillis() + ".xlsx");
List<DemoData> demoData1 = new ArrayList<>();
demoData1.add(new DemoData("excel-sheet1", new Date(), 123112.321));
demoData1.add(new DemoData("excel-sheet1", new Date(), 34.3));
List<DemoData> demoData2 = new ArrayList<>();
demoData2.add(new DemoData("excel-sheet2", new Date(), 123112.321));
demoData2.add(new DemoData("excel-sheet2", new Date(), 34.3));
ExcelShellData<DemoData> shellData1 = new ExcelShellData<>(demoData1, "sheet1", DemoData.class);
ExcelShellData<DemoData> shellData2 = new ExcelShellData<>(demoData2, "sheet2", DemoData.class);
excelData1.addShellData(shellData1);
excelData1.addShellData(shellData2);
// 第2个Excel
ExcelData excelData2 = new ExcelData();
excelData2.setFilename("结算单运单明细-2-" + System.currentTimeMillis() +".xlsx");
List<DemoData> demoData21 = new ArrayList<>();
demoData21.add(new DemoData("excel-sheet21", new Date(), 123112.321));
demoData21.add(new DemoData("excel-sheet22", new Date(), 34.3));
List<DemoData> demoData22 = new ArrayList<>();
demoData22.add(new DemoData("excel-sheet21", new Date(), 123112.321));
demoData22.add(new DemoData("excel-sheet22", new Date(), 34.3));
ExcelShellData<DemoData> shellData21 = new ExcelShellData<>(demoData21, "sheet1", DemoData.class);
ExcelShellData<DemoData> shellData22 = new ExcelShellData<>(demoData22, "sheet2", DemoData.class);
excelData2.addShellData(shellData21);
excelData2.addShellData(shellData22);
excelDataList.add(excelData1);
excelDataList.add(excelData2);
// 写法1///
// exportZip(fileName, excelDataList , response);
// 写法2///
byte[] bytes = exportZip(excelDataList);
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
response.setContentType("application/x-msdownload");
response.setCharacterEncoding("utf-8");
response.getOutputStream().write(bytes);
response.getOutputStream().flush();
return "succ";
}
}
可以看到压缩包解压后的效果:
其中一个文件内容如下:
sheet1:
/**
* Excel 数值长度大于maxLength的数值转换为字符串
*/
public static class ExcelBigNumberConvert implements Converter<Long> {
private final int maxLength;
public ExcelBigNumberConvert() {
this(15);
}
public ExcelBigNumberConvert(Integer maxLength) {
this.maxLength = maxLength;
}
@Override
public Class<Long> supportJavaTypeKey() {
return Long.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
@Override
public Long convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
Object data = cellData.getData();
if (data == null) {
return null;
}
String s = String.valueOf(data);
if (s.matches("^\\d+$")) {
return Long.parseLong(s);
}
return null;
}
@Override
public CellData<Object> convertToExcelData(Long object, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
if (object != null) {
String str = object.toString();
if (str.length() > maxLength) {
return new CellData<>(str);
}
}
return null;
}
}