Apache POI Excel - 如何配置列以扩展?

40 浏览
0 Comments

Apache POI Excel - 如何配置列以扩展?

我正在使用 Apache POI API 生成 excel 表格来输出一些数据。

我遇到的问题是当表格创建并打开时,列没有展开,因此一些较长的文本(例如日期格式的文本)不会在第一次查看时显示出来。

我可以在 excel 中双击列边界进行扩展或拖动边界以调整列宽,但可能会有20多列,我不想每次打开工作簿都手动操作 🙁

我发现(虽然可能是错误的方法),groupRow()setColumnGroupCollapsed() 可能能够完成此操作,但没有成功。也许我是用错了方法。

示例代码片段

        Workbook wb = new HSSFWorkbook();
        CreationHelper createHelper = wb.getCreationHelper();
        //create sheet
        Sheet sheet = wb.createSheet("masatoSheet");
        //not really working yet.... :(
        //set group for expand/collapse
        //sheet.groupRow(0, 10); //just random fromRow toRow argument values...
        //sheet.setColumnGroupCollapsed(0, true);
        //create row
        Row row = sheet.createRow((short)0);
        //put a cell in the row and store long text data
        row.createCell(0).setCellValue("Loooooooong text not to show up first");

当创建此表格时,\"Looooooong text not to show up first\" 字符串在单元格中,但由于列没有展开,只有 \"Loooooooo\" 显示出来。

如何配置,以便在打开工作簿时,列已经展开???

admin 更改状态以发布 2023年5月21日
0
0 Comments

提示:为了使自动调整大小生效,调用sheet.autoSizeColumn(columnNumber)应在将数据填充到Excel之后进行之后

在填充数据之前调用该方法将没有效果。

0
0 Comments

在将所有数据添加到工作表中后,您可以调用 autoSizeColumn(int column) 在工作表上自动适应列的正确大小。

这里提供了一个链接到API

参考此文章了解更多信息Problem in fitting the excel cell size to the size of the content when using apache poi

0