apache poi超大型自动列宽。

时间:2021-10-30 20:23:54

I am try to create a big excel 2010 with 30 columns and 1 million records with Apache poi latest. 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 . 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

我正在尝试用Apache poi最新版本创建一个包含30列和100万条记录的大型excel 2010。我正在创建的链接是http://svn.apache.org/repos/poi/trunk/src/examples/src/src/src/org/apache/poi/xssf/usermodel/examples/biggdemo.java中描述的。但是我希望列宽与列标题文本大小相同。但是当我用下面的代码创建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

内存

3 个解决方案

#1


30  

First Select the first row or header because only Header can give you the max number of cells in a row.

首先选择第一行或标题,因为只有标题才能给出一行中单元格的最大数量。

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.

这将使列的宽度与其标题的宽度相同。

#2


4  

I would also suggest you look at using sxssf. It uses an xml format to load it into 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.

我还建议您使用sxssf。它使用xml格式将其加载到Excel中。这是创建大型报表或网格的更快、更好的方式。默认情况下,xssf中的自动大小调整函数花费的时间太长。

#3


3  

sheet.autoSizeColumn(columnNumber) works. this will resize the column to the largest cell length.

sheet.autoSizeColumn(columnNumber)工作。这将使列的大小调整到最大的单元格长度。

#1


30  

First Select the first row or header because only Header can give you the max number of cells in a row.

首先选择第一行或标题,因为只有标题才能给出一行中单元格的最大数量。

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.

这将使列的宽度与其标题的宽度相同。

#2


4  

I would also suggest you look at using sxssf. It uses an xml format to load it into 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.

我还建议您使用sxssf。它使用xml格式将其加载到Excel中。这是创建大型报表或网格的更快、更好的方式。默认情况下,xssf中的自动大小调整函数花费的时间太长。

#3


3  

sheet.autoSizeColumn(columnNumber) works. this will resize the column to the largest cell length.

sheet.autoSizeColumn(columnNumber)工作。这将使列的大小调整到最大的单元格长度。