Browse Source

BUG修复

zhouy 2 weeks ago
parent
commit
2093bf042f

+ 4 - 1
src/main/java/com/uas/eis/core/support/HeaderRowHandler.java

@@ -17,6 +17,7 @@ public class HeaderRowHandler extends DefaultHandler {
     private int currentRowIndex;
     // 标记:是否已经读完表头,停止解析
     private boolean headerReadFinished = false;
+    private String currentCellType;
 
     public HeaderRowHandler(int targetRowNum, SharedStrings sharedStrings, List<String> headerResultList) {
         this.targetRowNum = targetRowNum;
@@ -46,6 +47,7 @@ public class HeaderRowHandler extends DefaultHandler {
         if ("c".equals(qName) && currentRowIndex == targetRowNum) {
             captureCellFlag = true;
             cellBuffer = "";
+            currentCellType = attributes.getValue("t");
         }
     }
 
@@ -63,8 +65,9 @@ public class HeaderRowHandler extends DefaultHandler {
             return;
         }
         if ("c".equals(qName) && currentRowIndex == targetRowNum) {
-            String cellVal = PoiSaxUtil.parseCellValue(cellBuffer, sharedStrings);
+            String cellVal = PoiSaxUtil.parseCellValue(cellBuffer, currentCellType, sharedStrings);
             headerResultList.add(cellVal);
+            currentCellType = null;
             captureCellFlag = false;
         }
     }

+ 10 - 2
src/main/java/com/uas/eis/core/support/SaxDataBatchHandler.java

@@ -34,6 +34,7 @@ public class SaxDataBatchHandler extends DefaultHandler {
     private final List<String> currentRowCells = new ArrayList<>();
     private String tempCellText;
     private boolean readCellContent;
+    private String currentCellType;
 
     public SaxDataBatchHandler(JdbcTemplate jdbcTemplate, String waferId, String tableName, String dbLinkSuffix, String dynamicItemColumns,
                                int maxCellSize,
@@ -71,6 +72,7 @@ public class SaxDataBatchHandler extends DefaultHandler {
         if ("c".equals(qName) && currentRowNum >= dataStartRow) {
             tempCellText = "";
             readCellContent = true;
+            currentCellType = attributes.getValue("t");
         }
     }
 
@@ -85,9 +87,14 @@ public class SaxDataBatchHandler extends DefaultHandler {
     public void endElement(String uri, String localName, String qName) {
         // 单元格结束,收集值
         if ("c".equals(qName) && currentRowNum >= dataStartRow) {
-            String cellVal = PoiSaxUtil.parseCellValue(tempCellText, sharedStrings);
+            String cellVal = PoiSaxUtil.parseCellValue(tempCellText, currentCellType,sharedStrings);
             currentRowCells.add(cellVal);
+            /*if(currentRowNum ==24) {
+                System.out.println("aaa:"+tempCellText+": currentCellType:"+currentCellType);
+                System.out.println("bbb:"+cellVal);
+            }*/
             readCellContent = false;
+            currentCellType = null;
         }
 
         // 单行解析完成,拼接INSERT语句
@@ -109,7 +116,8 @@ public class SaxDataBatchHandler extends DefaultHandler {
             }
 
             batchSqlBuffer.append("INSERT INTO ")
-                    .append(tableName).append(dbLinkSuffix)
+                    .append(tableName)
+                    .append(dbLinkSuffix)
                     .append(" (WAFER_ID,DETNO_,DATATYPE_,FLAG_,").append(dynamicItemColumns).append(") VALUES ('")
                     .append(waferId).append("',")
                     .append(detNo).append(",")

+ 2 - 1
src/main/java/com/uas/eis/serviceImpl/FileParse2ServiceImpl.java

@@ -1,5 +1,6 @@
 package com.uas.eis.serviceImpl;
 
+import com.uas.eis.core.config.SpObserver;
 import com.uas.eis.core.support.HeaderRowHandler;
 import com.uas.eis.core.support.SaxDataBatchHandler;
 import com.uas.eis.dao.BaseDao;
@@ -7,6 +8,7 @@ import com.uas.eis.entity.DataChipTestLog;
 import com.uas.eis.utils.FTPUtil;
 import org.apache.commons.net.ftp.FTPClient;
 import org.apache.poi.openxml4j.opc.OPCPackage;
+import org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable;
 import org.apache.poi.xssf.eventusermodel.XSSFReader;
 import org.apache.poi.xssf.model.SharedStrings;
 import org.apache.poi.xssf.model.StylesTable;
@@ -102,7 +104,6 @@ public class FileParse2ServiceImpl {
                     sharedStrings
             );
             dataHandler.parseSheet(xssfReader, xmlReader, stylesTable);
-
             // 4. 后置业务SQL:更新文件状态、归档、日志
             StringBuilder postSqlBlock = new StringBuilder(1024);
             postSqlBlock.append("BEGIN ");

+ 30 - 12
src/main/java/com/uas/eis/utils/PoiSaxUtil.java

@@ -8,21 +8,39 @@ public class PoiSaxUtil {
     /**
      * 解析单元格值,处理共享字符串索引
      */
-    public static String parseCellValue(String raw, SharedStrings sst) {
-        if (raw == null ||raw.isEmpty()) {
+    public static String parseCellValue(String raw, String cellType, SharedStrings sst) {
+        if (raw == null || raw.isEmpty()) {
             return "";
         }
-        // 非数字直接返回文本
-        if (!raw.matches("\\d+")) {
-            return raw.trim();
+        
+        if ("s".equals(cellType)) {
+            if (!raw.matches("\\d+")) {
+                return raw;
+            }
+            int idx;
+            try {
+                idx = Integer.parseInt(raw);
+            } catch (NumberFormatException e) {
+                return raw;
+            }
+            // 索引越界保护
+            if (idx < 0 || idx >= sst.getCount()) {
+                return "";
+            }
+            RichTextString richText = sst.getItemAt(idx);
+            return richText.getString();
         }
-        int idx;
-        try {
-            idx = Integer.parseInt(raw);
-        } catch (NumberFormatException e) {
-            return raw.trim();
+
+        // 布尔类型 0=false 1=true
+        if ("b".equals(cellType)) {
+            return "1".equals(raw) ? "true" : "false";
+        }
+
+        if ("str".equals(cellType)) {
+            return raw;
         }
-        RichTextString richText = sst.getItemAt(idx);
-        return richText.getString();
+
+        // 数字类型 / 无类型:直接返回原值,绝不查表
+        return raw;
     }
 }