In this Blog, we will find out
- How to read xls, xlsx format Excel sheet using Apache POI API
- How to write into an Excel sheet using Apache POI API
Java doesn’t provide built-in support for working with excel files so we need to rely on Open Source APIs. I came across Apache POI.
Reason I choose
- Apache POI provides stream-based processing. So memory usage is less.
- It supports xlsx format as well.
I implemented this in Spring Maven-based Project.
Apache POI API
- We need to implement XSSFWorkbook for xlsx format keeping the interface as Workbook common.
- We need to implement HSSFWorkbook for xls format
In the above code based on file extension, we will implement either xls or xlsx format.
Please note that we have a common interface Workbook.
Once we are done with File Type now we need to read Sheet, Row, Column or Cell.
The following code will demonstrate that
To write in Excel sheet and add some formula Logic to a Cell (Here Sum of Columns)