提交 1796ddf3 authored 作者: taojinlong's avatar taojinlong

fix: 解析excel失败

上级 0c42713f
...@@ -70,7 +70,7 @@ public class ExcelReaderUtil { ...@@ -70,7 +70,7 @@ public class ExcelReaderUtil {
} }
public static void main(String[] args) throws Exception { 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)); ExcelReaderUtil.readExcel(file, new FileInputStream("/Users/taojinlong/Desktop/" + file));
} }
} }
package io.dataease.commons.utils; package io.dataease.commons.utils;
import com.google.gson.Gson;
import io.dataease.datasource.dto.TableFiled; import io.dataease.datasource.dto.TableFiled;
import io.dataease.dto.dataset.ExcelSheetData; import io.dataease.dto.dataset.ExcelSheetData;
import io.dataease.i18n.Translator; import io.dataease.i18n.Translator;
import io.dataease.service.message.MsgAop;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.openxml4j.opc.OPCPackage; 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.ss.usermodel.DataFormatter;
import org.apache.poi.xssf.eventusermodel.XSSFReader; import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.model.SharedStringsTable; import org.apache.poi.xssf.model.SharedStringsTable;
...@@ -23,8 +19,6 @@ import org.xml.sax.helpers.XMLReaderFactory; ...@@ -23,8 +19,6 @@ import org.xml.sax.helpers.XMLReaderFactory;
import java.io.InputStream; import java.io.InputStream;
import java.util.*; import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
* @author y * @author y
...@@ -152,6 +146,7 @@ public class ExcelXlsxReader extends DefaultHandler { ...@@ -152,6 +146,7 @@ public class ExcelXlsxReader extends DefaultHandler {
parser.setContentHandler(this); parser.setContentHandler(this);
XSSFReader.SheetIterator sheets = (XSSFReader.SheetIterator) xssfReader.getSheetsData(); XSSFReader.SheetIterator sheets = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
while (sheets.hasNext()) { //遍历sheet while (sheets.hasNext()) { //遍历sheet
curRow = 1; //标记初始行为第一行 curRow = 1; //标记初始行为第一行
fields.clear(); fields.clear();
data.clear(); data.clear();
...@@ -252,17 +247,14 @@ public class ExcelXlsxReader extends DefaultHandler { ...@@ -252,17 +247,14 @@ public class ExcelXlsxReader extends DefaultHandler {
} else if ("v".equals(name)) { } else if ("v".equals(name)) {
//v => 单元格的值,如果单元格是字符串,则v标签的值为该字符串在SST中的索引 //v => 单元格的值,如果单元格是字符串,则v标签的值为该字符串在SST中的索引
String value = this.getDataValue(lastIndex.trim(), "");//根据索引值获取对应的单元格值 String value = this.getDataValue(lastIndex.trim(), "");//根据索引值获取对应的单元格值
if (preRef == null) { if (preRef == null) {
String regEx="[^0-9]"; preRef = "A" + curRow;
Pattern p = Pattern.compile(regEx); if(!preRef.equalsIgnoreCase(ref)){
Matcher m = p.matcher(ref);
if(curCol < Integer.valueOf(m.replaceAll("").trim()) -1 ){
cellList.add(curCol, ""); cellList.add(curCol, "");
curCol++; curCol++;
} }
preRef = ref;
} }
//补全单元格之间的空单元格 //补全单元格之间的空单元格
if (!"A".equals(preRef.substring(0, 1)) && curRow==1 && preRef.equalsIgnoreCase(ref)) { if (!"A".equals(preRef.substring(0, 1)) && curRow==1 && preRef.equalsIgnoreCase(ref)) {
throw new RuntimeException(Translator.get("i18n_excel_empty_column")); throw new RuntimeException(Translator.get("i18n_excel_empty_column"));
...@@ -292,14 +284,6 @@ public class ExcelXlsxReader extends DefaultHandler { ...@@ -292,14 +284,6 @@ public class ExcelXlsxReader extends DefaultHandler {
if (curRow == 1) { if (curRow == 1) {
maxRef = ref; maxRef = ref;
} }
//补全一行尾部可能缺失的单元格
if (maxRef != null) {
int len = countNullCell(maxRef, ref);
for (int i = 0; i <= len; i++) {
cellList.add(curCol, "");
curCol++;
}
}
if(curRow>1){ if(curRow>1){
List<String> tmp = new ArrayList<>(cellList); List<String> tmp = new ArrayList<>(cellList);
this.getData().add(tmp); this.getData().add(tmp);
...@@ -501,4 +485,4 @@ public class ExcelXlsxReader extends DefaultHandler { ...@@ -501,4 +485,4 @@ public class ExcelXlsxReader extends DefaultHandler {
return str; return str;
} }
} }
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论