EasyPoi 导出Excel(ExcelExportEntity生成表头)

2022-12-23,,,

[引入依赖]

 <!--easypoi-->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.0.1</version>
</dependency>
 /**
* 写入数据导出Excel
*
* @param response
*/
@GetMapping("/writeExcel")
public void writeExcel(HttpServletResponse response) throws IOException {
ExportParams params = new ExportParams();
//生成要导出的数据
List<Map<String, Object>> resultList = new ArrayList<>();
List<List<Object>> tempDataList = new ArrayList<>();
List<Object> tempRowList1 = new ArrayList<>(Arrays.asList(1, 8, 1, 4, 2, 0, 9, 2, 2, 2));
tempDataList.add(tempRowList1);
List<Object> tempRowList2 = new ArrayList<>(Arrays.asList(0, 1, 8, 3, 4, 5, 7, 7, 8, 9));
tempDataList.add(tempRowList2);
List<Object> tempRowList3 = new ArrayList<>(Arrays.asList(1, 1, 2, 8, 4, 5, 6, 7, 8, 8));
tempDataList.add(tempRowList3); for (List<Object> rowList : tempDataList) {
System.out.println("这个集合的值是:" + rowList);
Map<String, Object> rowMap = new HashMap<>();
for (int i = 1; i <= rowList.size(); i++) {
rowMap.put("title" + i, rowList.get(i - 1));
}
resultList.add(rowMap);
} //列名
List<ExcelExportEntity> entityList = new ArrayList<>();
for (int i = 1; i <= tempRowList1.size(); i++) {
ExcelExportEntity entity = new ExcelExportEntity("标题" + i, "title" + i);
entityList.add(entity);
} Workbook workbook = ExcelExportUtil.exportExcel(params, entityList, resultList); response.addHeader("Content-Disposition", "filename=test.xlsx");
//设置类型,扩展名为.xls
response.setContentType("application/vnd.ms-excel");
workbook.write(response.getOutputStream());
}

表头的值和数据的key是需要对应的

EasyPoi 导出Excel(ExcelExportEntity生成表头)的相关教程结束。

《EasyPoi 导出Excel(ExcelExportEntity生成表头).doc》

下载本文的Word格式文档,以方便收藏与打印。