本文介绍了自动宽度不适用于合并的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
{
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...
推荐答案
我发现了您的问题,它是这一行:
I've spotted your problem, it's this line:
sheet.autoSizeColumn(0);
来自 javadocs上autoSizeColumn(int)我们看到以下关键信息:
From the javadocs on autoSizeColumn(int) we see this key bit of information:
您需要切换到 autoSizeColumn(int,boolean),然后传入一个真值.这将告诉POI在调整大小时要考虑合并的像元.因此,您的代码应改为:
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);
这篇关于自动宽度不适用于合并的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!