Apache POI Excel - 如何配置要扩展的列? [英] Apache POI Excel - how to configure columns to be expanded?

查看:35
本文介绍了Apache POI Excel - 如何配置要扩展的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Apache POI API 生成 excel 电子表格 以输出一些数据.

I am using Apache POI API to generate excel spreadsheet to output some data.

我面临的问题是,在创建和打开电子表格时,列没有展开,因此乍一看没有显示某些长文本(如日期格式文本).

The problem I am facing is when the spreadsheet is created and opened, columns are not expanded so that some long text like Date formatted text is not showing up on first glance.

我可以在 excel 中双击列边框以展开或拖动边框来调整列宽,但可能有 20 多列,每次打开电子表格时我都无法手动执行此操作:(

I could just double click the column border in excel to expand or drag the border to adjust the column width but there could be 20+ columns and there is no way I want to do that manually every time I open the spreadsheet :(

我发现(虽然可能是错误的方法)groupRow()setColumnGroupCollapsed() 可能能够做到这一点,但没有运气.也许我用错了方法.

I found out (though could be wrong method) groupRow() and setColumnGroupCollapsed() might be able to do the trick but no luck. Maybe I'm using it in wrong way.

示例代码片段

        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"字符串位于单元格中,但由于列未展开,因此仅显示Loooooooo".

如何配置它以便当我打开电子表格时,该列已经展开???

How can I configure it so that when I open my spreadsheet, the column is already expanded???

推荐答案

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

After you have added all your data to the sheet, you can call autoSizeColumn(int column) on your sheet to autofit the columns to the proper size

这里是 API.

查看此帖子以获取更多参考使用apache poi时excel单元格大小适配内容大小的问题

See this post for more reference Problem in fitting the excel cell size to the size of the content when using apache poi

这篇关于Apache POI Excel - 如何配置要扩展的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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