Earlier in this post, we introduced Apache POI- a Java API useful for interacting with Microsoft office documents.
Now we’ll see how can we read and write to an excel file using the API.
Writing an excel file
Writing a file using POI is very simple and involve following steps:
- Create a workbook
- Create a sheet in workbook
- Create a row in sheet
- Add cells in sheet
- Repeat step 3 and 4 to write more data
Reading an excel file
Reading an excel file is also very simple if we divide this in steps.
- Create workbook instance from excel sheet
- Get to the desired sheet
- Increment row number
- iterate over all cells in a row
- repeat step 3 and 4 until all data is read
Sometimes you need to read a file at different location: Here’s how you can do it:
You can append to the existing file using following code:
This article is contributed by Pankaj Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Creating Sheets in Excel File in Java using Apache POI
- Creating a Cell at specific position in Excel file using Java
- Reading a CSV file in Java using OpenCV
- Apache POI | Getting Started
- Apache POI | Introduction
- Introduction to Apache Cassandra
- How to install Apache server in Ubuntu ?
- Where does PHP store the error log? (php5, apache, fastcgi, cpanel)
- Introduction to Apache Maven | A build automation tool for Java projects
- Excel VBA | sum() functions
- Excel VBA | count() functions
- Excel VBA | Average() Functions
- Difference between Data Scientist, Data Engineer, Data Analyst
- Writing a Windows batch script
- Benefits of writing GATE exam