Apache POI, Java

Auto sizing columns in Excel files created with POI in Java

So, I was going through my usage reports of the Amazon EC2 Cloud Computing platform and it turned out to be quite a frustrating experience. Reason? Have a look yourself.

Noticed anything? Well, the columns are not auto sized or expanded to include the full text. It is frustrating from the usability point of view as the user will have to expand each column to see the full text.

If you are creating Excel files in Java using Apache POI API, it is very simple to auto size the columns. After you are done adding data to the sheet, you just need to call a method to auto fit the column width.

Here is the code to auto size the first 10 columns of the spread sheet.

// Create the spreadsheet
HSSFSheet spreadSheet = workBook.createSheet("Hello_World");

// Create rows and write to the sheet

// Auto size the column widths
for(int columnIndex = 0; columnIndex < 10; columnIndex++) {

Note: If you are looking for how to create excel files using Apache POI API in Java, please refer this post.


7 thoughts on “Auto sizing columns in Excel files created with POI in Java

  1. the same code is used to make this column auto size, which is happening in windows machine but not happening in linux machine, PLS Help?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s