feat: Excel空单元格不参与类型判断

This commit is contained in:
taojinlong 2022-11-22 14:00:11 +08:00
parent dedf013830
commit 98acbb10d3
2 changed files with 87 additions and 80 deletions

View File

@ -125,12 +125,11 @@ public class ExcelXlsReader implements HSSFListener {
return data; return data;
} }
public void setData(List<List<String>> data) { public void setData(List<List<String>> data) {
this.data = data; this.data = data;
} }
/** /**
* 遍历excel下所有的sheet * 遍历excel下所有的sheet
* *
@ -212,8 +211,8 @@ public class ExcelXlsReader implements HSSFListener {
thisColumn = frec.getColumn(); thisColumn = frec.getColumn();
thisStr = String.valueOf(frec.getValue()); thisStr = String.valueOf(frec.getValue());
String fieldType = checkType(thisStr, thisColumn); String fieldType = checkType(thisStr, thisColumn);
if(fieldType.equalsIgnoreCase("LONG") && thisStr.endsWith(".0")){ if (fieldType != null && fieldType.equalsIgnoreCase("LONG") && thisStr.endsWith(".0")) {
thisStr = thisStr.substring(0, thisStr.length() -2); thisStr = thisStr.substring(0, thisStr.length() - 2);
} }
cellList.add(thisColumn, thisStr); cellList.add(thisColumn, thisStr);
checkRowIsNull(thisStr); //如果里面某个单元格含有值则标识该行不为空行 checkRowIsNull(thisStr); //如果里面某个单元格含有值则标识该行不为空行
@ -268,9 +267,9 @@ public class ExcelXlsReader implements HSSFListener {
value = value.equals("") ? "" : value; value = value.equals("") ? "" : value;
//向容器加入列值 //向容器加入列值
cellList.add(thisColumn, value); cellList.add(thisColumn, value);
if(formatIndex == 59 || formatIndex== 14){ if (formatIndex == 59 || formatIndex == 14) {
totalSheets.get(totalSheets.size() -1).getFields().get(thisColumn).setFieldType("DATETIME"); totalSheets.get(totalSheets.size() - 1).getFields().get(thisColumn).setFieldType("DATETIME");
}else { } else {
checkType(value, thisColumn); checkType(value, thisColumn);
} }
checkRowIsNull(value); //如果里面某个单元格含有值则标识该行不为空行 checkRowIsNull(value); //如果里面某个单元格含有值则标识该行不为空行
@ -308,7 +307,7 @@ public class ExcelXlsReader implements HSSFListener {
} }
lastColumnNumber = -1; lastColumnNumber = -1;
if(!totalSheets.stream().map(ExcelSheetData::getExcelLabel).collect(Collectors.toList()).contains(sheetName)){ if (!totalSheets.stream().map(ExcelSheetData::getExcelLabel).collect(Collectors.toList()).contains(sheetName)) {
ExcelSheetData excelSheetData = new ExcelSheetData(); ExcelSheetData excelSheetData = new ExcelSheetData();
excelSheetData.setExcelLabel(sheetName); excelSheetData.setExcelLabel(sheetName);
excelSheetData.setData(new ArrayList<>()); excelSheetData.setData(new ArrayList<>());
@ -316,7 +315,7 @@ public class ExcelXlsReader implements HSSFListener {
totalSheets.add(excelSheetData); totalSheets.add(excelSheetData);
} }
if(curRow == 0){ if (curRow == 0) {
for (String s : cellList) { for (String s : cellList) {
TableField tableField = new TableField(); TableField tableField = new TableField();
tableField.setFieldType("TEXT"); tableField.setFieldType("TEXT");
@ -324,13 +323,13 @@ public class ExcelXlsReader implements HSSFListener {
tableField.setFieldName(s); tableField.setFieldName(s);
tableField.setRemarks(s); tableField.setRemarks(s);
this.fields.add(tableField); this.fields.add(tableField);
totalSheets.get(totalSheets.size() -1).getFields().add(tableField); totalSheets.get(totalSheets.size() - 1).getFields().add(tableField);
} }
} }
if (flag && curRow != 0) { //该行不为空行且该行不是第一行发送第一行为列名不需要 if (flag && curRow != 0) { //该行不为空行且该行不是第一行发送第一行为列名不需要
if(!totalSheets.stream().map(ExcelSheetData::getExcelLabel).collect(Collectors.toList()).contains(sheetName)){ if (!totalSheets.stream().map(ExcelSheetData::getExcelLabel).collect(Collectors.toList()).contains(sheetName)) {
ExcelSheetData excelSheetData = new ExcelSheetData(); ExcelSheetData excelSheetData = new ExcelSheetData();
excelSheetData.setData(new ArrayList<>(data)); excelSheetData.setData(new ArrayList<>(data));
excelSheetData.setExcelLabel(sheetName); excelSheetData.setExcelLabel(sheetName);
@ -339,13 +338,13 @@ public class ExcelXlsReader implements HSSFListener {
excelSheetData.getData().add(tmp); excelSheetData.getData().add(tmp);
totalRows++; totalRows++;
totalSheets.add(excelSheetData); totalSheets.add(excelSheetData);
}else { } else {
List<String> tmp = new ArrayList<>(cellList); List<String> tmp = new ArrayList<>(cellList);
if(obtainedNum != null && totalSheets.stream().filter(s->s.getExcelLabel().equalsIgnoreCase(sheetName)).collect(Collectors.toList()).get(0).getData().size() < obtainedNum){ if (obtainedNum != null && totalSheets.stream().filter(s -> s.getExcelLabel().equalsIgnoreCase(sheetName)).collect(Collectors.toList()).get(0).getData().size() < obtainedNum) {
totalSheets.stream().filter(s->s.getExcelLabel().equalsIgnoreCase(sheetName)).collect(Collectors.toList()).get(0).getData().add(tmp); totalSheets.stream().filter(s -> s.getExcelLabel().equalsIgnoreCase(sheetName)).collect(Collectors.toList()).get(0).getData().add(tmp);
} }
if(obtainedNum == null){ if (obtainedNum == null) {
totalSheets.stream().filter(s->s.getExcelLabel().equalsIgnoreCase(sheetName)).collect(Collectors.toList()).get(0).getData().add(tmp); totalSheets.stream().filter(s -> s.getExcelLabel().equalsIgnoreCase(sheetName)).collect(Collectors.toList()).get(0).getData().add(tmp);
} }
totalRows++; totalRows++;
} }
@ -369,7 +368,7 @@ public class ExcelXlsReader implements HSSFListener {
} }
private String checkType(String str, int thisColumn){ private String checkType(String str, int thisColumn) {
String type = null; String type = null;
try { try {
double d = Double.valueOf(str); double d = Double.valueOf(str);
@ -382,22 +381,20 @@ public class ExcelXlsReader implements HSSFListener {
type = "DOUBLE"; type = "DOUBLE";
} }
} catch (Exception e) { } catch (Exception e) {
type = "TEXT";
} }
}catch (Exception e){ } catch (Exception e) {
type = "TEXT";
} }
if(curRow==1){ if (curRow == 1) {
totalSheets.get(totalSheets.size() -1).getFields().get(thisColumn).setFieldType(type); totalSheets.get(totalSheets.size() - 1).getFields().get(thisColumn).setFieldType(type == null ? "TEXT" : type);
} }
if(curRow > 1) { if (curRow > 1 && type != null) {
String oldType = totalSheets.get(totalSheets.size() -1).getFields().get(thisColumn).getFieldType(); String oldType = totalSheets.get(totalSheets.size() - 1).getFields().get(thisColumn).getFieldType();
if(type.equalsIgnoreCase("TEXT")){ if (type.equalsIgnoreCase("TEXT")) {
totalSheets.get(totalSheets.size() -1).getFields().get(thisColumn).setFieldType(type); totalSheets.get(totalSheets.size() - 1).getFields().get(thisColumn).setFieldType(type);
} }
if(type.equalsIgnoreCase("DOUBLE") && oldType.equalsIgnoreCase("LONG")){ if (type.equalsIgnoreCase("DOUBLE") && oldType.equalsIgnoreCase("LONG")) {
totalSheets.get(totalSheets.size() -1).getFields().get(thisColumn).setFieldType(type); totalSheets.get(totalSheets.size() - 1).getFields().get(thisColumn).setFieldType(type);
} }
} }
return type; return type;

View File

@ -1,4 +1,5 @@
package io.dataease.commons.utils; package io.dataease.commons.utils;
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.plugins.common.dto.datasource.TableField; import io.dataease.plugins.common.dto.datasource.TableField;
@ -35,7 +36,8 @@ public class ExcelXlsxReader extends DefaultHandler {
/** /**
* 自定义获取表格某些信 * 自定义获取表格某些信
*/ */
public Map map = new TreeMap<String,String>(); public Map map = new TreeMap<String, String>();
/** /**
* 单元格中的数据可能的数据类型 * 单元格中的数据可能的数据类型
*/ */
@ -56,7 +58,7 @@ public class ExcelXlsxReader extends DefaultHandler {
/** /**
* 总行数 * 总行数
*/ */
private int totalRows=0; private int totalRows = 0;
/** /**
* 一行内cell集合 * 一行内cell集合
@ -101,7 +103,6 @@ public class ExcelXlsxReader extends DefaultHandler {
private String formatString; private String formatString;
//定义前一个元素和当前元素的位置用来计算其中空的单元格数量如A6和A8等 //定义前一个元素和当前元素的位置用来计算其中空的单元格数量如A6和A8等
private String preRef = null, ref = null; private String preRef = null, ref = null;
@ -143,7 +144,7 @@ public class ExcelXlsxReader extends DefaultHandler {
return data; return data;
} }
public void setData(List<List<String>> data) { public void setData(List<List<String>> data) {
this.data = data; this.data = data;
} }
@ -190,11 +191,11 @@ public class ExcelXlsxReader extends DefaultHandler {
*/ */
@Override @Override
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException { public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
if(this.obtainedNum !=null && curRow>this.obtainedNum){ if (this.obtainedNum != null && curRow > this.obtainedNum) {
return; return;
} }
if(name.equalsIgnoreCase("mergeCell")){ if (name.equalsIgnoreCase("mergeCell")) {
throw new RuntimeException(Translator.get("i18n_excel_have_merge_region")); throw new RuntimeException(Translator.get("i18n_excel_have_merge_region"));
} }
//c => 单元格 //c => 单元格
@ -221,6 +222,7 @@ public class ExcelXlsxReader extends DefaultHandler {
* 得到单元格对应的索引值或是内容值 * 得到单元格对应的索引值或是内容值
* 如果单元格类型是字符串INLINESTR数字日期lastIndex则是索引值 * 如果单元格类型是字符串INLINESTR数字日期lastIndex则是索引值
* 如果单元格类型是布尔值错误公式lastIndex则是内容值 * 如果单元格类型是布尔值错误公式lastIndex则是内容值
*
* @param ch * @param ch
* @param start * @param start
* @param length * @param length
@ -228,7 +230,7 @@ public class ExcelXlsxReader extends DefaultHandler {
*/ */
@Override @Override
public void characters(char[] ch, int start, int length) throws SAXException { public void characters(char[] ch, int start, int length) throws SAXException {
if(this.obtainedNum !=null && curRow>this.obtainedNum){ if (this.obtainedNum != null && curRow > this.obtainedNum) {
return; return;
} }
lastIndex += new String(ch, start, length); lastIndex += new String(ch, start, length);
@ -244,14 +246,14 @@ public class ExcelXlsxReader extends DefaultHandler {
*/ */
@Override @Override
public void endElement(String uri, String localName, String name) throws SAXException { public void endElement(String uri, String localName, String name) throws SAXException {
if(this.obtainedNum !=null && curRow>this.obtainedNum){ if (this.obtainedNum != null && curRow > this.obtainedNum) {
return; return;
} }
//t元素也包含字符串 //t元素也包含字符串
if (isTElement) { //这个程序没经过 if (isTElement) { //这个程序没经过
//将单元格内容加入rowlist中在这之前先去掉字符串前后的空白符 //将单元格内容加入rowlist中在这之前先去掉字符串前后的空白符
String value = lastIndex.trim(); String value = lastIndex.trim();
if(curRow==1){ if (curRow == 1) {
TableField tableField = new TableField(); TableField tableField = new TableField();
tableField.setFieldType("TEXT"); tableField.setFieldType("TEXT");
tableField.setFieldSize(65533); tableField.setFieldSize(65533);
@ -271,21 +273,21 @@ public class ExcelXlsxReader extends DefaultHandler {
String value = this.getDataValue(lastIndex.trim(), "");//根据索引值获取对应的单元格值 String value = this.getDataValue(lastIndex.trim(), "");//根据索引值获取对应的单元格值
if (preRef == null) { if (preRef == null) {
preRef = "A" + curRow; preRef = "A" + curRow;
if(!preRef.equalsIgnoreCase(ref)){ if (!preRef.equalsIgnoreCase(ref)) {
cellList.add(curCol, ""); cellList.add(curCol, "");
curCol++; curCol++;
} }
} }
//补全单元格之间的空单元格 //补全单元格之间的空单元格
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"));
}else if (!ref.equals(preRef)) { } else if (!ref.equals(preRef)) {
int len = countNullCell(ref, preRef); int len = countNullCell(ref, preRef);
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
if(curCol < this.fields.size()){ if (curCol < this.fields.size()) {
cellList.add(curCol, ""); cellList.add(curCol, "");
if(curRow==1){ if (curRow == 1) {
addField("", curCol); addField("", curCol);
} }
curCol++; curCol++;
@ -293,7 +295,7 @@ public class ExcelXlsxReader extends DefaultHandler {
} }
} }
if(curCol < this.fields.size()){ if (curCol < this.fields.size()) {
cellList.add(curCol, value); cellList.add(curCol, value);
} }
curCol++; curCol++;
@ -309,8 +311,8 @@ public class ExcelXlsxReader extends DefaultHandler {
if (curRow == 1) { if (curRow == 1) {
maxRef = ref; maxRef = ref;
} }
if(curRow>1){ if (curRow > 1) {
for (int i=cellList.size();i<this.fields.size();i++){ for (int i = cellList.size(); i < this.fields.size(); i++) {
cellList.add(""); cellList.add("");
} }
List<String> tmp = new ArrayList<>(cellList); List<String> tmp = new ArrayList<>(cellList);
@ -322,7 +324,7 @@ public class ExcelXlsxReader extends DefaultHandler {
curCol = 0; curCol = 0;
preRef = null; preRef = null;
ref = null; ref = null;
flag=false; flag = false;
} }
} }
} }
@ -357,9 +359,8 @@ public class ExcelXlsxReader extends DefaultHandler {
formatIndex = style.getDataFormat(); formatIndex = style.getDataFormat();
formatString = style.getDataFormatString(); formatString = style.getDataFormatString();
short format = this.formatIndex; short format = this.formatIndex;
if ( (14 <= format && format <= 17) || format == 20 || format == 22 || format == 31 || format == 35 || format == 45 || format == 46 || format == 47 || (57 <= format && format <= 59) if ((14 <= format && format <= 17) || format == 20 || format == 22 || format == 31 || format == 35 || format == 45 || format == 46 || format == 47 || (57 <= format && format <= 59)
|| (175 < format && format < 178) || (182 <= format && format <= 196) || (210 <= format && format <= 213) || (208 == format)) || (175 < format && format < 178) || (182 <= format && format <= 196) || (210 <= format && format <= 213) || (208 == format)) { // 日期
{ // 日期
isDateFormat = true; isDateFormat = true;
} }
@ -369,6 +370,7 @@ public class ExcelXlsxReader extends DefaultHandler {
/** /**
* 对解析出来的数据进行类型处理 * 对解析出来的数据进行类型处理
*
* @param value 单元格的值 * @param value 单元格的值
* value代表解析BOOL的为0或1 ERROR的为内容值FORMULA的为内容值INLINESTR的为索引值需转换为内容值 * value代表解析BOOL的为0或1 ERROR的为内容值FORMULA的为内容值INLINESTR的为索引值需转换为内容值
* SSTINDEX的为索引值需转换为内容值 NUMBER为内容值DATE为内容值 * SSTINDEX的为索引值需转换为内容值 NUMBER为内容值DATE为内容值
@ -377,7 +379,7 @@ public class ExcelXlsxReader extends DefaultHandler {
*/ */
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public String getDataValue(String value, String thisStr) { public String getDataValue(String value, String thisStr) {
String type = "TEXT"; String type = null;
switch (nextDataType) { switch (nextDataType) {
// 这几个的顺序不能随便交换交换了很可能会导致数据错误 // 这几个的顺序不能随便交换交换了很可能会导致数据错误
case BOOL: //布尔值 case BOOL: //布尔值
@ -401,11 +403,11 @@ public class ExcelXlsxReader extends DefaultHandler {
String sstIndex = value.toString(); String sstIndex = value.toString();
try { try {
int idx = Integer.parseInt(sstIndex); int idx = Integer.parseInt(sstIndex);
if(sst != null){ if (sst != null) {
XSSFRichTextString rtss = new XSSFRichTextString(sst.getEntryAt(idx));//根据idx索引值获取内容值 XSSFRichTextString rtss = new XSSFRichTextString(sst.getEntryAt(idx));//根据idx索引值获取内容值
thisStr = rtss.toString(); thisStr = rtss.toString();
rtss = null; rtss = null;
}else { } else {
thisStr = value.toString(); thisStr = value.toString();
} }
@ -422,12 +424,13 @@ public class ExcelXlsxReader extends DefaultHandler {
} }
thisStr = thisStr.replace("_", "").trim(); thisStr = thisStr.replace("_", "").trim();
if(isDateFormat ){ if (isDateFormat) {
type = "DATETIME";isDateFormat = false; type = "DATETIME";
if(formatString != null && formatString.contains("%")){ isDateFormat = false;
if (formatString != null && formatString.contains("%")) {
type = getType(thisStr); type = getType(thisStr);
} }
}else { } else {
type = getType(thisStr); type = getType(thisStr);
} }
break; break;
@ -441,60 +444,67 @@ public class ExcelXlsxReader extends DefaultHandler {
thisStr = " "; thisStr = " ";
break; break;
} }
if(curRow==1){ if (curRow == 1) {
addField(thisStr, null); addField(thisStr, null);
}else { } else {
if(CollectionUtils.isEmpty(this.getFields())){ if (CollectionUtils.isEmpty(this.getFields())) {
throw new RuntimeException(Translator.get("i18n_excel_header_empty")); throw new RuntimeException(Translator.get("i18n_excel_header_empty"));
} }
if(curCol >= this.fields.size()){ if (curCol >= this.fields.size()) {
return thisStr; return thisStr;
} }
if(curRow==2){ if (curRow == 2) {
this.getFields().get(curCol).setFieldType(type); if (type != null) {
}else {
if(type.equalsIgnoreCase("TEXT")){
this.getFields().get(curCol).setFieldType(type); this.getFields().get(curCol).setFieldType(type);
} }
if(type.equalsIgnoreCase("DOUBLE") && this.getFields().get(curCol).getFieldType().equalsIgnoreCase("LONG")){ } else {
this.getFields().get(curCol).setFieldType(type); if (type != null) {
} if (type.equalsIgnoreCase("TEXT")) {
if(type.equalsIgnoreCase("DATETIME")){ this.getFields().get(curCol).setFieldType(type);
this.getFields().get(curCol).setFieldType(type); }
if (type.equalsIgnoreCase("DOUBLE") && this.getFields().get(curCol).getFieldType().equalsIgnoreCase("LONG")) {
this.getFields().get(curCol).setFieldType(type);
}
if (type.equalsIgnoreCase("DATETIME")) {
this.getFields().get(curCol).setFieldType(type);
}
} }
} }
} }
return thisStr; return thisStr;
} }
private void addField(String columeName, Integer index){ private void addField(String columeName, Integer index) {
TableField tableField = new TableField(); TableField tableField = new TableField();
tableField.setFieldType("TEXT"); tableField.setFieldType("TEXT");
tableField.setFieldSize(65533); tableField.setFieldSize(65533);
tableField.setFieldName(columeName); tableField.setFieldName(columeName);
tableField.setRemarks(columeName); tableField.setRemarks(columeName);
if(index != null){ if (index != null) {
this.fields.add(index, tableField); this.fields.add(index, tableField);
}else { } else {
this.fields.add(tableField); this.fields.add(tableField);
} }
} }
private String getType(String thisStr){
if(totalRows==0){ private String getType(String thisStr) {
if (totalRows == 0) {
return "TEXT"; return "TEXT";
} }
try{
if(thisStr.endsWith("%")){ try {
thisStr = thisStr.substring(0, thisStr.length()-1); if (thisStr.endsWith("%")) {
thisStr = String.valueOf(Double.valueOf(thisStr)/100); thisStr = thisStr.substring(0, thisStr.length() - 1);
thisStr = String.valueOf(Double.valueOf(thisStr) / 100);
} }
Long.valueOf(thisStr); Long.valueOf(thisStr);
return "LONG"; return "LONG";
}catch (Exception e){ } catch (Exception e) {
try { try {
Double.valueOf(thisStr); Double.valueOf(thisStr);
return "DOUBLE"; return "DOUBLE";
}catch (Exception ignore){ } } catch (Exception ignore) {
}
} }
return "TEXT"; return "TEXT";
} }