|
@@ -40,173 +40,173 @@ import static com.rongwei.safecommon.utils.SaveConstans.DatePattern.DATE_PATTERN
|
|
|
*/
|
|
|
@Component
|
|
|
public class CommonEasyExcelUtils {
|
|
|
- private static final Logger log = LoggerFactory.getLogger(CommonEasyExcelUtils.class.getClass().getName());
|
|
|
- private static final int MAX_COLUMN_WIDTH = 255 * 256;
|
|
|
- @Autowired
|
|
|
- private SysDictDao autoSysDictDao;
|
|
|
-
|
|
|
- private static SysDictDao sysDictDao;
|
|
|
- private static SysFileFolderDo sysFileFolderDo;
|
|
|
-
|
|
|
-
|
|
|
- @PostConstruct
|
|
|
- public void init() {
|
|
|
- sysDictDao = autoSysDictDao;
|
|
|
- }
|
|
|
-
|
|
|
- private static final Map<String, String> CONTENT_TYPE_MAP = new HashMap<String, String>(4) {{
|
|
|
- put(SaveConstans.FileSuffix.XLS, SaveConstans.ContentType.XLS);
|
|
|
- put(SaveConstans.FileSuffix.XLSX, SaveConstans.ContentType.XLSX);
|
|
|
-
|
|
|
- }};
|
|
|
-
|
|
|
- /**
|
|
|
- * 生成excel文件
|
|
|
- *
|
|
|
- * @param exportExcelCommonDTO
|
|
|
- * @param dataList
|
|
|
- * @param <T>
|
|
|
- */
|
|
|
- public static <T> void exportExcelUtil(ExportExcelCommonVo exportExcelCommonDTO, List<T> dataList) {
|
|
|
- log.info("开始导出excel");
|
|
|
- // excel文件名称
|
|
|
- String excelFileName = exportExcelCommonDTO.getExcelFileName();
|
|
|
- // excel 文件后缀
|
|
|
- String excelFileSuffix = exportExcelCommonDTO.getExcelFileSuffix();
|
|
|
- // 数据字典处理
|
|
|
- List<CommonGenerateExcelVo> excelFieldDescDTOList = getDictData(exportExcelCommonDTO.getExcelFieldDescDTOList());
|
|
|
-
|
|
|
- String dateStr = new SimpleDateFormat(DATE_PATTERN_YMD).format(new Date());
|
|
|
-
|
|
|
- String fileName = StringUtils.isBlank(excelFileName) ? dateStr : excelFileName + dateStr;
|
|
|
- try (FileOutputStream outputStream = new FileOutputStream(new File("指定目录/文件名"))) {
|
|
|
-// OutputStream outputStream = response.getOutputStream();
|
|
|
-// response.setHeader("Content-disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
|
|
|
-// response.setContentType(String.format("%s;%s", CONTENT_TYPE_MAP.get(excelFileSuffix), "charset=UTF-8"));
|
|
|
- XSSFWorkbook excelWorkBook = new XSSFWorkbook();
|
|
|
- /****************************设置单元格居中样式*********************************/
|
|
|
- XSSFCellStyle centerStyle = excelWorkBook.createCellStyle();
|
|
|
- // 水平居中
|
|
|
- centerStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
- // 垂直居中
|
|
|
- centerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
-
|
|
|
- // 创建sheet
|
|
|
- XSSFSheet sheet = excelWorkBook.createSheet(StringUtils.isBlank(excelFileName) ? "sheet1" : excelFileName);
|
|
|
- /********************************创建标题行***********************************/
|
|
|
-// if(StringUtils.isNotBlank(excelFileName)){
|
|
|
-// XSSFRow firstRow = sheet.createRow(0);
|
|
|
-// XSSFCell firstRowCell = firstRow.createCell(0);
|
|
|
-// firstRowCell.setCellValue(excelFileName);
|
|
|
-// firstRowCell.setCellStyle(centerStyle);
|
|
|
+// private static final Logger log = LoggerFactory.getLogger(CommonEasyExcelUtils.class.getClass().getName());
|
|
|
+// private static final int MAX_COLUMN_WIDTH = 255 * 256;
|
|
|
+// @Autowired
|
|
|
+// private SysDictDao autoSysDictDao;
|
|
|
+//
|
|
|
+// private static SysDictDao sysDictDao;
|
|
|
+// private static SysFileFolderDo sysFileFolderDo;
|
|
|
+//
|
|
|
+//
|
|
|
+// @PostConstruct
|
|
|
+// public void init() {
|
|
|
+// sysDictDao = autoSysDictDao;
|
|
|
+// }
|
|
|
+//
|
|
|
+// private static final Map<String, String> CONTENT_TYPE_MAP = new HashMap<String, String>(4) {{
|
|
|
+// put(SaveConstans.FileSuffix.XLS, SaveConstans.ContentType.XLS);
|
|
|
+// put(SaveConstans.FileSuffix.XLSX, SaveConstans.ContentType.XLSX);
|
|
|
+//
|
|
|
+// }};
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 生成excel文件
|
|
|
+// *
|
|
|
+// * @param exportExcelCommonDTO
|
|
|
+// * @param dataList
|
|
|
+// * @param <T>
|
|
|
+// */
|
|
|
+// public static <T> void exportExcelUtil(ExportExcelCommonVo exportExcelCommonDTO, List<T> dataList) {
|
|
|
+// log.info("开始导出excel");
|
|
|
+// // excel文件名称
|
|
|
+// String excelFileName = exportExcelCommonDTO.getExcelFileName();
|
|
|
+// // excel 文件后缀
|
|
|
+// String excelFileSuffix = exportExcelCommonDTO.getExcelFileSuffix();
|
|
|
+// // 数据字典处理
|
|
|
+// List<CommonGenerateExcelVo> excelFieldDescDTOList = getDictData(exportExcelCommonDTO.getExcelFieldDescDTOList());
|
|
|
+//
|
|
|
+// String dateStr = new SimpleDateFormat(DATE_PATTERN_YMD).format(new Date());
|
|
|
+//
|
|
|
+// String fileName = StringUtils.isBlank(excelFileName) ? dateStr : excelFileName + dateStr;
|
|
|
+// try (FileOutputStream outputStream = new FileOutputStream(new File("指定目录/文件名"))) {
|
|
|
+//// OutputStream outputStream = response.getOutputStream();
|
|
|
+//// response.setHeader("Content-disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
|
|
|
+//// response.setContentType(String.format("%s;%s", CONTENT_TYPE_MAP.get(excelFileSuffix), "charset=UTF-8"));
|
|
|
+// XSSFWorkbook excelWorkBook = new XSSFWorkbook();
|
|
|
+// /****************************设置单元格居中样式*********************************/
|
|
|
+// XSSFCellStyle centerStyle = excelWorkBook.createCellStyle();
|
|
|
+// // 水平居中
|
|
|
+// centerStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+// // 垂直居中
|
|
|
+// centerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+//
|
|
|
+// // 创建sheet
|
|
|
+// XSSFSheet sheet = excelWorkBook.createSheet(StringUtils.isBlank(excelFileName) ? "sheet1" : excelFileName);
|
|
|
+// /********************************创建标题行***********************************/
|
|
|
+//// if(StringUtils.isNotBlank(excelFileName)){
|
|
|
+//// XSSFRow firstRow = sheet.createRow(0);
|
|
|
+//// XSSFCell firstRowCell = firstRow.createCell(0);
|
|
|
+//// firstRowCell.setCellValue(excelFileName);
|
|
|
+//// firstRowCell.setCellStyle(centerStyle);
|
|
|
+//// }
|
|
|
+//
|
|
|
+// /********************************创建描述行***********************************/
|
|
|
+// List<String> fieldDescList = excelFieldDescDTOList.stream().map(CommonGenerateExcelVo::getFiledDesc).collect(Collectors.toList());
|
|
|
+// XSSFCell cell;
|
|
|
+// XSSFRow descRow = sheet.createRow(0);
|
|
|
+// for (int i = 0; i < fieldDescList.size(); i++) {
|
|
|
+// cell = descRow.createCell(i);
|
|
|
+// cell.setCellValue(fieldDescList.get(i));
|
|
|
+// cell.setCellType(CellType.STRING);
|
|
|
+// cell.setCellStyle(centerStyle);
|
|
|
// }
|
|
|
-
|
|
|
- /********************************创建描述行***********************************/
|
|
|
- List<String> fieldDescList = excelFieldDescDTOList.stream().map(CommonGenerateExcelVo::getFiledDesc).collect(Collectors.toList());
|
|
|
- XSSFCell cell;
|
|
|
- XSSFRow descRow = sheet.createRow(0);
|
|
|
- for (int i = 0; i < fieldDescList.size(); i++) {
|
|
|
- cell = descRow.createCell(i);
|
|
|
- cell.setCellValue(fieldDescList.get(i));
|
|
|
- cell.setCellType(CellType.STRING);
|
|
|
- cell.setCellStyle(centerStyle);
|
|
|
- }
|
|
|
- /********************************创建内容行***********************************/
|
|
|
- CommonGenerateExcelVo excelFieldDescDTO;
|
|
|
- String valueStr = null;
|
|
|
- Object fileValue;
|
|
|
- Function getFunction;
|
|
|
- DataFormat format = excelWorkBook.createDataFormat();
|
|
|
- // 样式
|
|
|
- XSSFCellStyle commonStyle = centerStyle;
|
|
|
- commonStyle.setDataFormat(format.getFormat("@"));
|
|
|
- XSSFRow valueRow;
|
|
|
- SimpleDateFormat simpleDateFormat;
|
|
|
- Map<String, Object> enumMap;
|
|
|
- for (int j = 0; j < dataList.size(); j++) {
|
|
|
- valueRow = sheet.createRow(j + 1);
|
|
|
- Object data = dataList.get(j);
|
|
|
- boolean isNumber;
|
|
|
- for (int i = 0; i < excelFieldDescDTOList.size(); i++) {
|
|
|
- cell = valueRow.createCell(i);
|
|
|
- isNumber = false;
|
|
|
- excelFieldDescDTO = excelFieldDescDTOList.get(i);
|
|
|
- getFunction = excelFieldDescDTO.getGetOrgNameFunction();
|
|
|
- if (getFunction != null) {
|
|
|
- fileValue = getFunction.apply(data);
|
|
|
- } else {
|
|
|
- fileValue = excelFieldDescDTO.getFileValue();
|
|
|
- }
|
|
|
- String datePattern = excelFieldDescDTO.getDatePattern();
|
|
|
- if (fileValue == null) {
|
|
|
- valueStr = "";
|
|
|
- } else if (fileValue instanceof java.util.Date) {
|
|
|
- simpleDateFormat = new SimpleDateFormat(StringUtils.isBlank(datePattern) ? DATE_PATTERN_YMD : datePattern);
|
|
|
- // 日期格式处理
|
|
|
- valueStr = simpleDateFormat.format(fileValue);
|
|
|
- } else if (excelFieldDescDTO.getCustomDictMap() != null && !excelFieldDescDTO.getCustomDictMap().isEmpty()) {
|
|
|
- // 数据字典处理
|
|
|
- enumMap = excelFieldDescDTO.getCustomDictMap();
|
|
|
- valueStr = enumMap == null ? "" : enumMap.getOrDefault(fileValue.toString(), "").toString();
|
|
|
- } else if (fileValue instanceof java.math.BigDecimal) {
|
|
|
- // 特殊处理 小数位数都是0的情况下 只显示整数部分
|
|
|
- valueStr = ((BigDecimal) fileValue).stripTrailingZeros().toPlainString();
|
|
|
- isNumber = true;
|
|
|
- } else if (fileValue instanceof java.lang.Integer || fileValue instanceof java.lang.Long) {
|
|
|
- valueStr = fileValue.toString();
|
|
|
- isNumber = true;
|
|
|
- } else {
|
|
|
- isNumber = false;
|
|
|
- valueStr = fileValue.toString();
|
|
|
- }
|
|
|
- if (isNumber) {
|
|
|
- cell.setCellValue(Double.parseDouble(valueStr));
|
|
|
- cell.setCellStyle(centerStyle);
|
|
|
- } else {
|
|
|
- cell.setCellStyle(commonStyle);
|
|
|
- cell.setCellValue(valueStr);
|
|
|
- }
|
|
|
- cell.setCellType(isNumber ? CellType.NUMERIC : CellType.STRING);
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
- for (int i = 0; i < fieldDescList.size(); i++) {
|
|
|
- sheet.autoSizeColumn(i, true);
|
|
|
- if (sheet.getColumnWidth(i) * 17 / 10 < MAX_COLUMN_WIDTH) {
|
|
|
- sheet.setColumnWidth(i, Math.max(sheet.getColumnWidth(i) * 17 / 10, 3000));
|
|
|
- } else {
|
|
|
- sheet.setColumnWidth(i, 6000);
|
|
|
- }
|
|
|
- sheet.setDefaultColumnStyle(i, centerStyle);
|
|
|
- }
|
|
|
-
|
|
|
- excelWorkBook.write(outputStream);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("excel导出异常");
|
|
|
- e.printStackTrace();
|
|
|
- throw new RuntimeException("excel导出失败!请联系系统管理员");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static List<CommonGenerateExcelVo> getDictData(List<CommonGenerateExcelVo> excelFieldDescDTOList) {
|
|
|
- if (excelFieldDescDTOList.isEmpty()) {
|
|
|
- return excelFieldDescDTOList;
|
|
|
- }
|
|
|
- List<String> dictTypeList = excelFieldDescDTOList.stream()
|
|
|
- .map(info -> info.getDictType())
|
|
|
- .filter(StringUtils::isNotBlank)
|
|
|
- .distinct()
|
|
|
- .collect(Collectors.toList());
|
|
|
- if (dictTypeList.isEmpty()) {
|
|
|
- return excelFieldDescDTOList;
|
|
|
- }
|
|
|
- List<SysDictDo> sysDictDos = sysDictDao.selectList(new LambdaQueryWrapper<SysDictDo>().eq(BaseDo::getDeleted, "0").in(SysDictDo::getDicttype, dictTypeList));
|
|
|
- excelFieldDescDTOList.forEach(data -> {
|
|
|
- if (StringUtils.isNotBlank(data.getDictType())) {
|
|
|
- data.setCustomDictMap(sysDictDos.stream().filter(dict -> dict.getDicttype().equals(data.getDictType()))
|
|
|
- .collect(Collectors.toMap(SysDictDo::getValue, SysDictDo::getName)));
|
|
|
- }
|
|
|
- });
|
|
|
- return excelFieldDescDTOList;
|
|
|
- }
|
|
|
+// /********************************创建内容行***********************************/
|
|
|
+// CommonGenerateExcelVo excelFieldDescDTO;
|
|
|
+// String valueStr = null;
|
|
|
+// Object fileValue;
|
|
|
+// Function getFunction;
|
|
|
+// DataFormat format = excelWorkBook.createDataFormat();
|
|
|
+// // 样式
|
|
|
+// XSSFCellStyle commonStyle = centerStyle;
|
|
|
+// commonStyle.setDataFormat(format.getFormat("@"));
|
|
|
+// XSSFRow valueRow;
|
|
|
+// SimpleDateFormat simpleDateFormat;
|
|
|
+// Map<String, Object> enumMap;
|
|
|
+// for (int j = 0; j < dataList.size(); j++) {
|
|
|
+// valueRow = sheet.createRow(j + 1);
|
|
|
+// Object data = dataList.get(j);
|
|
|
+// boolean isNumber;
|
|
|
+// for (int i = 0; i < excelFieldDescDTOList.size(); i++) {
|
|
|
+// cell = valueRow.createCell(i);
|
|
|
+// isNumber = false;
|
|
|
+// excelFieldDescDTO = excelFieldDescDTOList.get(i);
|
|
|
+// getFunction = excelFieldDescDTO.getGetOrgNameFunction();
|
|
|
+// if (getFunction != null) {
|
|
|
+// fileValue = getFunction.apply(data);
|
|
|
+// } else {
|
|
|
+// fileValue = excelFieldDescDTO.getFileValue();
|
|
|
+// }
|
|
|
+// String datePattern = excelFieldDescDTO.getDatePattern();
|
|
|
+// if (fileValue == null) {
|
|
|
+// valueStr = "";
|
|
|
+// } else if (fileValue instanceof java.util.Date) {
|
|
|
+// simpleDateFormat = new SimpleDateFormat(StringUtils.isBlank(datePattern) ? DATE_PATTERN_YMD : datePattern);
|
|
|
+// // 日期格式处理
|
|
|
+// valueStr = simpleDateFormat.format(fileValue);
|
|
|
+// } else if (excelFieldDescDTO.getCustomDictMap() != null && !excelFieldDescDTO.getCustomDictMap().isEmpty()) {
|
|
|
+// // 数据字典处理
|
|
|
+// enumMap = excelFieldDescDTO.getCustomDictMap();
|
|
|
+// valueStr = enumMap == null ? "" : enumMap.getOrDefault(fileValue.toString(), "").toString();
|
|
|
+// } else if (fileValue instanceof java.math.BigDecimal) {
|
|
|
+// // 特殊处理 小数位数都是0的情况下 只显示整数部分
|
|
|
+// valueStr = ((BigDecimal) fileValue).stripTrailingZeros().toPlainString();
|
|
|
+// isNumber = true;
|
|
|
+// } else if (fileValue instanceof java.lang.Integer || fileValue instanceof java.lang.Long) {
|
|
|
+// valueStr = fileValue.toString();
|
|
|
+// isNumber = true;
|
|
|
+// } else {
|
|
|
+// isNumber = false;
|
|
|
+// valueStr = fileValue.toString();
|
|
|
+// }
|
|
|
+// if (isNumber) {
|
|
|
+// cell.setCellValue(Double.parseDouble(valueStr));
|
|
|
+// cell.setCellStyle(centerStyle);
|
|
|
+// } else {
|
|
|
+// cell.setCellStyle(commonStyle);
|
|
|
+// cell.setCellValue(valueStr);
|
|
|
+// }
|
|
|
+// cell.setCellType(isNumber ? CellType.NUMERIC : CellType.STRING);
|
|
|
+//
|
|
|
+// }
|
|
|
+// }
|
|
|
+// for (int i = 0; i < fieldDescList.size(); i++) {
|
|
|
+// sheet.autoSizeColumn(i, true);
|
|
|
+// if (sheet.getColumnWidth(i) * 17 / 10 < MAX_COLUMN_WIDTH) {
|
|
|
+// sheet.setColumnWidth(i, Math.max(sheet.getColumnWidth(i) * 17 / 10, 3000));
|
|
|
+// } else {
|
|
|
+// sheet.setColumnWidth(i, 6000);
|
|
|
+// }
|
|
|
+// sheet.setDefaultColumnStyle(i, centerStyle);
|
|
|
+// }
|
|
|
+//
|
|
|
+// excelWorkBook.write(outputStream);
|
|
|
+// } catch (Exception e) {
|
|
|
+// log.error("excel导出异常");
|
|
|
+// e.printStackTrace();
|
|
|
+// throw new RuntimeException("excel导出失败!请联系系统管理员");
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// public static List<CommonGenerateExcelVo> getDictData(List<CommonGenerateExcelVo> excelFieldDescDTOList) {
|
|
|
+// if (excelFieldDescDTOList.isEmpty()) {
|
|
|
+// return excelFieldDescDTOList;
|
|
|
+// }
|
|
|
+// List<String> dictTypeList = excelFieldDescDTOList.stream()
|
|
|
+// .map(info -> info.getDictType())
|
|
|
+// .filter(StringUtils::isNotBlank)
|
|
|
+// .distinct()
|
|
|
+// .collect(Collectors.toList());
|
|
|
+// if (dictTypeList.isEmpty()) {
|
|
|
+// return excelFieldDescDTOList;
|
|
|
+// }
|
|
|
+// List<SysDictDo> sysDictDos = sysDictDao.selectList(new LambdaQueryWrapper<SysDictDo>().eq(BaseDo::getDeleted, "0").in(SysDictDo::getDicttype, dictTypeList));
|
|
|
+// excelFieldDescDTOList.forEach(data -> {
|
|
|
+// if (StringUtils.isNotBlank(data.getDictType())) {
|
|
|
+// data.setCustomDictMap(sysDictDos.stream().filter(dict -> dict.getDicttype().equals(data.getDictType()))
|
|
|
+// .collect(Collectors.toMap(SysDictDo::getValue, SysDictDo::getName)));
|
|
|
+// }
|
|
|
+// });
|
|
|
+// return excelFieldDescDTOList;
|
|
|
+// }
|
|
|
}
|