|
|
@ -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)$"); |
|
|
|