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 OpenCSV
- Difference Between Apache Hive and Apache Impala
- Difference Between Apache Hadoop and Apache Storm
- How to Convert Data URI to File then append to FormData?
- Apache POI | Introduction
- Apache POI | Getting Started
- Introduction to Apache Cassandra
- Why Apache Kafka is so Fast?
- Difference Between Hadoop and Apache Spark
- How to install Apache server in Ubuntu ?
- Apache Cassandra (NOSQL database)
- Where does PHP store the error log? (php5, apache, fastcgi, cpanel)