Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autowidth is not working for merged columns

Tags:

apache-poi

{
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...

like image 807
hardcode Avatar asked Jul 15 '15 06:07

hardcode


People also ask

Does AutoFit row height work on merged cells?

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.

How do you AutoFit text in merged cells?

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.

Why is AutoFit row height not working?

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.

Does filtering work on merged cells?

To use Filter, Sort or other functions, you need to unmerge cells and put to all of them the data from merged cells.


1 Answers

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);
like image 194
Gagravarr Avatar answered Sep 23 '22 02:09

Gagravarr