ly 3 роки тому
батько
коміт
bc22922a85

+ 2 - 2
pom.xml

@@ -241,12 +241,12 @@
         <dependency>
             <groupId>org.apache.poi</groupId>
             <artifactId>poi</artifactId>
-            <version>3.16</version>
+            <version>4.1.2</version>
         </dependency>
         <dependency>
             <groupId>org.apache.poi</groupId>
             <artifactId>poi-ooxml</artifactId>
-            <version>3.16</version>
+            <version>4.1.2</version>
         </dependency>
 
         <dependency>

+ 55 - 16
src/main/java/io/renren/common/utils/ExcelUtils.java

@@ -1,5 +1,6 @@
 package io.renren.common.utils;
 
+import org.apache.poi.hssf.usermodel.HSSFCell;
 import org.apache.poi.hssf.usermodel.HSSFDataFormat;
 import org.apache.poi.hssf.usermodel.HSSFDateUtil;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
@@ -12,6 +13,7 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.math.BigDecimal;
 import java.text.DecimalFormat;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
@@ -73,30 +75,67 @@ public class ExcelUtils {
         if (cell == null) {
             return cellValue;
         }
-        //判断数据的类型
-        switch (cell.getCellType()) {
-            case Cell.CELL_TYPE_NUMERIC: //数字
-                cellValue = stringDateProcess(cell);
-                break;
-            case Cell.CELL_TYPE_STRING: //字符串
-                cellValue = String.valueOf(cell.getStringCellValue());
+        System.out.println("Excel格式:"+ cell.getCellType().name());
+        switch (cell.getCellType().name()) {
+            case "STRING":
+                cellValue = cell.getRichStringCellValue().getString();
                 break;
-            case Cell.CELL_TYPE_BOOLEAN: //Boolean
-                cellValue = String.valueOf(cell.getBooleanCellValue());
+            case "NUMERIC":
+                System.out.println("Excel数据样式:"+ cell.getCellStyle().getDataFormatString());
+                if("General".equals(cell.getCellStyle().getDataFormatString())){
+                    cellValue = new BigDecimal(Double.toString(cell.getNumericCellValue())).toPlainString();
+                }else if("m/d/yyyy;@".equals(cell.getCellStyle().getDataFormatString())){
+                    cellValue =  new SimpleDateFormat("yyyy-MM-dd").format(cell.getDateCellValue());
+                }else if("@".equals(cell.getCellStyle().getDataFormatString())){
+                    cellValue = cell.getNumericCellValue() + "";
+                }else{
+                    try {
+                        cellValue =  new SimpleDateFormat("yyyy-MM-dd").format(cell.getDateCellValue());
+                    }catch (Exception e){
+                        cellValue = cell.getNumericCellValue() + "";
+                    }
+                }
                 break;
-            case Cell.CELL_TYPE_FORMULA: //公式
-                cellValue = String.valueOf(cell.getCellFormula());
+            case "FORMULA":
+                try {
+                    cellValue = cell.getStringCellValue();
+                } catch (IllegalStateException e) {
+                    cellValue = String.valueOf(cell.getNumericCellValue());
+                }
                 break;
-            case Cell.CELL_TYPE_BLANK: //空值
+            case "BLANK":
                 cellValue = "";
                 break;
-            case Cell.CELL_TYPE_ERROR: //故障
-                cellValue = "非法字符";
-                break;
             default:
-                cellValue = "未知类型";
+                cellValue = cell.toString();
                 break;
         }
+        //判断数据的类型
+//        switch (cell.getCellType()) {
+//            case Cell.CELL_TYPE_NUMERIC: //数字
+//                cellValue = stringDateProcess(cell);
+//                break;
+//            case Cell.CELL_TYPE_STRING: //字符串
+//                cellValue = String.valueOf(cell.getStringCellValue());
+//                break;
+//            case Cell.CELL_TYPE_BOOLEAN: //Boolean
+//                cellValue = String.valueOf(cell.getBooleanCellValue());
+//                break;
+//            case Cell.CELL_TYPE_FORMULA: //公式
+//                cellValue = String.valueOf(cell.getCellFormula());
+//                break;
+//            case Cell.CELL_TYPE_BLANK: //空值
+//                cellValue = "";
+//                break;
+//            case Cell.CELL_TYPE_ERROR: //故障
+//                cellValue = "非法字符";
+//                break;
+//            default:
+//                cellValue = "未知类型";
+//                break;
+//        }
+        System.out.println("ExcelcellValue:"+ cellValue);
+
         return cellValue;
     }
 

+ 1 - 2
src/main/java/io/renren/modules/common/AspenController.java

@@ -31,8 +31,7 @@ public class AspenController extends AbstractController {
     @Scheduled(cron = "0/15 * * * * ?")
 //    @Scheduled(cron = "0 */15 * * * ?")
     public void getExcelData(){
-//        Workbook workbook = ExcelUtils.getWorkBook("B://GLOBAL/7430-BYC/NANJING/CB/CBP/CBP_C/15 CBP Cracker Share 乙烯装置共享文件夹/Dashboard data/物料.xlsx");
-        Workbook workbook = ExcelUtils.getWorkBook("D://ssyFile/ceshi.xlsx");
+        Workbook workbook = ExcelUtils.getWorkBook("D://ssyFile/bcc.xlsx");
         Sheet sheet = workbook.getSheetAt(0);
         int rowNum = sheet.getPhysicalNumberOfRows();
         TDashboarddataEntity dashboarddataEntity = new TDashboarddataEntity();