sticky_elbows
sticky_elbows

Reputation: 1514

How to auto-size all columns in a sheet with Apache POI

I am creating excel files in Java with Apache POI 3. The default width of the columns are not enough to show the full contents of some cells. I found the method autosizeColumn(int column) in the Sheet interface which works for individual columns, but I am wondering how to do it for all columns in a sheet and maybe only to some columns if they contain a certain value in their first cell.

for example:

if(cell.getStringCellValue().equals("some value")){
    //autosize or set the width to a certain value for this cell's column
}

How can I do it?

Upvotes: 1

Views: 5190

Answers (1)

Davis Broda
Davis Broda

Reputation: 4135

you could set the DefaultColumnWidth as below, if you wanted to resize all columns. Default width is 8, for reference, so the below example sets it to be 50% wider.

XSSFSheet sheet = wb.createSheet(sheetName);
sheet.setDefaultColumnWidth(12);

Upvotes: 1

Related Questions