简体   繁体   English

apache poi excel大自动列宽

[英]apache poi excel big auto column width

I am try to create a big excel 2010 with 30 columns and 1 million records with Apache poi latest. 我试着用Apache poi最新创建一个包含30列和100万条记录的大型excel 2010。 I am creating as describe in this link http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/BigGridDemo.java . 我正在创建此链接中的描述http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/BigGridDemo.java but I want column width to be same as column header text size. 但我希望列宽与列标题文本大小相同。 but when I am doing this after creating excel with following code 但是在使用以下代码创建excel之后我正在执行此操作

for (int x = 0; x < sheet.getRow(0).getPhysicalNumberOfCells(); x++) {
            sheet.setColumnWidth(x, 20*256);
        }

it is taking a huge time and even with 5gb heap size I am getting out of memory. 它占用了大量的时间,即使有5GB的堆大小,我也会失去记忆。

thanks 谢谢

ram 内存

First Select the first row or header because only Header can give you the max number of cells in a row. 首先选择第一行或标题,因为只有Header可以为您提供一行中的最大单元格数。

HSSFRow row = wb.getSheetAt(0).getRow(0);

Then use autoSizeColumn on each column of that row 然后在该行的每一列上使用autoSizeColumn

for(int colNum = 0; colNum<row.getLastCellNum();colNum++)   
    wb.getSheetAt(0).autoSizeColumn(colNum);

This will set the column width same as that of its Header width. 这将设置列宽与其标题宽度相同。

I would also suggest you look at using sxssf. 我还建议你看一下使用sxssf。 It uses an xml format to load it into Excel. 它使用xml格式将其加载到Excel中。 This is much faster and a better way to create large reports or grids. 这是更快,更好的方法来创建大型报表或网格。 The autosizing function in xssf takes too long by default. 默认情况下,xssf中的自动调整功能时间过长。

sheet.autoSizeColumn(columnNumber) works. sheet.autoSizeColumn(columnNumber)有效。 this will resize the column to the largest cell length. 这会将列调整为最大单元格长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM