Uploading Excel File Using Jsp
By Ramakrishna on Feb 9, 2009 in Upload Excel sheet using jsp/java
There are two choice for reading and writing the Excel file in your application.
1.By Using the JExcelapi
2.By Jakarta’s Poi.jar which uses the HSSFSheet read and write.
–> Using the JExcelapi which is not suitable for the important data. It fails to read several files and in most occasions it also fails to create cells. In short JExcelAPI isn’t suitable for enterprise use.
–>Where as the POI.jar is a Jakarta Project’s which was developed by pure Java implementation of the Excel file format. It is a mature product and was able to correctly and effortlessly read excel data generated from various sources, including non-MS Excel products like Open Office, and for various versions of Excel. so for this reasons it is Highly recommended.
Before Using this Code you have to download the poi.jar file and place it in the lib folder of your application (or) place in the jar folder and give the path in the environment variables in the My Computers, and then import the HSSF files and then follow the code
<%@ page import = “java.io.*” %>
<%@ page import = “java.sql.*” %>
<%@ page import = “javax.servlet.*” %>
<%@ page import = “javax.servlet.http.*” %>
<%@ page import = “java.util.*” %>
<%@ page import = “java.awt.*” %>
<%@ page import = “java.io.File” %>
<%@ page import = “java.io.IOException” %>
<%@ page import = “javax.swing.filechooser.FileSystemView” %>
<%@ page import=”org.apache.poi.hssf.usermodel.HSSFSheet”%>
<%@ page import=”org.apache.poi.hssf.usermodel.HSSFWorkbook”%>
<%@ page import=”org.apache.poi.hssf.usermodel.HSSFRow”%>
<%@ page import=”org.apache.poi.hssf.usermodel.HSSFCell”%>
<%
//Here Datedas is the value of the length which is retrived from the database in my application
String fname1=”File”-”+Datedas+”.xls”;
//Giving name to the file
//creating a file object
File fname=new File(fname1);
//Creating an object to the HSSF Class
HSSFWorkbook hwb = new HSSFWorkbook();
//Giving the name to the Excel file which will be created by this HSSFsheet
HSSFSheet sheet = hwb.createSheet(”stock sheet”);
//Creating the row for your data
HSSFRow pw = sheet.createRow((short)2);
//Mention the Heading of your Excel sheet
pw.createCell((short) 0).setCellValue(”S.No”);
pw.createCell((short) 1).setCellValue(”Heading1″);
pw.createCell((short) 2).setCellValue(”Heading2“);
pw.createCell((short) 3).setCellValue(”Heading3“);
pw.createCell((short) 4).setCellValue(”Heading4“);
pw.createCell((short) 5).setCellValue(”Heading5“);
pw.createCell((short) 6).setCellValue(”Heading6“);
pw.createCell((short) 7).setCellValue(”Heading7“);
pw.createCell((short) 8).setCellValue(”Heading8“);
pw.createCell((short) 9).setCellValue(”Heading9“);
pw.createCell((short) 10).setCellValue(”Heading10“);
pw.createCell((short) 11).setCellValue(”Heading11″);
//String stored1=”",stored2=”",stored3=”",stored4=”",stored5=”",stored6=”",stored7=”",stored9=”";
//float stored8=0.0f,stored10=0.0f,stored12=0.0f;
int comprk=1;
int index=3;
//int compno=0;
int sno=0;
while(rs.next())
{
compno=1;
sno++;
System.out.println(”Testing in the loop “+compno);
//Creating the Excel Sheet Rows
HSSFRow row = sheet.createRow((short)index);
row.createCell((short) 0).setCellValue(sno);
row.createCell((short) .setCellValue(Integer.parseInt(rs.getString(1)));
row.createCell((short) 2).setCellValue(rs.getString(2));
row.createCell((short) 3).setCellValue(rs.getString(3));
row.createCell((short) 4).setCellValue(rs.getString(4));
row.createCell((short) 5).setCellValue(rs.getString(5));
row.createCell((short) 6).setCellValue(rs.getString(6));
row.createCell((short) 7).setCellValue(rs.getString(7));
row.createCell((short) 8).setCellValue(rs.getFloat(8));
row.createCell((short) 9).setCellValue(rs.getString(9));
row.createCell((short) 10).setCellValue(rs.getFloat(10));
row.createCell((short) 11).setCellValue(rs.getFloat(13));
index++;
}
FileOutputStream fileOut = new FileOutputStream(fname);
hwb.write(fileOut);
fileOut.flush();
fileOut.close();
out.println(”<b>Your excel file has been Successfully generated</b>”);
%>
