Bladeren bron

feat: poi版本5.2.3

zhangkai 3 maanden geleden
bovenliggende
commit
ecad2f8fe9

+ 0 - 4
nationrel-module-weibull-api/pom.xml

@@ -28,22 +28,18 @@
         <dependency>
             <groupId>org.apache.poi</groupId>
             <artifactId>poi</artifactId>
-            <version>3.9</version>
         </dependency>
         <dependency>
             <groupId>org.apache.poi</groupId>
             <artifactId>poi-scratchpad</artifactId>
-            <version>3.9</version>
         </dependency>
         <dependency>
             <groupId>org.apache.poi</groupId>
             <artifactId>poi-ooxml</artifactId>
-            <version>3.9</version>
         </dependency>
         <dependency>
             <groupId>org.apache.poi</groupId>
             <artifactId>poi-excelant</artifactId>
-            <version>3.9</version>
         </dependency>
         <!--  poi End -->
         <dependency>

+ 66 - 53
nationrel-module-weibull-api/src/main/java/com/nationrel/modules/weibull/common/excel/app/impl/XLSApp.java

@@ -9,7 +9,7 @@ import org.apache.poi.hssf.usermodel.HSSFCell;
 import org.apache.poi.hssf.usermodel.HSSFRow;
 import org.apache.poi.hssf.usermodel.HSSFSheet;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellType;
 import org.apache.poi.ss.usermodel.CellValue;
 import org.apache.poi.ss.usermodel.FormulaEvaluator;
 import org.apache.poi.ss.util.CellRangeAddress;
@@ -20,17 +20,19 @@ import java.lang.reflect.Method;
 import java.util.*;
 
 /**
+ * @version V1.0
  * @ClassName XLSApp
  * @Description 2003Excel导入
- * @version V1.0
  */
 public class XLSApp implements ExcelApp {
-    /** 公式求值器 */
+    /**
+     * 公式求值器
+     */
     private FormulaEvaluator evaluator;
     private String cofSheetName = "flagSheet";
 
     @Override
-    public List<Object> readAllRow (ExcelAppBean bean) throws Exception {
+    public List<Object> readAllRow(ExcelAppBean bean) throws Exception {
         List<Object> result = new ArrayList<Object>();
         HSSFWorkbook book = null;
         try {
@@ -60,7 +62,7 @@ public class XLSApp implements ExcelApp {
             int colNum;
             // 读取行, 实际情况:有可能有多行表头,因此开始行号根据配置所得
             for (int rowNum = bean.getStartRow() - 1; rowNum < sheet.getLastRowNum() - tailRow + 1; rowNum++) {
-                obj =  c.newInstance();
+                obj = c.newInstance();
                 row = sheet.getRow(rowNum);
                 if (row == null) {
                     return result;
@@ -101,8 +103,8 @@ public class XLSApp implements ExcelApp {
     /**
      * 判断指定的单元格是否是合并单元格
      *
-     * @param sheet 数据sheet
-     * @param row 行下标
+     * @param sheet  数据sheet
+     * @param row    行下标
      * @param column 列下标
      * @return boolean
      */
@@ -161,7 +163,7 @@ public class XLSApp implements ExcelApp {
             HSSFCell cell = null;
             int idx;
             while (it.hasNext()) {
-                obj =  c.newInstance();
+                obj = c.newInstance();
                 rowNum = it.next();
                 cols = rowCols.get(rowNum);
                 row = sheet.getRow(rowNum - 1);
@@ -172,7 +174,7 @@ public class XLSApp implements ExcelApp {
                 for (int i = 0; i < cols.size(); i++) {
                     idx = cols.get(i);
                     cell = row.getCell(colNums.get(idx) - 1);
-                    if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) {
+                    if (cell == null || cell.getCellType() == CellType.BLANK) {
                         continue;
                     }
                     readCell(c, obj, cell, titles.get(idx));
@@ -188,6 +190,7 @@ public class XLSApp implements ExcelApp {
         }
         return result;
     }
+
     @Override
     public List<Object> readByColumnCof(ExcelAppBean bean) throws Exception {
         List<Object> result = new ArrayList<Object>();
@@ -217,7 +220,7 @@ public class XLSApp implements ExcelApp {
                 boolean readOk = true;
                 // 每一行都从起始列开始
                 col0 = startColNum;
-                obj =  c.newInstance();
+                obj = c.newInstance();
                 row = sheet.getRow(i);
                 if (row == null) {
                     return result;
@@ -231,7 +234,7 @@ public class XLSApp implements ExcelApp {
                         return result;
                     }
                     // 如果第一个要读取的单元格为空,则读取下一行
-                    if (j == 0 && cell.getCellType() == Cell.CELL_TYPE_BLANK) {
+                    if (j == 0 && cell.getCellType() == CellType.BLANK) {
                         readOk = false;
                         break;
                     }
@@ -253,6 +256,7 @@ public class XLSApp implements ExcelApp {
         }
         return result;
     }
+
     @Override
     public void write(Object obj, ExcelAppBean bean) throws Exception {
         OutputStream out = null;
@@ -307,6 +311,7 @@ public class XLSApp implements ExcelApp {
             }
         }
     }
+
     @Override
     public ByteArrayOutputStream download(Object obj, ExcelAppBean bean) throws Exception {
         ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -363,14 +368,15 @@ public class XLSApp implements ExcelApp {
         }
         return out;
     }
+
     /**
-     * @Title: getBook
-     * @Description: 获取Excel实例
      * @param bean bean
      * @return HSSFWorkbook
-     * @exception Exception e
+     * @throws Exception e
+     * @Title: getBook
+     * @Description: 获取Excel实例
      */
-    private HSSFWorkbook getBook (ExcelAppBean bean) throws Exception{
+    private HSSFWorkbook getBook(ExcelAppBean bean) throws Exception {
         InputStream is = null;
         HSSFWorkbook book = null;
         try {
@@ -394,13 +400,14 @@ public class XLSApp implements ExcelApp {
             }
         }
     }
+
     /**
-     * @Title: getSheet
-     * @Description: 获取Sheet, 如果没给定sheetName, 则返回第一个Sheet
      * @param book book
      * @param bean bean
      * @return HSSFSheet
-     * @exception Exception e
+     * @throws Exception e
+     * @Title: getSheet
+     * @Description: 获取Sheet, 如果没给定sheetName, 则返回第一个Sheet
      */
     private HSSFSheet getSheet(HSSFWorkbook book, ExcelAppBean bean) throws Exception {
         String sheetName = bean.getSourceSheetName();
@@ -424,31 +431,32 @@ public class XLSApp implements ExcelApp {
         }
         return sheet;
     }
+
     /**
-     * @Title: getValue
-     * @Description: 根据单元格类型获取值
      * @param cell 单元格对象
      * @return String
+     * @Title: getValue
+     * @Description: 根据单元格类型获取值
      */
     private Object getCellValue(HSSFCell cell) {
         Object value = null;
         try {
             CellValue cellValue = this.evaluator.evaluate(cell);
             switch (cellValue.getCellType()) {
-                case Cell.CELL_TYPE_BOOLEAN:
+                case BOOLEAN:
                     value = cellValue.getBooleanValue();
                     break;
-                case Cell.CELL_TYPE_NUMERIC:
+                case NUMERIC:
                     value = cellValue.getNumberValue();
                     break;
-                case Cell.CELL_TYPE_STRING:
+                case STRING:
                     value = cellValue.getStringValue();
                     break;
-                case Cell.CELL_TYPE_BLANK:
+                case BLANK:
                     break;
-                case Cell.CELL_TYPE_ERROR:
+                case ERROR:
                     break;
-                case Cell.CELL_TYPE_FORMULA:
+                case FORMULA:
                     break;
                 default:
                     value = cellValue.getStringValue();
@@ -458,11 +466,12 @@ public class XLSApp implements ExcelApp {
         }
         return value;
     }
+
     /**
+     * @param cell     单元格对象
+     * @param objValue 要写入的值
      * @Title: getValue
      * @Description: 填写单元格
-     * @param cell 单元格对象
-     * @param objValue 要写入的值
      */
     private void setCellValue(HSSFCell cell, Object objValue) {
         Object orgValue = this.getCellValue(cell);
@@ -486,13 +495,14 @@ public class XLSApp implements ExcelApp {
             }
         }
     }
+
     /**
+     * @param row        第一行
+     * @param columnName 列名
+     * @return Map<String, Integer>
+     * @throws Exception e
      * @Title: getColumnNumber
      * @Description: 根据列名获取列索引号
-     * @param row 第一行
-     * @param columnName 列名
-     * @return Map<String,Integer>
-     * @exception Exception e
      */
     private Integer getColumnNumber(HSSFRow row, String columnName) throws Exception {
         Integer colNum = null;
@@ -515,14 +525,15 @@ public class XLSApp implements ExcelApp {
         }
         return colNum;
     }
+
     /**
+     * @param c    类模板
+     * @param obj  实体对象
+     * @param cell 单元格
+     * @param key  字段
+     * @throws Exception e
      * @Title: setValue
      * @Description: 调用实体对象的get方法获取值,并为单元给设置值
-     * @param c 类模板
-     * @param obj 实体对象
-     * @param cell 单元格
-     * @param key 字段
-     * @exception Exception e
      */
     private void writeCell(Class<?> c, Object obj, HSSFCell cell, String key) throws Exception {
         try {
@@ -545,14 +556,15 @@ public class XLSApp implements ExcelApp {
             throw e;
         }
     }
+
     /**
+     * @param c    类模板
+     * @param obj  实体对象
+     * @param cell 单元格
+     * @param key  void
+     * @throws Exception e
      * @Title: getValue
      * @Description: 获取单元格值,并调用实体对象的Set方法
-     * @param c 类模板
-     * @param obj 实体对象
-     * @param cell 单元格
-     * @param key void
-     * @exception Exception e
      */
     private void readCell(Class<?> c, Object obj, HSSFCell cell, String key) throws Exception {
         try {
@@ -589,14 +601,14 @@ public class XLSApp implements ExcelApp {
     }
 
     /**
+     * @param c     类模板
+     * @param obj   实体对象
+     * @param cell  单元格
+     * @param key   实体对象方法
+     * @param sheet sheet
+     * @param row   void
      * @Title: readMergeCell
      * @Description: 获取单元格值,并调用实体对象的Set方法
-     * @param c 类模板
-     * @param obj 实体对象
-     * @param cell 单元格
-     * @param key 实体对象方法
-     * @param sheet sheet
-     * @param row void
      */
     private void readMergeCell(Class<?> c, Object obj, HSSFCell cell, String key, HSSFSheet sheet, HSSFRow row) throws Exception {
         try {
@@ -625,8 +637,8 @@ public class XLSApp implements ExcelApp {
     /**
      * 获取合并单元格的值
      *
-     * @param sheet 数据sheet
-     * @param row 数据当前行
+     * @param sheet  数据sheet
+     * @param row    数据当前行
      * @param column 当前列
      * @return String
      */
@@ -654,12 +666,13 @@ public class XLSApp implements ExcelApp {
 
         return null;
     }
+
     /**
+     * @param sheet sheet
+     * @param bean  bean
+     * @throws Exception e
      * @Title: validation
      * @Description: 判断是否执行过宏
-     * @param sheet sheet
-     * @param bean bean
-     * @exception Exception e
      */
     private void validation(HSSFSheet sheet, ExcelAppBean bean) throws Exception {
         boolean vaFlag = bean.isValidateFlag();

+ 9 - 9
nationrel-module-weibull-api/src/main/java/com/nationrel/modules/weibull/common/excel/app/impl/XLSXApp.java

@@ -5,7 +5,7 @@ import com.nationrel.modules.weibull.common.excel.bean.ExcelAppBean;
 import com.nationrel.modules.weibull.common.excel.util.ExcelAppUtil;
 import com.nationrel.modules.weibull.util.StringUtil;
 import com.nationrel.modules.weibull.util.ValidateUtil;
-import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellType;
 import org.apache.poi.ss.usermodel.CellValue;
 import org.apache.poi.ss.usermodel.FormulaEvaluator;
 import org.apache.poi.xssf.usermodel.XSSFCell;
@@ -139,7 +139,7 @@ public class XLSXApp implements ExcelApp {
                 for (int i = 0; i < cols.size(); i++) {
                     int idx = cols.get(i);
                     cell = row.getCell(colNums.get(idx) - 1);
-                    if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) {
+                    if (cell == null || cell.getCellType() == CellType.BLANK) {
                         continue;
                     }
                     readCell(c, obj, cell, titles.get(idx));
@@ -201,7 +201,7 @@ public class XLSXApp implements ExcelApp {
                         return result;
                     }*/
                     // 如果第一个要读取的单元格为空,则读取下一行
-                    if (j == 0 && cell.getCellType() == Cell.CELL_TYPE_BLANK) {
+                    if (j == 0 && cell.getCellType() == CellType.BLANK) {
                         readOk = false;
                         break;
                     }
@@ -411,20 +411,20 @@ public class XLSXApp implements ExcelApp {
         try {
             CellValue cellValue = this.evaluator.evaluate(cell);
             switch (cellValue.getCellType()) {
-                case Cell.CELL_TYPE_BOOLEAN:
+                case BOOLEAN:
                     value = cellValue.getBooleanValue();
                     break;
-                case Cell.CELL_TYPE_NUMERIC:
+                case NUMERIC:
                     value = cellValue.getNumberValue();
                     break;
-                case Cell.CELL_TYPE_STRING:
+                case STRING:
                     value = cellValue.getStringValue();
                     break;
-                case Cell.CELL_TYPE_BLANK:
+                case BLANK:
                     break;
-                case Cell.CELL_TYPE_ERROR:
+                case ERROR:
                     break;
-                case Cell.CELL_TYPE_FORMULA:
+                case FORMULA:
                     break;
                 default:
                     value = cellValue.getStringValue();

+ 6 - 6
nationrel-module-weibull-api/src/main/java/com/nationrel/modules/weibull/util/ExportExcelUtil.java

@@ -6,7 +6,7 @@ import com.nationrel.modules.weibull.general.util.common.ValidateUtils;
 import jakarta.servlet.http.HttpServletResponse;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.poi.hssf.util.HSSFColor;
-import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.xssf.usermodel.*;
 
 import java.io.OutputStream;
@@ -60,10 +60,10 @@ public class ExportExcelUtil {
             //创建单元格,并设置值表头 设置表头居中
             XSSFCellStyle titleCellStyle = wb.createCellStyle();
             XSSFCellStyle cellStyle = wb.createCellStyle();
-            cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
-            cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
+            cellStyle.setAlignment(HorizontalAlignment.CENTER);
+            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
             // 设置:表头Cell样式
-            writeCellStyle(titleCellStyle, XSSFCellStyle.SOLID_FOREGROUND, HSSFColor.LIME.index, XSSFCellStyle.ALIGN_CENTER, XSSFCellStyle.VERTICAL_CENTER);
+            writeCellStyle(titleCellStyle, FillPatternType.SOLID_FOREGROUND, IndexedColors.LIME.index, HorizontalAlignment.CENTER, VerticalAlignment.CENTER);
             // 填充工作表
             fillSheet(sheet, list, fieldMap, titleCellStyle, cellStyle);
 
@@ -244,8 +244,8 @@ public class ExportExcelUtil {
      * @param alignment         设置单元格的水平对齐类型
      * @param verticalAlignment 设置单元格的垂直对齐类型
      */
-    public static void writeCellStyle(CellStyle cellStyle, short pattern, short backgroundColor,
-                                      short alignment, short verticalAlignment) {
+    public static void writeCellStyle(CellStyle cellStyle, FillPatternType pattern, short backgroundColor,
+                                      HorizontalAlignment alignment, VerticalAlignment verticalAlignment) {
         cellStyle.setFillPattern(pattern);
         cellStyle.setFillForegroundColor(backgroundColor);
         cellStyle.setAlignment(alignment);

+ 4 - 5
nationrel-module-weibull-api/src/main/java/com/nationrel/modules/weibull/util/ImportExcelUtil.java

@@ -5,7 +5,6 @@ import com.nationrel.modules.weibull.general.domain.enums.common.EwbConstants;
 import com.nationrel.modules.weibull.general.domain.enums.reliaqube.ReliaqubeParamDataCheckEnum;
 import com.nationrel.modules.weibull.general.exception.DescribeException;
 import org.apache.commons.lang3.StringUtils;
-import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.xssf.usermodel.XSSFCell;
 import org.apache.poi.xssf.usermodel.XSSFRow;
 import org.apache.poi.xssf.usermodel.XSSFSheet;
@@ -92,10 +91,10 @@ public class ImportExcelUtil {
         DecimalFormat df2 = new DecimalFormat("0.00");  //格式化数字
 
         switch (cell.getCellType()) {
-            case Cell.CELL_TYPE_STRING:
+            case STRING:
                 value = cell.getRichStringCellValue().getString();
                 break;
-            case Cell.CELL_TYPE_NUMERIC:
+            case NUMERIC:
                 if ("General".equals(cell.getCellStyle().getDataFormatString())) {
                     value = df.format((int) cell.getNumericCellValue());
                 } else if ("m/d/yy".equals(cell.getCellStyle().getDataFormatString())) {
@@ -104,7 +103,7 @@ public class ImportExcelUtil {
                     value = df2.format((int) cell.getNumericCellValue());
                 }
                 break;
-            case Cell.CELL_TYPE_BOOLEAN:
+            case BOOLEAN:
                 value = cell.getBooleanCellValue() + StringUtils.EMPTY;
                 break;
 //            case Cell.CELL_TYPE_BLANK:
@@ -144,4 +143,4 @@ public class ImportExcelUtil {
         }
         return null;
     }
-}
+}

+ 6 - 7
nationrel-module-weibull-api/src/main/java/com/nationrel/modules/weibull/util/NevadaExportExcelUtil.java

@@ -3,8 +3,7 @@ package com.nationrel.modules.weibull.util;
 import com.nationrel.modules.weibull.general.domain.enums.common.EwbConstants;
 import jakarta.servlet.http.HttpServletResponse;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.poi.hssf.util.HSSFColor;
-import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.xssf.usermodel.*;
 import org.springframework.util.CollectionUtils;
 
@@ -65,10 +64,10 @@ public class NevadaExportExcelUtil {
                     //创建单元格,并设置值表头 设置表头居中
                     XSSFCellStyle titleCellStyle = wb.createCellStyle();
                     XSSFCellStyle cellStyle = wb.createCellStyle();
-                    cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
-                    cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
+                    cellStyle.setAlignment(HorizontalAlignment.CENTER);
+                    cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
                     // 设置:表头Cell样式
-                    writeCellStyle(titleCellStyle, XSSFCellStyle.SOLID_FOREGROUND, HSSFColor.LIME.index, XSSFCellStyle.ALIGN_CENTER, XSSFCellStyle.VERTICAL_CENTER);
+                    writeCellStyle(titleCellStyle, FillPatternType.SOLID_FOREGROUND, IndexedColors.LIME.index, HorizontalAlignment.CENTER, VerticalAlignment.CENTER);
                     // 填充工作表
                     fillSheet(sheet, list, colHeader, titleCellStyle, cellStyle);
                 }
@@ -228,8 +227,8 @@ public class NevadaExportExcelUtil {
      * @param alignment         设置单元格的水平对齐类型
      * @param verticalAlignment 设置单元格的垂直对齐类型
      */
-    public static void writeCellStyle(CellStyle cellStyle, short pattern, short backgroundColor,
-                                      short alignment, short verticalAlignment) {
+    public static void writeCellStyle(CellStyle cellStyle, FillPatternType pattern, short backgroundColor,
+                                      HorizontalAlignment alignment, VerticalAlignment verticalAlignment) {
         cellStyle.setFillPattern(pattern);
         cellStyle.setFillForegroundColor(backgroundColor);
         cellStyle.setAlignment(alignment);