ExcelUtil.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package com.huaxu.util;
  2. import com.alibaba.excel.EasyExcel;
  3. import com.alibaba.excel.enums.CellDataTypeEnum;
  4. import com.alibaba.excel.metadata.CellData;
  5. import com.alibaba.excel.metadata.Head;
  6. import com.alibaba.excel.util.CollectionUtils;
  7. import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
  8. import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
  9. import com.alibaba.excel.write.metadata.style.WriteCellStyle;
  10. import com.alibaba.excel.write.metadata.style.WriteFont;
  11. import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
  12. import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
  13. import com.huaxu.common.ToolUtil;
  14. import org.apache.commons.lang.time.DateFormatUtils;
  15. import org.apache.poi.ss.usermodel.*;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.util.Date;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Map;
  22. /**
  23. * @ClassName ExcelUtil
  24. * @Description: Excel导出通用工具类
  25. * @Author :lihui
  26. * @Date 2021/5/17
  27. * @Version V1.0
  28. **/
  29. public class ExcelUtil {
  30. /**
  31. * 仅仅输出明细数据到excel文件, 增加自定义的头部
  32. */
  33. public static String writeSimpleExcelWithHeader(String path, String excelName, List<List<String>> head, List list) {
  34. String filePath = "";
  35. String fileName = DateFormatUtils.format(new Date(), "yyyy/MM/dd") + File.separator;
  36. fileName = fileName + excelName + System.currentTimeMillis() + ".xlsx";
  37. try {
  38. getAbsoluteFile(path, fileName);
  39. // 这里 需要指定写用哪个class去写,指定模板名称及数据
  40. EasyExcel.write(path + fileName).head(head).registerWriteHandler(new CustomizeColumnWidth())
  41. .registerWriteHandler(getStyleStrategy()).sheet("sheet").doWrite(list);
  42. filePath = getPathFileName(path, fileName);
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. }
  46. return filePath;
  47. }
  48. private static final String getPathFileName(String uploadDir, String fileName) throws IOException {
  49. String pathFileName = uploadDir + "/" + fileName;
  50. return ToolUtil.path(pathFileName);
  51. }
  52. private static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException {
  53. File desc = new File(uploadDir + File.separator + fileName);
  54. if (!desc.getParentFile().exists()) {
  55. desc.getParentFile().mkdirs();
  56. }
  57. return desc;
  58. }
  59. private static HorizontalCellStyleStrategy getStyleStrategy() {
  60. // 头的策略
  61. WriteCellStyle headWriteCellStyle = new WriteCellStyle();
  62. // 设置对齐
  63. headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
  64. // 背景色, 设置为白色,也是默认颜色
  65. headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
  66. // 字体
  67. WriteFont headWriteFont = new WriteFont();
  68. headWriteFont.setFontHeightInPoints((short) 11);
  69. headWriteCellStyle.setWriteFont(headWriteFont);
  70. return new HorizontalCellStyleStrategy(headWriteCellStyle, new WriteCellStyle());
  71. }
  72. /**
  73. * 自定义头部的 列的宽度设置 策略. .
  74. */
  75. static class CustomizeColumnWidth extends AbstractColumnWidthStyleStrategy {
  76. private Map<Integer, Map<Integer, Integer>> CACHE = new HashMap(8);
  77. @Override
  78. protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<CellData> list, Cell cell, Head head, Integer integer, Boolean isHead) {
  79. boolean needSetWidth = isHead || !CollectionUtils.isEmpty(list);
  80. if (needSetWidth) {
  81. Map<Integer, Integer> maxColumnWidthMap = CACHE.get(writeSheetHolder.getSheetNo());
  82. if (maxColumnWidthMap == null) {
  83. maxColumnWidthMap = new HashMap(16);
  84. CACHE.put(writeSheetHolder.getSheetNo(), maxColumnWidthMap);
  85. }
  86. Integer columnWidth = this.dataLength(list, cell, isHead);
  87. if (columnWidth >= 0) {
  88. if (columnWidth > 255) {
  89. columnWidth = 255;
  90. }
  91. Integer maxColumnWidth = (Integer)((Map)maxColumnWidthMap).get(cell.getColumnIndex());
  92. if (maxColumnWidth == null || columnWidth > maxColumnWidth) {
  93. ((Map)maxColumnWidthMap).put(cell.getColumnIndex(), columnWidth);
  94. writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), columnWidth * 256);
  95. }
  96. }
  97. }
  98. }
  99. private Integer dataLength(List<CellData> cellDataList, Cell cell, Boolean isHead) {
  100. if (isHead) {
  101. return cell.getStringCellValue().getBytes().length;
  102. } else {
  103. CellData cellData = (CellData)cellDataList.get(0);
  104. CellDataTypeEnum type = cellData.getType();
  105. if (type == null) {
  106. return -1;
  107. } else {
  108. switch(type) {
  109. case STRING:
  110. return cellData.getStringValue().getBytes().length;
  111. case BOOLEAN:
  112. return cellData.getBooleanValue().toString().getBytes().length;
  113. case NUMBER:
  114. return cellData.getNumberValue().toString().getBytes().length;
  115. default:
  116. return -1;
  117. }
  118. }
  119. }
  120. }
  121. @Override
  122. public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {
  123. // 设置行高测试
  124. short height = 600;
  125. row.setHeight(height);
  126. }
  127. }
  128. }