From 1796ddf3ebbb6a7bcf1346ccaa10ad58c7de93d5 Mon Sep 17 00:00:00 2001 From: taojinlong Date: Fri, 13 Aug 2021 17:30:15 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E6=9E=90excel=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/utils/ExcelReaderUtil.java | 2 +- .../commons/utils/ExcelXlsxReader.java | 26 ++++--------------- 2 files changed, 6 insertions(+), 22 deletions(-) diff --git a/backend/src/main/java/io/dataease/commons/utils/ExcelReaderUtil.java b/backend/src/main/java/io/dataease/commons/utils/ExcelReaderUtil.java index 2baa422e88..a739345885 100644 --- a/backend/src/main/java/io/dataease/commons/utils/ExcelReaderUtil.java +++ b/backend/src/main/java/io/dataease/commons/utils/ExcelReaderUtil.java @@ -70,7 +70,7 @@ public class ExcelReaderUtil { } public static void main(String[] args) throws Exception { - String file ="全国现有确诊趋势.xlsx"; + String file ="【审批】客户接待07.11-08.10.xlsx"; ExcelReaderUtil.readExcel(file, new FileInputStream("/Users/taojinlong/Desktop/" + file)); } } diff --git a/backend/src/main/java/io/dataease/commons/utils/ExcelXlsxReader.java b/backend/src/main/java/io/dataease/commons/utils/ExcelXlsxReader.java index 8cb2920d69..ebc647baac 100644 --- a/backend/src/main/java/io/dataease/commons/utils/ExcelXlsxReader.java +++ b/backend/src/main/java/io/dataease/commons/utils/ExcelXlsxReader.java @@ -1,13 +1,9 @@ package io.dataease.commons.utils; -import com.google.gson.Gson; import io.dataease.datasource.dto.TableFiled; import io.dataease.dto.dataset.ExcelSheetData; import io.dataease.i18n.Translator; -import io.dataease.service.message.MsgAop; import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.lang3.StringUtils; import org.apache.poi.openxml4j.opc.OPCPackage; -import org.apache.poi.ss.usermodel.BuiltinFormats; import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.xssf.eventusermodel.XSSFReader; import org.apache.poi.xssf.model.SharedStringsTable; @@ -23,8 +19,6 @@ import org.xml.sax.helpers.XMLReaderFactory; import java.io.InputStream; import java.util.*; -import java.util.regex.Matcher; -import java.util.regex.Pattern; /** * @author y @@ -152,6 +146,7 @@ public class ExcelXlsxReader extends DefaultHandler { parser.setContentHandler(this); XSSFReader.SheetIterator sheets = (XSSFReader.SheetIterator) xssfReader.getSheetsData(); while (sheets.hasNext()) { //遍历sheet + curRow = 1; //标记初始行为第一行 fields.clear(); data.clear(); @@ -252,17 +247,14 @@ public class ExcelXlsxReader extends DefaultHandler { } else if ("v".equals(name)) { //v => 单元格的值,如果单元格是字符串,则v标签的值为该字符串在SST中的索引 String value = this.getDataValue(lastIndex.trim(), "");//根据索引值获取对应的单元格值 - if (preRef == null) { - String regEx="[^0-9]"; - Pattern p = Pattern.compile(regEx); - Matcher m = p.matcher(ref); - if(curCol < Integer.valueOf(m.replaceAll("").trim()) -1 ){ + preRef = "A" + curRow; + if(!preRef.equalsIgnoreCase(ref)){ cellList.add(curCol, ""); curCol++; } - preRef = ref; } + //补全单元格之间的空单元格 if (!"A".equals(preRef.substring(0, 1)) && curRow==1 && preRef.equalsIgnoreCase(ref)) { throw new RuntimeException(Translator.get("i18n_excel_empty_column")); @@ -292,14 +284,6 @@ public class ExcelXlsxReader extends DefaultHandler { if (curRow == 1) { maxRef = ref; } - //补全一行尾部可能缺失的单元格 - if (maxRef != null) { - int len = countNullCell(maxRef, ref); - for (int i = 0; i <= len; i++) { - cellList.add(curCol, ""); - curCol++; - } - } if(curRow>1){ List tmp = new ArrayList<>(cellList); this.getData().add(tmp); @@ -501,4 +485,4 @@ public class ExcelXlsxReader extends DefaultHandler { return str; } -} \ No newline at end of file +}