Tuesday, 15 July 2014

How to Generate Excel File Using Java...

package com.ashish;

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

public class CreateExlFile{
     public static void main(String[]args){
    try{
    String filename="D:/NewExcelFile.xls" ;
    HSSFWorkbook workbook=new HSSFWorkbook();
    HSSFSheet sheet =  workbook.createSheet("FirstSheet");

    HSSFRow rowhead=   sheet.createRow((short)0);
    rowhead.createCell(0).setCellValue("No.");
    rowhead.createCell(1).setCellValue("Name");
    rowhead.createCell(2).setCellValue("Address");
    rowhead.createCell(3).setCellValue("Email");

    HSSFRow row=   sheet.createRow((short)1);
    row.createCell(0).setCellValue("1");
    row.createCell(1).setCellValue("Sankumarsingh");
    row.createCell(2).setCellValue("India");
    row.createCell(3).setCellValue("sankumarsingh@gmail.com");

    FileOutputStream fileOut =  new FileOutputStream(filename);
    workbook.write(fileOut);
    fileOut.close();
    System.out.println("Your excel file has been generated!");

    } catch ( Exception ex ) {
        System.out.println(ex);

    }
       }
   }
you need to download the following jar file..
http://www.java2s.com/Code/Jar/p/Downloadpoi39jar.htm
 it is apache  poi library for more details please see the reference
http://poi.apache.org/download.html
we have more alternative approach please follow the link
http://viralpatel.net/blogs/java-read-write-excel-file-apache-poi/
http://howtodoinjava.com/2013/06/19/readingwriting-excel-files-in-java-poi-tutorial/

No comments:

Post a Comment