Showing posts with label Eclipse. Show all posts
Showing posts with label Eclipse. Show all posts

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.

How to add external jar files in Eclipse

In this project I am going to describe how to add jar files in eclipse.

1. First of all locate the folder where your jar file is.

2. Then open Eclipse and go to Project Option  in the menu bar

Project -> Properties -> Java Build Path -> Libraries -> Add External Jars

Adding External Jar file in Eclipse


3. Browse to the location where your jar file is located.

Click on the open and then Ok.

You are done. Feel free to comment. If you have another problem comment. If you like this post share it and visit other posts also. Thanks for visiting.

Monday, 16 March 2015

How to generate automatically set and get function for elements in eclipse

In this post I am going to describe you how to generate setter and getter function automatically for elements of the class.

First of all go to any of the elements of the class, right click on it and go to sources and there you will find the option of many types like this image.

Generate Getters and Setters


Click on the Generate Getters and Setters.

Now It will ask for which elements you want to create those function. Select those elements by cheking those check boxes and click on finish.

I hope it helps. If you find any problem related this describe it in comment. May be I can help you.
Thanks for visiting my blog. Share it , like it or comment it If you like my post. Check again for new posts.

Related Posts

Related Posts Plugin for WordPress, Blogger...