Export to Excel in Java
Here is a code to export data to excel from Java.
you may required poi.jars (download all jars) to execute this code.

//Export to Excel in Java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.sql.Date;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow;
import java.io.FileOutputStream;

public class excelexport
{
public static void main(String arfs[])
{
String ename="Java Infinite";             
String emonth="August";              
int wdays= 31;                            
try
{ 
    
response.setContentType("application/vnd.ms-excel");
    
response.setHeader("Content-Disposition", "attachment; filename=filename.xls");   
WritableWorkbook workBook = Workbook.createWorkbook(response.getOutputStream());  
HSSFWorkbook workbook = new HSSFWorkbook();     
String filename = "path/file.xls" ;    
String filename ="path/data.xls";
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=filename.xls"); 
HSSFWorkbook hwb = new HSSFWorkbook();    
HSSFSheet sheet = hwb.createSheet("sheet");    
HSSFRow rowhead = sheet.createRow((short)0);   
rowhead.createCell((short) 0).setCellValue("Name"); //excel sheet row headings
rowhead.createCell((short) 1).setCellValue("Month);    
rowhead.createCell((short) 2).setCellValue("wdays");                       
int k=0;                       
int j = k+1;                  
HSSFRow row = sheet.createRow((short)j);                   
row.createCell((short) 0).setCellValue(ename);   //excel sheet row values                
row.createCell((short) 1).setCellValue(emonth);                    
row.createCell((short) 2).setCellValue(wdays);    
FileOutputStream fileOut = new FileOutputStream(filename);   
hwb.write(fileOut);    
hwb.write(response.getOutputStream()); // Write workbook to response.
response.getOutputStream().flush();    
fileOut.close();    
response.getOutputStream().close();
}               

catch(Exception e)              
{
 e.printStackTrace();             
}
}
}

 

By Sri

Leave a Reply

Your email address will not be published. Required fields are marked *