poi有一个隐含的最大行号

时间:2016-03-23 14:12:36

标签: apache-poi

我使用poi 3.12创建excel,并发现一种现象,即明确创建30000行,但打开生成的excel,只有27239行,并且没有错误输出。因此,似乎poi设置了默认的最大行数 - 27239。但我试过跟踪源代码,找不到像max rows这样的变量。代码是

InputStream is = new FileInputStream(srcFile);
POIFSFileSystem pOIFSFileSystem=new POIFSFileSystem(is); 
HSSFWorkbook srcWorkbook = new HSSFWorkbook(pOIFSFileSystem);
buildBusiData(srcWorkbook);
FileOutputStream fileOutputStream = new FileOutputStream(desFile);
srcWorkbook.write(fileOutputStream);

fileOutputStream.flush();
fileOutputStream.close();

那怎么回事?

1 个答案:

答案 0 :(得分:0)

我找到了原因,那就是源文件(模板)只有27239行

#source file
cat coupon_code_box.csv | wc -l
27240
# 
head coupon_code_box.csv -n 3
table header here
" "," "," "," "," "," "," "," "," "
" "," "," "," "," "," "," "," "," "

并且在我的代码中没有明确地创建新行,所以如果行号大于27239,它将忽略它。

Row row = sheet.getRow(rowIndex);
if(row!=null){
    Cell cell = row.getCell(celIndex);
    cell.setCellValue(fillVal);
}

现在我的解决方案是

Row row = sheet.getRow(rowIndex);
if(row==null){
    row=sheet.createRow(rowIndex); //explicitly create one new row
}
if(row!=null){
    Cell cell = row.getCell(celIndex);
    if(cell==null)
        cell = row.createCell(celIndex); //explicitly create one new cell
    cell.setCellValue(fillVal);
}