Excel JAVA + POI API - need to increase column width

I want to increase column width of excel sheet. since I am writing a trough the code is long. and I need to drag the column manually to see the full text.

I did it -

HSSFRow dataRow = sampleDataSheet.createRow(0); HSSFCellStyle cellStyle = setHeaderStyle(sampleWorkbook); cellStyle.setWrapText(true); ***sampleDataSheet.autoSizeColumn(1000000);*** 

But it does not change anything..

+9
source share
3 answers

That should work. but

 sampleDataSheet.autoSizeColumn(1000000); 

automatically extends column 1,000,000.

If you want to automatically expand column 0 (first column), use:

 sampleDataSheet.autoSizeColumn(0); 

To automatically expand a column from 0 to 9 (first 10 columns):

 for (int i=0; i<10; i++){ sampleDataSheet.autoSizeColumn(i); } 

In addition, you must create all your rows and fill them with content first before you call autoSizeColumn (so that the column gets the width of the value with the widest width).

(If you want to set the column width to a fixed value, use HSSFSheet.setColumnWidth (int, int) instead.)

+18
source
 // We can set column width for each cell in the sheet sheet.setColumnWidth(0, 1000); sheet.setColumnWidth(1, 7500); sheet.setColumnWidth(2, 7500); // By applying style for cells we can see the total text in the cell for specified width HSSFCellStyle cellStyle = workBook.createCellStyle(); cell.setCellStyle(cellStyle ); cellStyle.setWrapText(true); 
+12
source

sheet.autoSizeColumn(columnNumber) works. this will resize the column to the largest cell length.

-one
source

All Articles