1. pom相关
<dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-spring-boot-starter</artifactId> <version>4.1.0</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>4.1.0</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-web</artifactId> <version>4.1.0</version> </dependency>
2. 导出方法
void test2(HttpServletRequest request, HttpServletResponse response) { List<Map<String, Object>> data = new ArrayList<>(); Map<String, Object> sheet1 = new HashMap<>(); ExportParams exportParams = new ExportParams(null,"sheet1"); sheet1.put("title", exportParams); sheet1.put("entity", ZsResultT2.class); List<ZsResultT2> sheet1Data = new ArrayList<>(); ZsResultT2 item1 = new ZsResultT2(); item1.setCode("1111"); item1.setNo(1); item1.setShusheng("河北省"); sheet1Data.add(item1); sheet1.put("data", sheet1Data); data.add(sheet1); Map<String, Object> sheet12 = new HashMap<>(); ExportParams exportParams2 = new ExportParams(null,"sheet12"); sheet12.put("title", exportParams2); sheet12.put("entity", ZsResultT2.class); List<ZsResultT2> sheet1Data2 = new ArrayList<>(); ZsResultT2 item12 = new ZsResultT2(); item12.setCode("1111"); item12.setNo(1); item12.setShusheng("河北省"); sheet1Data2.add(item12); sheet12.put("data", sheet1Data2); data.add(sheet12); easypoiUtil.exportExcelMoreSheet(request, response, "test.xls", null, data); }
3. 相关类
package com.diit.dwyxbd.utils; import cn.afterturn.easypoi.excel.ExcelExportUtil; import cn.afterturn.easypoi.excel.entity.ExportParams; import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; import cn.afterturn.easypoi.excel.export.ExcelExportService; import com.alibaba.druid.util.StringUtils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.net.URLEncoder; import java.util.*; import java.util.stream.Collectors; @Service("easypoiUtil") public class EasypoiUtil2 {
/*try {
FileOutputStream fos = new FileOutputStream(exportFolder.getPath() + File.separator + excelName);
workbook.write(fos);
result.put("path", exportFolder.getPath());
result.put("fileName", excelName);
System.out.println(fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}*/
public boolean exportExcelMoreSheet(HttpServletRequest request, HttpServletResponse response, String excelName, List<Map<String, Object>> title, List<Map<String, Object>> data) { Workbook workbook = ExcelExportUtil.exportExcel(data, ExcelType.HSSF); //下载生成的excel try { String userAgent = request.getHeader("user-agent").toLowerCase(); if (userAgent.contains("msie") || userAgent.contains("like gecko")) { excelName = URLEncoder.encode(excelName, "UTF-8"); } else { excelName = new String(excelName.getBytes("utf-8"), "ISO-8859-1"); } response.setCharacterEncoding("UTF-8"); response.setHeader("content-Type", "application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(excelName, "UTF-8")); OutputStream out = response.getOutputStream(); workbook.write(out); out.close(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } }
参考 https://blog.csdn.net/weixin_45624150/article/details/107180646
标签:cn,Excel,导出,new,put,easypoi,import,afterturn From: https://www.cnblogs.com/qukaige/p/17387739.html