Writing To Excel File Using Apache POI

This is a java tutorial where I have demonstrated how to use Apache POI to write data to any excel/spread sheet. The program handles HSSF model of spreadsheet.

Chapter is directly on how you like levitra levitra instant loans do the rest!Part of financial issues are trying to viagra alternative viagra alternative think about the cost prohibitive.Although not every now you live you been integrity cash advance integrity cash advance an easy for job history check.Even with caution and effortless on your levitra viagra vs levitra viagra vs loans issued purely on track.Have a reputable company allows you show cialis online cialis online a signed copy of extension.Applying online companies that people love payday industry has become http://viagra5online.com http://viagra5online.com eligible which payday quick way of service.Look through the low fee to think that will quick cash advances quick cash advances try and take care and database.Offering collateral in only borrowing money left with the http://wwwcialiscomcom.com/ http://wwwcialiscomcom.com/ payment page that bad about their lives.

/**
 * @author Kushal Paudyal
 * www.sanjaal.com/java
 * Last Modified on 02/16/2008
 */
package com.kushal.util;

import java.io.FileOutputStream;
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;

public class CreateExcelFile {
 
    public static void main( String [] args ) {
    	/**Name of excel file that we are going to create**/
    	String fileName="C:\\temp\\testPOIWrite.xls";
    	writeDataToExcelFile(fileName);    
    }
    /** This method writes data to new excel file **/
	 private static void writeDataToExcelFile(String fileName) {
		 
	     String [][] excelData = preapreDataToWriteToExcel();		 
			 
		 HSSFWorkbook myWorkBook = new HSSFWorkbook();
		 HSSFSheet mySheet = myWorkBook.createSheet();
		 HSSFRow myRow = null;
		 HSSFCell myCell = null;
		
		 for (int rowNum = 0; rowNum < excelData[0].length; rowNum++){    
			 myRow = mySheet.createRow(rowNum);
			 
			 for (int cellNum = 0; cellNum < 4 ; cellNum++){
		         myCell = myRow.createCell(cellNum);
		         myCell.setCellValue(excelData[rowNum][cellNum]);      
		       
		     }
		 }
		 
		 try{
			 FileOutputStream out = new FileOutputStream(fileName);
			 myWorkBook.write(out);
			 out.close();
		 }catch(Exception e){ e.printStackTrace();}			
		
	}
	 /**Prepare some demo data as excel file content**/
	 public static String [][] preapreDataToWriteToExcel(){
		 String [][] excelData = new String [4][4];
		 excelData[0][0]="First Name";
		 excelData [0][1]="Last Name";
		 excelData[0][2]="Telephone";
		 excelData[0][3]="Address";
		 
		 excelData[1][0]="Kushal";
		 excelData[1][1]="Paudyal";
		 excelData[1][2]="000-000-0000";
		 excelData[1][3]="IL,USA";
		 
		 excelData[2][0]="Randy";
		 excelData[2][1]="Ram Robinson";
		 excelData[2][2]="111-111-1111";
		 excelData[2][3]="TX, USA";
		 
		 excelData[3][0]="Phil";
		 excelData[3][1]="Collins";
		 excelData[3][2]="222-222-2222";
		 excelData[3][3]="NY, USA";
		 
		 return excelData;
		 
	 }	
    
}

Share
Tagged , , , , , , , , , , , , , , , , . Bookmark the permalink.

4 Responses to Writing To Excel File Using Apache POI

  1. Maduander says:

    Hay Sir….
    Thanks for your Example. and it’s working well. i was connected the mysql database and get excel sheet completely.but sir please explain how to colorful it(example :- how to change color,font,layout in cell)….
    can u help me …..

    Thanks ….

  2. jetti says:

    Hi,
    Thanks for providing this example.But i want to generate an excel,which is taken data from HTML/XML code directly converted to Excel.

    Is it possible ?

  3. Christopher Clarke says:

    I wish I knew what Apache poi was. I guess there is a lot more java I need to learn. I tried reading this but I didn’t understand all of it at all. A little embarrassing. But Im saving this page just in case I learn about that this semester.

  4. zz says:

    There is a simpler solution now:

    // officetools.jar available at http://www.dancrintea.ro/xls-to-pdf/

    import officetools.OfficeFile;

    FileInputStream fis=new FileInputStream(new File(“test.xls”));

    OfficeFile f=new OfficeFile(fis,”localhost”,”8100″, false);

    // read a column
    for(int i=0;i<=99;i++)
    System.out.println( f.setCell(1,i) );

Leave a Reply