Autowidth is not working for merged columns

不问归期 提交于 2019-12-30 19:56:08

问题


{
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);
}

autosize is working when I merged two columns of row zero, but after merging two columns of second row autosize is not working.. thanks in advance...


回答1:


I've spotted your problem, it's this line:

sheet.autoSizeColumn(0);

From the javadocs on autoSizeColumn(int) we see this key bit of information:

Default is to ignore merged cells.

You need to switch to instead call autoSizeColumn(int,boolean), and pass in a true value. This will tell POI to take account of merged cells during the sizing. So, your code should instead be:

sheet.autoSizeColumn(0, true);


来源:https://stackoverflow.com/questions/31423387/autowidth-is-not-working-for-merged-columns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!