Thursday 19 March 2015

How to write data to an excel file in java (Eclipse)

In this post I am going to describe how to write data in to an excel file in java.

There are number of ways. One of them is by using Apache POI.

Apache POI is a project under the guidance of Apache Software Foundation. This POI libraries are used to write data to Microsoft office formats like excel, word etc.

I am assuming you are using Eclipse IDE for developing Java project.

1. First thing you have to download the apache POI from the following link according to your computers environment.(Operating System, 32/64 bit version)


I prefer to download the stable version.

2. After Downloading unzip the folder. You will see some jar files. There will be a jar file with named like poi-3.11-20141221.jar . Digits may vary in the files name according to their version and their release date.

3. Include this file into your projects external jar files. For doing the same you can visit my previous post.


Now we are done with preprocessing. We have to write code for writing data in an excel file.

4. Import these classes to your code.

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class writeToExcel{


       public static void main(Strings [] args) throws InterruptedException{

                  try{

                               FileOutputStream fileOut = new FileOutputStream("test2.xls");
                               HSSFWorkbook workbook = new HSSFWorkbook();
                               HSSFSheet worksheet = workbook.createSheet("test2 Worksheet");

                                // It means we create an output file with name test2.xls and open an workbook 
                                // for working and worksheet for creating data and then we will write to our file

                             HSSFRow row1 = worksheet.createRow(0);
                             HSSFCell column1= row1.createCell(0);
                             column1.setCellValue("hello");
                             HSSFCell column2= row1.createCell(1);
                             column2.setCellValue("Thanks for visiting ");
          
                             // you can iterate through for creating more rows. make sure that it is indexed 
                            // from 0 as array in C 
                            // First create row and then column and set their values.
                               
                                workbook.write(fileOut);
                                  fileOut.flush();
                                fileOut.close();
                                workbook.close();
                   }

                    catch (FileNotFoundException exc) {
                                                 exc.printStackTrace();
                   } catch (IOException exc) {
                                                 exc.printStackTrace();
                   }
      }

}

This code is running. If you get any error comment. I will try to help. If you find the post useful Share it.

No comments:

Post a Comment

Related Posts

Related Posts Plugin for WordPress, Blogger...