apache poi excel 大自动列宽 [英] apache poi excel big auto column width

查看:34
本文介绍了apache poi excel 大自动列宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用最新的 Apache poi 创建一个包含 30 列和 100 万条记录的大型 excel 2010.我正在按照此链接中的描述创建 http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/BigGridDemo.java.但我希望列宽与列标题文本大小相同.但是当我在使用以下代码创建 excel 后执行此操作时

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

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

这需要很长时间,即使堆大小为 5GB,我的内存也不足.

it is taking a huge time and even with 5gb heap size I am getting out of memory.

谢谢

内存

推荐答案

First 选择第一行或第一行,因为只有 Header 才能为您提供一行中的最大单元格数.

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);

然后对该行的每一列使用 autoSizeColumn

Then use autoSizeColumn on each column of that row

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.

这篇关于apache poi excel 大自动列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆