Java 使用 poi 自动调整列宽

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22004483/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:59:45  来源:igfitidea点击:

Autosize column width using poi

javaexcelapache-poi

提问by rick

I am appending data to the existing excel using POI. But on appending the data, the excel headers which are already present before the editing are compressed to small areas. Can someone please suggest me how to avoid this? I have applied even autoSizeafter appending data, but of no use.

我正在使用POI. 但是在附加数据时,编辑前已经存在的excel标题被压缩到小区域。有人可以建议我如何避免这种情况吗?即使autoSize在附加数据后我也申请了,但没有用。

FileInputStream inputStream = new FileInputStream(new File("dataexcel.xlsx"));

    XSSFWorkbook workbook = new XSSFWorkbook(intputStream);
    XSSFSheet sheet = workbook.getSheetAt(0);
    int rowNum = sheet.getPhysicalNumberOfRows();

                    int cellNum = 1;
                    Row lastRow = sheet.createRow(rowNum);

    for(int i =0 ; i<7; i++)
    {
                    Cell cell = lastRow.createCell(cellNum++);
                    cell.setCellValue(value);
                    cell.setCellStyle(generalStyle);
    }

Suppose the headers has a long text like "This is very long text"and appending the 7 values(2,3,1,7,4,6,5) to the existing excel in each cell to the next row like above. Then the enitre columns' widths shrink to the values of these single digits and the headers' text is almost invisible until it is expanded manually.

假设标题有一个长文本,例如"This is very long text"将 7 个值(2,3,1,7,4,6​​,5)附加到每个单元格中的现有 excel 到下一行,如上所示。然后整个列的宽度缩小到这些单个数字的值,并且标题的文本在手动扩展之前几乎不可见。

回答by Zaw Than oo

Use autoSizeColumn()method?

使用autoSizeColumn()方法?

Example Your Sheet as below

示例您的工作表如下

+----------+----------+----------+-------------+
| Column A | Column B | Column C | Column D    |
+----------+----------+----------+-------------+
|   xxx    |    xxx   |This is very long text  |
|          |          |          |             |
+----------+----------+----------+-------------+

If so, make auto column size for Column c, sheet.autoSizeColumn(2);. Result will be

如果是这样,请为Column c, 设置自动列大小sheet.autoSizeColumn(2);。结果将是

+----------+----------+------------------------+-------------+
| Column A | Column B | Column C               | Column D    |
+----------+----------+------------------------+-------------+
|   xxx    |    xxx   |This is very long text                |
|          |          |                        |             |
+----------+----------+------------------------+-------------+

回答by IceArdor

I believe this is a known issue in Apache POI 3.9 and earlier. The problem is POI sets the column width to 0. Try updating to POI 3.10 and see if that solves your problem.

我相信这是 Apache POI 3.9 及更早版本中的一个已知问题。问题是 POI 将列宽设置为 0。尝试更新到 POI 3.10,看看是否能解决您的问题。

More info at https://issues.apache.org/bugzilla/show_bug.cgi?id=55644

更多信息请访问https://issues.apache.org/bugzilla/show_bug.cgi?id=55644

回答by Vadim Nevskikh

POI 4.0: Try to call sheet.autoSizeColumn(*)after the calling all of createCellmethods.

POI 4.0:尝试sheet.autoSizeColumn(*)在调用所有createCell方法后调用。