自动宽度不适用于合并列

Autowidth is not working for merged columns

{
row=sheet.createRow(0);
cell=row.createCell(0);
cell.setCellValue("header");
cell=row.createCell(1);
sheet.addMergedRegion(new CellRangeAddress(0,0,0,1));
row=sheet.createRow(1);
cell=row.createCell(0);
cell.setCellValue("Keys");
cell=row.createCell(1);
cell=row.setCellValue("Values");
row=sheet.createRow(2);
cell=row.createCell(0);
cell.setCellValue("No data");
cell=row.createCell(1);
sheet.addMergedRegion(new CellRangeAddress(1,1,0,1);
sheet.autoSizeColumn(0);
}

当我合并第 0 行的两列时,autosize 工作正常,但是在合并第二行的两列后,autosize 不工作。 提前致谢...

我发现了你的问题,是这一行:

sheet.autoSizeColumn(0);

javadocs on autoSizeColumn(int)我们可以看到这个关键信息位:

Default is to ignore merged cells.

您需要改为调用 autoSizeColumn(int,boolean),并传入一个真值。这将告诉 POI 在调整大小时考虑合并的单元格。因此,您的代码应该改为:

sheet.autoSizeColumn(0, true);