Download Excel File In Java

Recently, I came across requirement to create ExcelSheet from thin Java Client used by Salesforce. So, I though to share my experience on Creating Excel Sheet in Java. As we know that Java is product of Oracle and Excel is product of Microsoft. Off-course, There will be no standard functionality available in Java to achieve our requirement of creating Excel Sheet. However thanks to Apache for their POI Library. POI Stands for “Poor Obfuscation Implementation” as the file formats created by this library is obfuscated poorly with help of reverse Engineering. Anyways, we don’t have to bother about it and thankful to them for providing such wonderful library.

Apache POI library can be used to create Excel Sheet, Word Document and PowerPoint. In this post, we will be totally focusing on Excel Sheet using “XSSF (XML Spreadsheet Format)” component.

Prerequisite :
Add all jar files downloaded from Apache POI download site in Java Program’s build path.

Download Excel File In Java

Error while downloading Excel 2007 file from a Java - based web page stackoverflow.com. We have created a portal using APACHE POI Framework, and when.

Demo Code :

  • How to Read and Write Excel Files in Java using Apache POI - callicoder/java-read-write-excel-file-using-apache-poi. Clone or download.
  • Nov 23, 2013 - Below is the JAVA code to read and write the Xlsx file using POI library. File file = new File(“Path of the sheet”); XSSFWorkbook workbook = new XSSFWorkbook(file.toString()); XSSFSheet sheet = workbook.getSheetAt(0).

Code Written above is very simple and self Explanatory. This is the Output of above code,

Java

I have collected few common Questions asked on Apache POI, lets discuss it.

Java Generate Excel File

1. How to Auto Re-size Column in Generated Excel Sheet?
Ans : We can use “autoSizeColumn(int ColumnNumber)” method of Sheet Object.

2. How to add Styles in Cell or Column in Excel Sheet ?
Ans : As shown in above code, We have Object named “CellStyle” used in method “getHaderStyle()”. We can set Background Color as well and all other stuff.

Java Excel Library

3. How to merge Columns in Excel ?
Ans : Using below code snippet

4. How to Auto – Re-size row in Generated Excel Sheet ?
Ans: To Auto-re-size row, we have to follow two steps:

  • Set Word Wrap property to true in CellStyle
  • Call method setHeightInPoints on row object

Download Excel File From Web

Sample Code :

I am waiting for your feedback and suggestions on this post. Happy Coding !!! 🙂