Browse Source

更换easypoi版本,新增导出方法

master
YUJT 4 years ago
parent
commit
4bc760d912
  1. 7
      epdc-commons-tools/pom.xml
  2. 72
      epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/utils/ExcelUtils.java

7
epdc-commons-tools/pom.xml

@ -17,7 +17,7 @@
<commons.lang.version>3.7</commons.lang.version>
<commons.fileupload.version>1.3.3</commons.fileupload.version>
<hutool.version>4.1.8</hutool.version>
<easypoi.version>3.1.0</easypoi.version>
<easypoi.version>4.3.0</easypoi.version>
<joda.time.version>2.9.9</joda.time.version>
<fastjson.version>1.2.59</fastjson.version>
<jsoup.version>1.11.3</jsoup.version>
@ -91,6 +91,11 @@
<artifactId>easypoi-web</artifactId>
<version>${easypoi.version}</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>${easypoi.version}</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>

72
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.
*
* <p>
* https://www.renren.io
*
* <p>
* 版权所有侵权必究
*/
@ -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工具类
@ -45,15 +45,15 @@ public class ExcelUtils {
*/
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,6 +64,46 @@ 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 <T> void exportBigExcel(String fileName, Object queryParams, IExcelExportServer server,
HttpServletResponse response, ExportParams params, Class<T> 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<targetClass>再导出
*
@ -75,7 +115,7 @@ public class ExcelUtils {
public static void exportExcelToTarget(HttpServletResponse response, String fileName, Collection<?> sourceList,
Class<?> targetClass) throws Exception {
List<Object> 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)$");

Loading…
Cancel
Save