Introduction To Apache POI – The Java API To Read Write Microsoft Documents – Excel
Apache POI provides pure Java libraries for reading and writing files in Microsoft Office formats, such as Word, PowerPoint and Excel. It has different components for reading and writing different Microsoft Office files:
- POIFS for OLE 2 Documents
- HSSF and XSSF for Excel Documents
- HWPF and XWPF for Word Documents
- HSLF and XSLF for PowerPoint Documents
- HPSF for OLE 2 Document Properties
- HDGF and XDGF for Visio Documents
- HPBF for Publisher Documents
- HMEF for TNEF (winmail.dat) Outlook Attachments
- HSMF for Outlook Messages
Hope now you get to understand that Apache POI is not only to read and write Excel sheet. It provides different components to read different Microsoft office files. So, if interviewer asks you that how you read an excel file in Java, your answer should be – ” We use HSSF and XSSF component provided by Apache POI to read and write excel in Java”.
You will find excel with two extensions:-
- .xls ( Microsoft Excel 2003 file )
- .xlsx (Microsoft Excel 2007 file or later )
Apache POI provides different components for each type of extension of excel, which are given below:
- .xls – HSSF ( Horrible SpreadSheet Format )- You need to download “poi” jar files.
- .xlsx – XSSF ( XML SpreadSheet Format )- You need to download “poi-ooxml ” jar files.
If you do not want to read write excel based on its extension, you can use common spreadsheet usermodel which requires poi-ooxml and core poi libraries. Common spreadsheet is used widely now as it can read excel with any extension with same lines of code. No worries of using HSSF or XSSF for workbook and sheet.
Let’s update answer of above discussed question:
So, if interviewer asks you that how you read an excel file in Java, your answer should be – ” We use HSSF, XSSF or Common spreadsheet (SS) usermodel provided by Apache POI to read and write excel in Java”. Common SS usermodel can read/write excel with both extensions .xls and .xlsx.
In next post, we will explore more about it. Stay tuned.
In case of any doubt, suggestion or you find some mistake, feel free to let me know in comments.