- Community Pick
For the purposes of this article, I will be using NetBeans 6.9. If you want to follow along with this tutorial, open NetBeans and create a new "Java Application" called something like "ExcelCreator".
If you don't have NetBeans you can get it here: http://netbeans.org/downlo
What we're going to do is create a simple Java Application to create a spreadsheet. We're going to have a text box, an "Add" button, a JList, and an "Export to Excel" button. When the "Add" button is pressed, the inputted text will be added to the JList. When the "Export to Excel" button is pressed, the application with take the values in the JList and create an Excel Spreadsheet with those values in the "B" column and a counter variable in the "A" column. For example sake, we'll also center the values in the cells. So if there are the following 5 values in the JList ('item a', 'item b', 'item c', 'item d', 'item e') the Excel Spreadsheet will look like this:
Once you have created a new "Java Application", create a new JFrame Form called "GUI" in the <default package> under "Source Packages". Add the text box, JList, and two buttons to you form to make it look like this:
Be sure to create meaningful variable names for your text box and JList especially as you'll use them in the code later. I called my text box "txtInput" and my JList I called "jlValues".
Let's create code for the "Add" button so that when the "Add" button is pressed, the inputted text will be added to the JList and the text box empties for a new value to be entered. Simply double-click on the "Add" to create an event for it. Inside the ActionPerformed code block, put the following code (read the comments for code explanation!):
Now we are ready to download the POI and add it as a library to our project. Pay close attention to the following steps as they are crucial to our desired functionality.
- 1
Downloading the POI
Go to this URL to download the POI: http://poi.apache.org/down
Click on this one here:
Choose any mirror and save it to your desktop.
Unzip the folder to any directory that you choose, I put mine in "C:/Program Files/java/3rd_Party_APIs"
Here is where mine is located:
- 2
Add the POI JAR as a Project Library
We need to add the highlighted JAR file to our projects library list. To do that, right click on the Project Name in the "Projects Window" and select "Properties". In the "Categories" panel on the left, select "Libraries". On the right, press the "Add Jar/Folder" button. Find your extracted folder that you downloaded in Step 1 and double click on the jar that I highlighted in the last screen-shot. If the window looks like the following, click okay at the bottom and you'll have successfully set up the library.
- 3
Add Import Statement
Go to the top of your GUI.java class and add the following import lines at the very top just before the class declaration:
Now we are ready to write the code that actually creates the Excel Spreadsheet with the values from the database. Double-click on the "Export to Excel" button and add the code snippets by following these next set of steps:
- 1
Create the WorkBook Object
The first thing that we need to do is create the WorkBook object itself and Create a Sheet in the Workbook:
- 2
Create Cells for Each Value in the JList
- 3
Create the Excel File via FileOutputStream and Open it for Viewing
There are a lot of other things you can do with the Apache POI. For more information and code examples, look here: http://poi.apache.org/spre
Also, my example program "GUI.java" is attached.
Happy Excel Creating :)