From 4bc760d91246751ea4d99ab2e382518f87389ed0 Mon Sep 17 00:00:00 2001 From: YUJT Date: Fri, 18 Jun 2021 09:44:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=8D=A2easypoi=E7=89=88=E6=9C=AC?= =?UTF-8?q?=EF=BC=8C=E6=96=B0=E5=A2=9E=E5=AF=BC=E5=87=BA=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- epdc-commons-tools/pom.xml | 7 +- .../epdc/commons/tools/utils/ExcelUtils.java | 88 ++++++++++++++----- 2 files changed, 72 insertions(+), 23 deletions(-) diff --git a/epdc-commons-tools/pom.xml b/epdc-commons-tools/pom.xml index f95b582..cacc5eb 100644 --- a/epdc-commons-tools/pom.xml +++ b/epdc-commons-tools/pom.xml @@ -17,7 +17,7 @@ 3.7 1.3.3 4.1.8 - 3.1.0 + 4.3.0 2.9.9 1.2.59 1.11.3 @@ -91,6 +91,11 @@ easypoi-web ${easypoi.version} + + cn.afterturn + easypoi-annotation + ${easypoi.version} + joda-time joda-time diff --git a/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/utils/ExcelUtils.java b/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/utils/ExcelUtils.java index f3f675b..5a3d356 100644 --- a/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/utils/ExcelUtils.java +++ b/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/utils/ExcelUtils.java @@ -1,8 +1,8 @@ /** * Copyright (c) 2018 人人开源 All rights reserved. - * + *

* https://www.renren.io - * + *

* 版权所有,侵权必究! */ @@ -10,9 +10,11 @@ package com.elink.esua.epdc.commons.tools.utils; import cn.afterturn.easypoi.excel.ExcelExportUtil; import cn.afterturn.easypoi.excel.entity.ExportParams; +import cn.afterturn.easypoi.excel.export.ExcelBatchExportService; +import cn.afterturn.easypoi.handler.inter.IExcelExportServer; import org.apache.commons.lang3.StringUtils; -import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.springframework.beans.BeanUtils; @@ -20,12 +22,10 @@ import org.springframework.web.multipart.MultipartFile; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.net.URLEncoder; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Date; -import java.util.List; +import java.util.*; /** * Excel工具类 @@ -38,22 +38,22 @@ public class ExcelUtils { /** * Excel导出 * - * @param response response - * @param fileName 文件名 - * @param list 数据List - * @param pojoClass 对象Class + * @param response response + * @param fileName 文件名 + * @param list 数据List + * @param pojoClass 对象Class */ public static void exportExcel(HttpServletResponse response, String fileName, Collection list, Class pojoClass) throws IOException { - if(StringUtils.isBlank(fileName)){ + if (StringUtils.isBlank(fileName)) { //当前日期 fileName = DateUtils.format(new Date()); } Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), pojoClass, list); Sheet sheet1 = workbook.getSheetAt(0); - sheet1.setDefaultColumnWidth(50*256); - sheet1.setDefaultRowHeight((short)(2*256)); + sheet1.setDefaultColumnWidth(50 * 256); + sheet1.setDefaultRowHeight((short) (2 * 256)); response.setCharacterEncoding("UTF-8"); response.setHeader("content-Type", "application/vnd.ms-excel"); response.setHeader("Content-Disposition", @@ -64,18 +64,58 @@ public class ExcelUtils { out.close(); } + /** + * 导出大量数据(超百万行的数据,建议用CSV导出。) + * + * @param fileName 文件名 + * @param queryParams 查询条件 + * @param server 查询服务 + * @param response 浏览器请求返回 + * @param params Excel属性 + * @param pojoClass Excel对象Class + * @return void + * @author work@yujt.net.cn + * @date 2021/6/17 13:55 + */ + public static void exportBigExcel(String fileName, Object queryParams, IExcelExportServer server, + HttpServletResponse response, ExportParams params, Class pojoClass) throws IOException { + ExcelBatchExportService batchService = new ExcelBatchExportService(); + batchService.init(params, pojoClass); + Workbook workbook = batchService.exportBigExcel(server, queryParams); + + if (StringUtils.isBlank(fileName)) { + //当前日期 + fileName = DateUtils.format(new Date()); + } + + response.setCharacterEncoding("UTF-8"); + response.setHeader("content-Type", "application/vnd.ms-excel"); + response.setHeader("Content-Disposition", + "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx"); + + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + workbook.write(byteArrayOutputStream); + response.setHeader("Content-Length", String.valueOf(byteArrayOutputStream.size())); + + ServletOutputStream out = response.getOutputStream(); + out.write(byteArrayOutputStream.toByteArray()); + + out.flush(); + out.close(); + } + /** * Excel导出,先sourceList转换成List,再导出 * - * @param response response - * @param fileName 文件名 - * @param sourceList 原数据List - * @param targetClass 目标对象Class + * @param response response + * @param fileName 文件名 + * @param sourceList 原数据List + * @param targetClass 目标对象Class */ public static void exportExcelToTarget(HttpServletResponse response, String fileName, Collection sourceList, Class targetClass) throws Exception { List targetList = new ArrayList<>(sourceList.size()); - for(Object source : sourceList){ + for (Object source : sourceList) { Object target = targetClass.newInstance(); BeanUtils.copyProperties(source, target); targetList.add(target); @@ -86,12 +126,13 @@ public class ExcelUtils { /** * 获取单元格内容 + * * @param cell * @return */ - public static String getCellContent(Cell cell){ - String value = ""; - if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { + public static String getCellContent(Cell cell) { + String value; + if (cell.getCellType() == CellType.NUMERIC) { value = String.valueOf(cell.getNumericCellValue()); } else { value = cell.getStringCellValue(); @@ -105,6 +146,7 @@ public class ExcelUtils { /** * 获取excle版本 + * * @param mFile * @return */ @@ -127,6 +169,7 @@ public class ExcelUtils { /** * 验证EXCEL文件 + * * @param filePath * @return */ @@ -141,6 +184,7 @@ public class ExcelUtils { public static boolean isExcel2003(String filePath) { return filePath.matches("^.+\\.(?i)(xls)$"); } + // @描述:是否是2007的excel,返回true是2007 public static boolean isExcel2007(String filePath) { return filePath.matches("^.+\\.(?i)(xlsx)$");