Skip to content

Commit

Permalink
解决首部文本空单元格
Browse files Browse the repository at this point in the history
  • Loading branch information
SwordfallYeung committed Oct 22, 2019
1 parent a397563 commit 6092df2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
8 changes: 3 additions & 5 deletions src/main/java/org/poi/ExcelReaderUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,9 @@ public static void copyToTemp(File file,String tmpDir) throws Exception{

public static void main(String[] args) throws Exception {
/*String path="C:\\Users\\y15079\\Desktop\\shenjiangnan\\H_20180208_ASS_PRODUCT_test3.xlsx";*/
<<<<<<< HEAD
String path="D:\\Github\\test.xls";
=======
String path="D:\\H3CIDEA\\POIExcel\\test.xls";
>>>>>>> 48495bea02107cd564555bb0b19754aa91bd6b6c
//String path="D:\\Github\\test.xls";

String path="D:\\H3CIDEA\\POIExcel\\test.xlsx";
/*String path="C:\\Users\\y15079\\Desktop\\shenjiangnan\\TestSample\\1010filesCollection5000100";*/
//H_20180111_Base_Date(4)_0420.xlsx
/*String path="C:\\Users\\y15079\\Desktop\\shenjiangnan\\TestSample\\REWORK\\H_20180105_Cto_REWORK_1600.xlsx";*/
Expand Down
26 changes: 22 additions & 4 deletions src/main/java/org/poi/ExcelXlsxReaderWithDefaultHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ enum CellDataType {
/**
* 判断上一单元格是否为文本空单元格
*/
private boolean startElementFlag = true;
private boolean endElementFlag = false;
private boolean charactersFlag = false;

Expand All @@ -122,7 +123,7 @@ enum CellDataType {
private String formatString;

//定义前一个元素和当前元素的位置,用来计算其中空的单元格数量,如A6和A8等
private String preRef = null, ref = null;
private String prePreRef = "A", preRef = null, ref = null;

//定义该文档一行最大的单元格数,用来补全一行最后可能缺失的单元格
private String maxRef = null;
Expand Down Expand Up @@ -175,23 +176,31 @@ public void startElement(String uri, String localName, String name, Attributes a
//c => 单元格
if ("c".equals(name)) {


//前一个单元格的位置
if (preRef == null) {
preRef = attributes.getValue("r");

} else {
//判断前一次是否为文本空字符串,true则表明不是文本空字符串,false表明是文本空字符串跳过把空字符串的位置赋予preRef
//中部文本空单元格标识 ‘endElementFlag’ 判断前一次是否为文本空字符串,true则表明不是文本空字符串,false表明是文本空字符串跳过把空字符串的位置赋予preRef
if (endElementFlag){
preRef = ref;
}
}

//当前单元格的位置
ref = attributes.getValue("r");
//首部文本空单元格标识 ‘startElementFlag’ 判断前一次,即首部是否为文本空字符串,true则表明不是文本空字符串,false表明是文本空字符串, 且已知当前格,即第二格带“B”标志,则ref赋予preRef
if (!startElementFlag && preRef.contains(prePreRef)){
cellList.add(curCol, "");
curCol++;
prePreRef = preRef;
preRef = ref;
}
//设定单元格类型
this.setNextDataType(attributes);
endElementFlag = false;
charactersFlag = false;
startElementFlag = false;
}

//当元素为t时
Expand Down Expand Up @@ -219,6 +228,7 @@ public void startElement(String uri, String localName, String name, Attributes a
*/
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
startElementFlag = true;
charactersFlag = true;
lastIndex += new String(ch, start, length);
}
Expand Down Expand Up @@ -255,7 +265,15 @@ public void endElement(String uri, String localName, String name) throws SAXExce
cellList.add(curCol, "");
curCol++;
}
}
} else if (ref.equals(preRef)){ //ref等于preRef,且以B或者C...开头,表明首部为空格
cellList.add(curCol, "");
curCol++;
int len = countNullCell(ref, "A");
for (int i = 0; i < len; i++) {
cellList.add(curCol, "");
curCol++;
}
}
cellList.add(curCol, value);
curCol++;
endElementFlag = true;
Expand Down
Binary file modified test.xlsx
Binary file not shown.
Binary file added test1.xlsx
Binary file not shown.

0 comments on commit 6092df2

Please sign in to comment.