Skip to content

Commit

Permalink
解决03版xls行尾部空单元格没有添加的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
SwordfallYeung committed Sep 26, 2019
1 parent a92c9f3 commit 48495be
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/poi/ExcelReaderUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ 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";*/
String path="D:\\H3CIDEA\\POIExcel\\test.xlsx";
String path="D:\\H3CIDEA\\POIExcel\\test.xls";
/*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
21 changes: 18 additions & 3 deletions src/main/java/org/poi/ExcelXlsReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public class ExcelXlsReader implements HSSFListener {

private POIFSFileSystem fs;

/**
* 把第一行列名的长度作为列的总长
*/
private int totalColums = 0;

/**
* 总行数
*/
Expand Down Expand Up @@ -267,9 +272,19 @@ public void processRecord(Record record) {
}
lastColumnNumber = -1;

if (flag&&curRow!=0) { //该行不为空行且该行不是第一行,发送(第一行为列名,不需要)
ExcelReaderUtil.sendRows(filePath, sheetName, sheetIndex, curRow + 1, cellList); //每行结束时,调用sendRows()方法
totalRows++;
if (flag) { //该行不为空行且该行不是第一行,发送(第一行为列名,不需要)
if (curRow == 0 ){
totalColums = cellList.size(); //获取第一行列名的总数
}else {
//2003版尾部为空单元格的,xls里面是以该行最后一个有值的单元格为结束标记的,尾部空单元格跳过,故需补全
if (cellList.size() <= totalColums){ // 其他行如果尾部单元格总数小于totalColums,则补全单元格
for (int i = cellList.size(); i < totalColums; i++){
cellList.add(i, "");
}
}
ExcelReaderUtil.sendRows(filePath, sheetName, sheetIndex, curRow + 1, cellList); //每行结束时,调用sendRows()方法
totalRows++;
}
}
//清空容器
cellList.clear();
Expand Down

0 comments on commit 48495be

Please sign in to comment.