From 8cc76ba8799a439c488bb85d632740d10dbd922a Mon Sep 17 00:00:00 2001 From: "ray.zh" Date: Sat, 5 Oct 2024 20:32:43 +0800 Subject: [PATCH] =?UTF-8?q?Create=202024-10-05-easypoi=E5=AF=BC=E5=87=BA?= =?UTF-8?q?=E5=A4=9Asheet=E7=9A=84excel.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\272\345\244\232sheet\347\232\204excel.md" | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 "_posts/2024-10-05-easypoi\345\257\274\345\207\272\345\244\232sheet\347\232\204excel.md" diff --git "a/_posts/2024-10-05-easypoi\345\257\274\345\207\272\345\244\232sheet\347\232\204excel.md" "b/_posts/2024-10-05-easypoi\345\257\274\345\207\272\345\244\232sheet\347\232\204excel.md" new file mode 100644 index 0000000..34c1bb2 --- /dev/null +++ "b/_posts/2024-10-05-easypoi\345\257\274\345\207\272\345\244\232sheet\347\232\204excel.md" @@ -0,0 +1,129 @@ +--- +layout: post +title: "easypoi导出多sheet的excel" +date: 2024-10-05 13:29:20 +0800 +categories: + - java +tags: + - excel导出 + - 多sheetExcel导出 +--- + +项目有个导出需求,需要导出多个sheet,xlsx格式的excel表格。 + +这里简单说明下,在excel中,有xls,xlsx两种格式。 + +- xls是excel2005版本以下的文件格式,支持数据量小而且格式很老。 +- xlsx是2005版本以上支持的格式,数据量大而且新特性多 + +我觉得都2024年了,大家导出excel应该都使用xlsx格式了吧。 + +在网上找了一圈没有什么好使的代码(其实找到了一个xls的导出代码,可以直接使用)。但是xlsx的却没找到现成的,于是基于xls的修改了下,这里直接贴出可用的,大家拿去直接食用吧。 + +1. 引入依赖pom +```xml + + cn.afterturn + easypoi-base + 4.5.0 + + + cn.afterturn + easypoi-web + 4.5.0 + + + cn.afterturn + easypoi-annotation + 4.5.0 + + +``` + +2. 导入上面的依赖后,样例java代码如下: + +大概说明下,以下代码中定义了两个sheet, 并导出到`test.xlsx`文件中 +```java + + import cn.afterturn.easypoi.excel.entity.ExportParams; + import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; + import cn.afterturn.easypoi.excel.export.ExcelExportService; + import org.apache.poi.ss.usermodel.Workbook; + import org.apache.poi.xssf.streaming.SXSSFWorkbook; + import org.apache.poi.xssf.usermodel.XSSFWorkbook; + import org.apache.poi.xssf.usermodel.XSSFWorkbookType; + + // java导出代码 + List data1List = new ArrayList(); + List data2List = new ArrayList(); + Workbook workbook = new XSSFWorkbook(XSSFWorkbookType.XLSX); + ExportParams exportParams0 = new ExportParams(); + exportParams0.setSheetName("sheet1"); + exportParams0.setType(ExcelType.XSSF); + ExportParams exportParams1 = new ExportParams(); + exportParams1.setSheetName("shhet2"); + exportParams1.setType(ExcelType.XSSF); + + ExcelExportService service0 = new ExcelExportService(); + // 这里将data1List集合导出到excel,sheet1中 + service0.createSheet(workbook, exportParams0, Data1.class, data1List); + ExcelExportService service1 = new ExcelExportService(); + // 这里将data2List集合导出到excel,sheet2中 + service1.createSheet(workbook, exportParams1, Data2.class, data2List); + + + FileOutputStream fos = null; + try { + // 写出文件 + File f = new File("./test.xlsx"); + f.createNewFile(); + fos = new FileOutputStream(f); + workbook.write(fos); + fos.close(); + } catch (Exception e) { + throw new RuntimeException(e); + } +``` + +最后一点,定义导出实体类时需要注意, 所有的导出字段需要指定`width`属性,不然当前版本会报错。 + +```java +@Data +@ExcelTarget(value = "Data1") +public class Data1 implements Serializable { + /** + * 审核状态 + * commAuditStatus + */ + @Excel(name = "审核状态", width = 20) + private String commAuditStatus; + + /** + * 申请单号 + * billId + */ + @Excel(name = "申请单号", width = 20) + private String billId; +} + + +@Data +@ExcelTarget(value = "Data2") +public class Data2 implements Serializable { + /** + * 审核状态2 + * commAuditStatus + */ + @Excel(name = "审核状态2", width = 20) + private String commAuditStatus; + + /** + * 申请单号2 + * billId + */ + @Excel(name = "申请单号2", width = 20) + private String billId; +} +``` + +上面代码粘贴即可直接食用,好用点个赞!