{
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...
In Excel, you cannot use the AutoFit feature on a column that contains a cell merged with cells in other columns. Likewise, you cannot use AutoFit on a row that contains a cell merged with cells in other rows.
It is quite time consuming to manually adjust every row. When a group of cells in a row are merged and the text wraps to two or more lines, double-clicking the row border just to the left of column A (or Format>Row>Autofit) auto-heights the row to one line of text.
Sometimes, Autofit refuses to work when there are merged cells in your Excel. This is a known issue if you're using really old versions of Excel (2003 or prior versions). Microsoft has even acknowledged this issue here. If this happens to you, the only workaround is to manually set the row height or column width.
To use Filter, Sort or other functions, you need to unmerge cells and put to all of them the data from merged cells.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With