Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAVA+POI API Excel- Need to increase width of the column

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

I did this –

HSSFRow dataRow = sampleDataSheet.createRow(0);

HSSFCellStyle cellStyle = setHeaderStyle(sampleWorkbook);

cellStyle.setWrapText(true);

***sampleDataSheet.autoSizeColumn(1000000);***

But its not changing anything..

like image 260
smriti Avatar asked Aug 19 '12 17:08

smriti


People also ask

How do I change the width of a column in Excel using poi?

If you set a column width to be eight characters wide, e.g. setColumnWidth(columnIndex, 8*256) , then the actual value of visible characters (the value shown in Excel) is derived from the following equation: Truncate([numChars*7+5]/7*256)/256 = 8; which gives 7.29 .

How do you make Excel Cells expand to fit text automatically in Java?

In Excel, using the Ribbon option you can autofit the text by following the given steps: Choose one or all the columns given on the sheet to AutoFit column width. For this, you need to go to the Home tab > Cells group > Format > AutoFit Column Width.

How do you increase a width of a column?

Set a column to a specific width Select the column or columns that you want to change. On the Home tab, in the Cells group, click Format. Under Cell Size, click Column Width. In the Column width box, type the value that you want.


2 Answers

This should work. However,

sampleDataSheet.autoSizeColumn(1000000);

auto-expands column 1000000.

If you want to auto-expand column 0(the first column), use:

sampleDataSheet.autoSizeColumn(0);

To auto-expand column 0 to 9(the first 10 columns):

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

Also, you should create all your rows and fill them with content first, before you call autoSizeColumn(so the column gets the width of the value with the broadest width).

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

like image 71
Integrating Stuff Avatar answered Sep 28 '22 02:09

Integrating Stuff


// 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);
like image 24
swamy Avatar answered Sep 28 '22 03:09

swamy