铜川网站建设/百度产品大全入口
Easypoi的目标不是替代poi,而是让一个不懂导入导出的快速使用poi完成Excel和word的各种操作。
使用建议:大数据量,避免内存溢出,以及提升读取效率,最好行数在10万行级别以上使用。
演示使用:基于注解 @Excel 。
注解:
1、引入依赖
<dependency> <groupId>cn.afterturngroupId> <artifactId>easypoi-spring-boot-starterartifactId> <version>4.0.0version>dependency>
2、浏览器导出 ExcelUtils类
public class ExcelUtils { /** * 浏览器下载 * @param countPage 总页数 * @param pag 页数 * @param list 导出列表 * @param title 表头 * @param sheetName * @param pojoClass 导出类 * @param fileName 文件名 * @param response */ public static void exportutils(Integer countPage, Integer pag , List> list, String title, String sheetName, Class> pojoClass, String fileName, HttpServletResponse response){ Workbook workbook = defaultBigExcel(list, pojoClass, new ExportParams(title, sheetName)); if(countPage.equals(pag)){ ExcelExportUtil.closeExportBigExcel(); if (workbook != null){ downLoadExcel(fileName, response, workbook); } } } private static Workbook defaultBigExcel(List> list, Class> pojoClass, ExportParams exportParams) { return ExcelExportUtil.exportBigExcel(exportParams,pojoClass,list ); } private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) { try { response.setCharacterEncoding("UTF-8"); response.setHeader("content-Type", "application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); workbook.write(response.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } }}
3、测试 TEST
// 导出实体类@Data@Accessors(chain = true)public class ExcelVo { @Excel(name = "用户名", orderNum = "0" ,type = 1,width = 15 ) private String userName; @Excel(name = "年龄", orderNum = "1" ,type = 1) private Integer age; @Excel(name = "手机号", orderNum = "2",type = 1,width = 15) private String mobile; @Excel(name = "性别", orderNum = "2",type = 1,replace = { "1_男", "2_女"}) private Integer sex;}
@Overridepublic void export(HttpServletRequest request, HttpServletResponse response) { //需要导出的数据 Listlist = testuserMapper.export(null,null); Integer pageCount = list.size() / 2000 + 1; // 分批次导出 for(int i=1; i <= pageCount; i++){ List excel = testuserMapper.export(2000,i); ExcelUtils.exportutils(pageCount,i,excel,"内容表头","页码"+ i,ExcelVo.class,"导出.xlsx",response); excel.clear(); }}
4、效果图