导入处理无数据的单元格,记录所有有数据的单元格及下标,最后将无数据的单元格根据下标补全。

This commit is contained in:
duandongmei
2019-01-29 09:20:33 +06:00
parent 453ed153bc
commit 32d4ad3cd7

View File

@@ -8,7 +8,9 @@ import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
@@ -32,6 +34,8 @@ import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import com.nis.util.StringUtil;
/**
* 使用CVS模式解决XLSX文件可以有效解决用户模式内存溢出的问题
@@ -102,6 +106,7 @@ public abstract class XLSXCovertCSVReader {
// The last column printed to the output stream
private int lastColumnNumber = -1;
private List<Object> rowlist = new ArrayList<Object>();
private Map<Integer,Object> rowMap = new HashMap<Integer, Object>();
// Gathers characters as they are seen.
private StringBuffer value;
// private String[] record;
@@ -128,6 +133,7 @@ public abstract class XLSXCovertCSVReader {
this.nextDataType = xssfDataType.NUMBER;
this.formatter = new DataFormatter();
rowlist.clear();// 每次读取都清空行集合
rowMap.clear();
}
/*
@@ -287,6 +293,7 @@ public abstract class XLSXCovertCSVReader {
int len = countNullCell(refnum, preRefnum);
for(int i=0;i<len;i++){
rowlist.add(curCol, "");
rowMap.put(thisColumn, "");
curCol++;
}
}
@@ -298,6 +305,7 @@ public abstract class XLSXCovertCSVReader {
}
// System.out.println("refnum="+refnum+"preRefnum="+preRefnum+"curCol="+curCol);
rowlist.add(curCol, thisVal);
rowMap.put(thisColumn, thisVal);
rowData.add(new IndexValue(refnum,lastContents) );
curCol++;
// record[thisColumn] = thisStr;
@@ -324,15 +332,22 @@ public abstract class XLSXCovertCSVReader {
int len = countNullCell(maxRefnum, refnum);
for(int i=0;i<=len;i++){
rowlist.add(curCol, "");
//rowMap.put(thisColumn, "");
curCol++;
}
int totalLen= countTotalCell(maxRefnum);
if(rowlist.size()<totalLen) {
List<Object> tempList=new ArrayList<>(totalLen);
for(int i=0;i<(totalLen-rowlist.size());i++) {
tempList.add("");
//修改某一个单元格为空,字段映射顺序错乱问题
for(int i=0;i<totalLen;i++) {
if(!StringUtil.isEmpty(rowMap.get(i))){
tempList.add(rowMap.get(i));
}else{
tempList.add(i,"");
}
}
tempList.addAll(rowlist);
//tempList.addAll(rowlist);
rowlist= tempList;
}
}
@@ -343,6 +358,7 @@ public abstract class XLSXCovertCSVReader {
optRows(sheetIndex,lastColumnNumber,rowlist);
rowlist.clear();
rowMap.clear();
// rows.add(record.clone());
isCellNull = false;
// for (int i = 0; i < record.length; i++) {
@@ -352,6 +368,7 @@ public abstract class XLSXCovertCSVReader {
}
rowlist.clear();
rowData.clear();
rowMap.clear();
curCol = 0;
preRefnum = null;
refnum = null;