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

发布于 2024-10-10 17:37:26 字数 1110 浏览 7 评论 0原文

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

我面临的问题是,当创建并打开电子表格时,列不会扩展,因此一些长文本(例如日期格式的文本)乍一看不会显示。

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

我发现 方法) groupRow()setColumnGroupCollapsed() 可能可以做到这一点,但可能我使用了错误的方式。

(虽然可能是错误的 strong>示例代码片段

        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”。

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

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.

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 :(

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.

Sample Code snippet

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

When this spreadsheet is created, the "Looooooong text not to show up first" string is in the cell but since the column is not expanded only "Loooooooo" is showing up.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

动听の歌 2024-10-17 17:37:26

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

这是指向API

请参阅此帖子以获取更多参考
使用apache poi时将excel单元格大小与内容大小匹配的问题

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

Here is a link to the API.

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

旧故 2024-10-17 17:37:26

提示:要使自动调整大小正常工作,应在将数据填充到 Excel 后调用 sheet.autoSizeColumn(columnNumber)

在填充数据之前调用该方法不会有任何效果。

Tip : To make Auto size work , the call to sheet.autoSizeColumn(columnNumber) should be made after populating the data into the excel.

Calling the method before populating the data, will have no effect.

厌倦 2024-10-17 17:37:26

如果您想自动调整工作簿中所有列的大小,以下方法可能有用:

public void autoSizeColumns(Workbook workbook) {
    int numberOfSheets = workbook.getNumberOfSheets();
    for (int i = 0; i < numberOfSheets; i++) {
        Sheet sheet = workbook.getSheetAt(i);
        if (sheet.getPhysicalNumberOfRows() > 0) {
            Row row = sheet.getRow(sheet.getFirstRowNum());
            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                int columnIndex = cell.getColumnIndex();
                sheet.autoSizeColumn(columnIndex);
            }
        }
    }
}

If you want to auto size all columns in a workbook, here is a method that might be useful:

public void autoSizeColumns(Workbook workbook) {
    int numberOfSheets = workbook.getNumberOfSheets();
    for (int i = 0; i < numberOfSheets; i++) {
        Sheet sheet = workbook.getSheetAt(i);
        if (sheet.getPhysicalNumberOfRows() > 0) {
            Row row = sheet.getRow(sheet.getFirstRowNum());
            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                int columnIndex = cell.getColumnIndex();
                sheet.autoSizeColumn(columnIndex);
            }
        }
    }
}
神妖 2024-10-17 17:37:26

您可以尝试这样的操作:

HSSFSheet summarySheet = wb.createSheet();
summarySheet.setColumnWidth(short column, short width);

这里参数是:工作表中的列号及其宽度
但是,宽度单位很小,例如您可以尝试 4000。

You can try something like this:

HSSFSheet summarySheet = wb.createSheet();
summarySheet.setColumnWidth(short column, short width);

Here params are:column number in sheet and its width
But,the units of width are pretty small, you can try 4000 for example.

向地狱狂奔 2024-10-17 17:37:26

对于 Excel POI:

sheetName.autoSizeColumn(cellnum); 

For Excel POI:

sheetName.autoSizeColumn(cellnum); 
烟花肆意 2024-10-17 17:37:26

如果您知道列的数量(如果它等于集合列表)。您可以简单地使用这个衬垫来调整一张纸的所有列(如果您至少使用 java 8):

IntStream.range(0, columnCount).forEach(sheet::autoSizeColumn)

If you know the count of your columns (f.e. it's equal to a collection list). You can simply use this one liner to adjust all columns of one sheet (if you use at least java 8):

IntStream.range(0, columnCount).forEach(sheet::autoSizeColumn)
℉絮湮 2024-10-17 17:37:26

下面的示例代码

HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("your sheet name");
HSSFRow row = sheet.createRow(0);

cell = row.createCell(0);
cell.setCellValue("A BIG NAME WITH AUTO SIZE FEATURE ENABLED");

// 这很重要

sheet.autoSizeColumn(0);

// 参数必须是单元格编号

cell = row.createCell(1);
cell.setCellValue("a big name without auto size feature enabled");

检查输出并疯狂:)

sample code below

HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("your sheet name");
HSSFRow row = sheet.createRow(0);

cell = row.createCell(0);
cell.setCellValue("A BIG NAME WITH AUTO SIZE FEATURE ENABLED");

//this is crucial

sheet.autoSizeColumn(0);

//argument must be cell number

cell = row.createCell(1);
cell.setCellValue("a big name without auto size feature enabled");

Check the output and go nuts :)

明天过后 2024-10-17 17:37:26

您可以在循环之后添加它。

for (int i = 0; i<53;i++) {
    sheet.autoSizeColumn(i);
}

You can add this, after your loop.

for (int i = 0; i<53;i++) {
    sheet.autoSizeColumn(i);
}
旧竹 2024-10-17 17:37:26

我使用下面的简单解决方案:

这是您的工作簿和工作表:

XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("YOUR Workshhet");

然后将数据添加到包含列和行的工作表中。将数据添加到工作表后,将以下代码写入 autoSizeColumn 宽度。

for (int columnIndex = 0; columnIndex < 15; columnIndex++) {
                    sheet.autoSizeColumn(columnIndex);
                }

此处,您添加工作表中的列数,而不是 15。

希望有人能帮助这个。

I use below simple solution:

This is your workbook and sheet:

XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("YOUR Workshhet");

then add data to your sheet with columns and rows. Once done with adding data to sheet write following code to autoSizeColumn width.

for (int columnIndex = 0; columnIndex < 15; columnIndex++) {
                    sheet.autoSizeColumn(columnIndex);
                }

Here, instead 15, you add the number of columns in your sheet.

Hope someone helps this.

孤君无依 2024-10-17 17:37:26

您也可以将文本换行。 PFB示例代码:

CellStyle wrapCellStyle = new_workbook.createCellStyle();
                    wrapCellStyle.setWrapText(true);

You can wrap the text as well. PFB sample code:

CellStyle wrapCellStyle = new_workbook.createCellStyle();
                    wrapCellStyle.setWrapText(true);
执笏见 2024-10-17 17:37:26

非常简单,使用这一行代码
dataSheet.autoSizeColumn(0)

或给出括号中的列数
dataSheet.autoSizeColumn(单元格编号)

Its very simple, use this one line code
dataSheet.autoSizeColumn(0)

or give the number of column in bracket
dataSheet.autoSizeColumn(cell number )

ゃ人海孤独症 2024-10-17 17:37:26

您可以使用调整列大小sheet.autoSizeColumn,但不要每次都使用它,当您完成写入时使用它

you can use resize column sheet.autoSizeColumn but dont use it each time use it when you done writing

娇妻 2024-10-17 17:37:26

将数据添加到工作表后,这对我有用。

 if (errorSpreadsheet.getPhysicalNumberOfRows() > 0)
        {
            Row xssfRow = errorSpreadsheet.getRow(errorSpreadsheet.getFirstRowNum());
            Iterator<Cell> cellIterator = xssfRow.cellIterator();
            while (cellIterator.hasNext())
            {
                Cell cell = cellIterator.next();
                int columnIndex = cell.getColumnIndex();
                errorSpreadsheet.autoSizeColumn(columnIndex);
            }
        }
    

After adding the data to the sheet, this working for me.

 if (errorSpreadsheet.getPhysicalNumberOfRows() > 0)
        {
            Row xssfRow = errorSpreadsheet.getRow(errorSpreadsheet.getFirstRowNum());
            Iterator<Cell> cellIterator = xssfRow.cellIterator();
            while (cellIterator.hasNext())
            {
                Cell cell = cellIterator.next();
                int columnIndex = cell.getColumnIndex();
                errorSpreadsheet.autoSizeColumn(columnIndex);
            }
        }
    
枕头说它不想醒 2024-10-17 17:37:26

如果您想进一步扩展单元格,可以使用setColumnWidth()

You can use setColumnWidth() if you want to expand your cell more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文