|
@ -10,13 +10,20 @@ package com.elink.esua.epdc.commons.tools.utils; |
|
|
|
|
|
|
|
|
import cn.afterturn.easypoi.excel.ExcelExportUtil; |
|
|
import cn.afterturn.easypoi.excel.ExcelExportUtil; |
|
|
import cn.afterturn.easypoi.excel.entity.ExportParams; |
|
|
import cn.afterturn.easypoi.excel.entity.ExportParams; |
|
|
|
|
|
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; |
|
|
import cn.afterturn.easypoi.excel.export.ExcelBatchExportService; |
|
|
import cn.afterturn.easypoi.excel.export.ExcelBatchExportService; |
|
|
import cn.afterturn.easypoi.handler.inter.IExcelExportServer; |
|
|
import cn.afterturn.easypoi.handler.inter.IExcelExportServer; |
|
|
|
|
|
import com.elink.esua.epdc.commons.tools.exception.RenException; |
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
|
|
import org.apache.poi.hssf.usermodel.DVConstraint; |
|
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFDataValidation; |
|
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
|
|
import org.apache.poi.ss.usermodel.Cell; |
|
|
import org.apache.poi.ss.usermodel.Cell; |
|
|
import org.apache.poi.ss.usermodel.CellType; |
|
|
import org.apache.poi.ss.usermodel.CellType; |
|
|
import org.apache.poi.ss.usermodel.Sheet; |
|
|
import org.apache.poi.ss.usermodel.Sheet; |
|
|
import org.apache.poi.ss.usermodel.Workbook; |
|
|
import org.apache.poi.ss.usermodel.Workbook; |
|
|
|
|
|
import org.apache.poi.ss.util.CellRangeAddressList; |
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
|
|
import org.springframework.beans.BeanUtils; |
|
|
import org.springframework.beans.BeanUtils; |
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
|
|
|
|
@ -24,6 +31,7 @@ import javax.servlet.ServletOutputStream; |
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
import java.io.ByteArrayOutputStream; |
|
|
import java.io.ByteArrayOutputStream; |
|
|
import java.io.IOException; |
|
|
import java.io.IOException; |
|
|
|
|
|
import java.io.InputStream; |
|
|
import java.net.URLEncoder; |
|
|
import java.net.URLEncoder; |
|
|
import java.util.*; |
|
|
import java.util.*; |
|
|
|
|
|
|
|
@ -167,6 +175,66 @@ public class ExcelUtils { |
|
|
return ""; |
|
|
return ""; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Excel导出,先sourceList转换成List<targetClass>,再导出 |
|
|
|
|
|
* |
|
|
|
|
|
* @param response response |
|
|
|
|
|
* @param fileName 文件名 |
|
|
|
|
|
* @param sourceList 原数据List |
|
|
|
|
|
* @param sheetNames sheet名字列表 |
|
|
|
|
|
*/ |
|
|
|
|
|
public static void exportExcelToTargetWithSheets(HttpServletResponse response, String fileName, List<String> sheetNames, |
|
|
|
|
|
List<Class<?>> targetClassList,Collection<?>... sourceList) throws Exception { |
|
|
|
|
|
if(sheetNames.size()!=targetClassList.size() && targetClassList.size()!=sourceList.length){ |
|
|
|
|
|
throw new RenException("参数传递出错"); |
|
|
|
|
|
} |
|
|
|
|
|
// 将sheets使用得map进行包装
|
|
|
|
|
|
List<Map<String, Object>> sheetsList = new ArrayList<>(); |
|
|
|
|
|
for (int i = 0;i<sheetNames.size();i++) { |
|
|
|
|
|
Map<String, Object> deptDataMap = new HashMap<>(4); |
|
|
|
|
|
ExportParams exportParams = new ExportParams(); |
|
|
|
|
|
exportParams.setSheetName(sheetNames.get(i)); |
|
|
|
|
|
deptDataMap.put("title", exportParams); |
|
|
|
|
|
// 模版导出对应得实体类型
|
|
|
|
|
|
deptDataMap.put("entity", targetClassList.get(i)); |
|
|
|
|
|
// sheet中要填充得数据
|
|
|
|
|
|
deptDataMap.put("data", sourceList[i]); |
|
|
|
|
|
sheetsList.add(deptDataMap); |
|
|
|
|
|
} |
|
|
|
|
|
exportExcelWithSheets(response,fileName,sheetsList); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 多sheetExcel导出 |
|
|
|
|
|
* |
|
|
|
|
|
* @param response response |
|
|
|
|
|
* @param fileName 文件名 |
|
|
|
|
|
* @param sheetsList 多sheet组装 |
|
|
|
|
|
*/ |
|
|
|
|
|
public static void exportExcelWithSheets(HttpServletResponse response, String fileName, List<Map<String, Object>> sheetsList) throws IOException { |
|
|
|
|
|
|
|
|
|
|
|
if(StringUtils.isBlank(fileName)){ |
|
|
|
|
|
//当前日期
|
|
|
|
|
|
fileName = DateUtils.format(new Date()); |
|
|
|
|
|
} |
|
|
|
|
|
Workbook workbook = null; |
|
|
|
|
|
try { |
|
|
|
|
|
workbook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF); |
|
|
|
|
|
} catch (Exception ex) { |
|
|
|
|
|
workbook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.XSSF); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
response.setCharacterEncoding("UTF-8"); |
|
|
|
|
|
response.setHeader("content-Type", "application/vnd.ms-excel"); |
|
|
|
|
|
response.setHeader("Content-Disposition", |
|
|
|
|
|
"attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xls"); |
|
|
|
|
|
ServletOutputStream out = response.getOutputStream(); |
|
|
|
|
|
workbook.write(out); |
|
|
|
|
|
out.flush(); |
|
|
|
|
|
out.close(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 验证EXCEL文件 |
|
|
* 验证EXCEL文件 |
|
|
* |
|
|
* |
|
@ -189,4 +257,111 @@ public class ExcelUtils { |
|
|
public static boolean isExcel2007(String filePath) { |
|
|
public static boolean isExcel2007(String filePath) { |
|
|
return filePath.matches("^.+\\.(?i)(xlsx)$"); |
|
|
return filePath.matches("^.+\\.(?i)(xlsx)$"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 得到Workbook对象 |
|
|
|
|
|
* |
|
|
|
|
|
* @param file |
|
|
|
|
|
* @return |
|
|
|
|
|
* @throws IOException |
|
|
|
|
|
*/ |
|
|
|
|
|
public static Workbook getWorkBook(MultipartFile file) throws IOException { |
|
|
|
|
|
//这样写 excel 能兼容03和07
|
|
|
|
|
|
InputStream is = file.getInputStream(); |
|
|
|
|
|
Workbook hssfWorkbook = null; |
|
|
|
|
|
try { |
|
|
|
|
|
hssfWorkbook = new HSSFWorkbook(is); |
|
|
|
|
|
} catch (Exception ex) { |
|
|
|
|
|
is = file.getInputStream(); |
|
|
|
|
|
hssfWorkbook = new XSSFWorkbook(is); |
|
|
|
|
|
} |
|
|
|
|
|
return hssfWorkbook; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Excel导出,先sourceList转换成List<targetClass>,再导出 |
|
|
|
|
|
* |
|
|
|
|
|
* @param response response |
|
|
|
|
|
* @param fileName 文件名 |
|
|
|
|
|
* @param sourceList 原数据List |
|
|
|
|
|
* @param sheetNames sheet名字列表 |
|
|
|
|
|
*/ |
|
|
|
|
|
public static void exportExcelToTargetWithSheets(HttpServletResponse response, String fileName, List<String> sheetNames, |
|
|
|
|
|
List<Class<?>> targetClassList, List<ExcelSelectionDto> excelSelectionDtos, Collection<?>... sourceList) throws Exception { |
|
|
|
|
|
if (sheetNames.size() != targetClassList.size() && targetClassList.size() != sourceList.length) { |
|
|
|
|
|
throw new RenException("参数传递出错"); |
|
|
|
|
|
} |
|
|
|
|
|
// 将sheets使用得map进行包装
|
|
|
|
|
|
List<Map<String, Object>> sheetsList = new ArrayList<>(); |
|
|
|
|
|
for (int i = 0; i < sheetNames.size(); i++) { |
|
|
|
|
|
Map<String, Object> deptDataMap = new HashMap<>(4); |
|
|
|
|
|
ExportParams exportParams = new ExportParams(); |
|
|
|
|
|
exportParams.setSheetName(sheetNames.get(i)); |
|
|
|
|
|
deptDataMap.put("title", exportParams); |
|
|
|
|
|
// 模版导出对应得实体类型
|
|
|
|
|
|
deptDataMap.put("entity", targetClassList.get(i)); |
|
|
|
|
|
// sheet中要填充得数据
|
|
|
|
|
|
deptDataMap.put("data", sourceList[i]); |
|
|
|
|
|
sheetsList.add(deptDataMap); |
|
|
|
|
|
} |
|
|
|
|
|
exportExcelWithSheets(response, fileName, sheetsList, excelSelectionDtos); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 多sheetExcel导出 |
|
|
|
|
|
* |
|
|
|
|
|
* @param response response |
|
|
|
|
|
* @param fileName 文件名 |
|
|
|
|
|
* @param sheetsList 多sheet组装 |
|
|
|
|
|
*/ |
|
|
|
|
|
public static void exportExcelWithSheets(HttpServletResponse response, String fileName, List<Map<String, Object>> sheetsList, List<ExcelSelectionDto> excelSelectionDtos) throws IOException { |
|
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isBlank(fileName)) { |
|
|
|
|
|
//当前日期
|
|
|
|
|
|
fileName = DateUtils.format(new Date()); |
|
|
|
|
|
} |
|
|
|
|
|
Workbook workbook = null; |
|
|
|
|
|
try { |
|
|
|
|
|
workbook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF); |
|
|
|
|
|
} catch (Exception ex) { |
|
|
|
|
|
workbook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.XSSF); |
|
|
|
|
|
} |
|
|
|
|
|
//插入下拉列表
|
|
|
|
|
|
if (excelSelectionDtos != null && excelSelectionDtos.size() > 0) { |
|
|
|
|
|
for (ExcelSelectionDto excelSelectionDto : excelSelectionDtos) { |
|
|
|
|
|
selectList(workbook, excelSelectionDto.getSheetIndex(), excelSelectionDto.getFirstCol(), excelSelectionDto.getLastCol(), excelSelectionDto.getExcelSelections()); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
response.setCharacterEncoding("UTF-8"); |
|
|
|
|
|
response.setHeader("content-Type", "application/vnd.ms-excel"); |
|
|
|
|
|
response.setHeader("Content-Disposition", |
|
|
|
|
|
"attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xls"); |
|
|
|
|
|
ServletOutputStream out = response.getOutputStream(); |
|
|
|
|
|
workbook.write(out); |
|
|
|
|
|
out.flush(); |
|
|
|
|
|
out.close(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* firstRow 開始行號 根据此项目,默认为2(下标0开始) |
|
|
|
|
|
* lastRow 根据此项目,默认为最大65535 |
|
|
|
|
|
* firstCol 区域中第一个单元格的列号 (下标0开始) |
|
|
|
|
|
* lastCol 区域中最后一个单元格的列号 |
|
|
|
|
|
* strings 下拉内容 |
|
|
|
|
|
*/ |
|
|
|
|
|
public static void selectList(Workbook workbook, Integer sheetIndex, int firstCol, int lastCol, String[] strings) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Sheet sheet = workbook.getSheetAt(sheetIndex); |
|
|
|
|
|
// 生成下拉列表
|
|
|
|
|
|
// 只对(x,x)单元格有效
|
|
|
|
|
|
CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(1, 65535, firstCol, lastCol); |
|
|
|
|
|
// 生成下拉框内容
|
|
|
|
|
|
DVConstraint dvConstraint = DVConstraint.createExplicitListConstraint(strings); |
|
|
|
|
|
HSSFDataValidation dataValidation = new HSSFDataValidation(cellRangeAddressList, dvConstraint); |
|
|
|
|
|
// 对sheet页生效
|
|
|
|
|
|
sheet.addValidationData(dataValidation); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|