java SXSSFWorkbook 上的 AutosizeColumns
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14497082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AutosizeColumns on SXSSFWorkbook
提问by sterym
Is it possible to autoSizeColumns on a streaming SXSSFWorkbook? I implemented an export functionality to export a list of objects to excel. At first I used the XSSFWorkbook (not streaming) and after all the cells were created, I autosized all the columns, resulting in a nice excel file.
是否可以在流式 SXSSFWorkbook 上使用 autoSizeColumns?我实现了一个导出功能,将对象列表导出到 excel。起初我使用了 XSSFWorkbook(不是流媒体),在创建了所有单元格之后,我自动调整了所有列的大小,从而生成了一个不错的 excel 文件。
For performance issues we wanted to change the workbook to the streaming version, but this resulted in a NullPointer at org.apache.poi.ss.util.SheetUtil.getCellWidth.
对于性能问题,我们希望将工作簿更改为流式版本,但这导致 org.apache.poi.ss.util.SheetUtil.getCellWidth 处出现 NullPointer。
Is it possible to call autoSizeColumns for a SXSSFWorkbook?
是否可以为 SXSSFWorkbook 调用 autoSizeColumns?
Im using poi-ooxml 3.9, but I have the same issue in 3.8.
我使用的是 poi-ooxml 3.9,但我在 3.8 中遇到了同样的问题。
采纳答案by cremersstijn
You need to make sure every cell has a value.
您需要确保每个单元格都有一个值。
We use the following code to set a string value to a cell:
我们使用以下代码为单元格设置字符串值:
Cell c = row.createCell(i);
c.setCellValue(text == null ? "" : text );
** Cell should never be null values else it throws NullPointerException. Hence set the value as shown above.
** 单元格不应该是空值,否则它会抛出 NullPointerException。因此设置如上所示的值。
Thanks a lot, this helped!!
非常感谢,这有帮助!!
回答by Virendra khade
Use sheet.isColumnTrackedForAutoSizing(0);for first and subsequently used for other column, i have faced exception whenever code executed autoSizeColumn(0) get executed. by using above code i have resolved the issue and it's good to expand the column width too based on the text.
使用sheet.isColumnTrackedForAutoSizing(0); 对于第一列并随后用于其他列,每当执行代码 autoSizeColumn(0) 时,我都会遇到异常。通过使用上面的代码,我已经解决了这个问题,并且根据文本扩展列宽也很好。
回答by Srini Yedire
Error: NullPointerExceptionon org.apache.poi.ss.util.SheetUtil.getCellWidth(SheetUtil.java:122)
错误:NullPointerExceptiononorg.apache.poi.ss.util.SheetUtil.getCellWidth(SheetUtil.java:122)
Fix: Always set value to Cell as shown below, it throws NullPointerException when there is null in Cell, so set the value as:
修复:总是如下图设置值给Cell,当Cell为null时会抛出NullPointerException,所以设置值为:
Cell c = row.createCell(i);
c.setCellValue(text == null ? "" : text );