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

查看:131
本文介绍了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文本不显示第一个字符串在单元格中,但是由于列只有Loooooooo才会展开。

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

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

推荐答案

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

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

以下是 API

查看此信息以获取更多参考
装配中的问题使用apache poi时,将细胞大小与内容大小

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天全站免登陆