Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Jsp Jsp to Excel

Related Tutorials


 
 

Share on Google+Share on Google+

Jsp to Excel

Advertisement
In this section you will learn how to create an excel file and write data into it using jsp.

Jsp to Excel

        

In this section you will learn how to create an excel file and write data into it using jsp. For this, you have to import the package org.apache.poi.hssf.usermodel.* in order to use the classes HSSFSheet, HSSFCell, HSSFRow, HSSFWorkbook. The class HSSFRow allow us to create a row  in which we can set the cells and in order to create a new cell and add the values into cells, we have used the class HSSFCell.

createSheet("sheet") : This method is used to create a HSSFSheet.

createRow((short)value) : This method is used to create a new row. We have used ((short)0) as a value for creating 0th row.

createCell((short)value) : This method is used to create a new cell. We have used ((short)0) as a value for creating 0th cell.

setCellValue(values) : This method is used to add the value into the cell. You can add value of any data type into cell by using this method.

 

Here is the code of excel.jsp

<%@ page import="org.apache.poi.hssf.usermodel.HSSFSheet"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFCell"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFRow"%>
<%@ page import="java.io.*" %>
<%try{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(" sheet");
HSSFRow row = sheet.createRow((short)0);
HSSFCell cell = row.createCell((short)0);
cell.setCellValue(1);
row.createCell((short)1).setCellValue("Anusmita Singh");
row.createCell((short)2).setCellValue("Delhi");
row.createCell((short)3).setCellValue("anu@roseindia.net");
FileOutputStream fileOut = new FileOutputStream("c:\\data.xls");
wb.write(fileOut);
fileOut.close(); 
}catch ( Exception ex ){ 

%>

The excel sheet is created in the C drive with the specified data:

Download Source Code:

 

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 12, 2009

Related Tutorials

Discuss: Jsp to Excel   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
sha
April 7, 2012
jsp excel error

While trying to run jsp file i am getting below error, please guide me: org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 6 in the generated java file Only a type can be imported. org.apache.poi.hssf.usermodel.HSSFSheet resolves to a package An error occurred at line: 7 in the generated java file Only a type can be imported. org.apache.poi.hssf.usermodel.HSSFWorkbook resolves to a package An error occurred at line: 8 in the generated java file Only a type can be imported. org.apache.poi.hssf.usermodel.HSSFCell resolves to a package An error occurred at line: 9 in the generated java file Only a type can be imported. org.apache.poi.hssf.usermodel.HSSFRow resolves to a package An error occurred at line: 8 in the jsp file: /exceldemo.jsp HSSFWorkbook cannot be resolved to a type 5: <%@ page import="java.io.*" %> 6: <% 7: try { 8: HSSFWorkbook wb = new HSSFWorkbook(); 9: HSSFSheet sheet = wb.createSheet("sheet"); 10: HSSFRow row = sheet.createRow((short)0); 11: HSSFCell cell = row.createCell((short)0); An error occurred at line: 8 in the jsp file: /exceldemo.jsp HSSFWorkbook cannot be resolved to a type 5: <%@ page import="java.io.*" %> 6: <% 7: try { 8: HSSFWorkbook wb = new HSSFWorkbook(); 9: HSSFSheet sheet = wb.createSheet("sheet"); 10: HSSFRow row = sheet.createRow((short)0); 11: HSSFCell cell = row.createCell((short)0); An error occurred at line: 9 in the jsp file: /exceldemo.jsp HSSFSheet cannot be resolved to a type 6: <% 7: try { 8: HSSFWorkbook wb = new HSSFWorkbook(); 9: HSSFSheet sheet = wb.createSheet("sheet"); 10: HSSFRow row = sheet.createRow((short)0); 11: HSSFCell cell = row.createCell((short)0); 12: cell.setCellValue(1); An error occurred at line: 10 in the jsp file: /exceldemo.jsp HSSFRow cannot be resolved to a type 7: try { 8: HSSFWorkbook wb = new HSSFWorkbook(); 9: HSSFSheet sheet = wb.createSheet("sheet"); 10: HSSFRow row = sheet.createRow((short)0); 11: HSSFCell cell = row.createCell((short)0); 12: cell.setCellValue(1); 13: row.createCell((short)1).setCellValue("Anusmita Singh"); An error occurred at line: 11 in the jsp file: /exceldemo.jsp HSSFCell cannot be resolved to a type 8: HSSFWorkbook wb = new HSSFWorkbook(); 9: HSSFSheet sheet = wb.createSheet("sheet"); 10: HSSFRow row = sheet.createRow((short)0); 11: HSSFCell cell = row.createCell((short)0); 12: cell.setCellValue(1); 13: row.createCell((short)1).setCellValue("Anusmita Singh"); 14: row.createCell((short)2).setCellValue("Delhi"); Stacktrace: org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:93) org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330) org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:451) org.apache.jasper.compiler.Compiler.compile(Compiler.java:319) org.apache.jasper.compiler.Compiler.compile(Compiler.java:298) org.apache.jasper.compiler.Compiler.compile(Compiler.java:286) org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:565) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:309) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259) javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
Aniket
October 7, 2012
Above script haven't worked ....please help

Got following errors org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 14 in the generated java file Only a type can be imported. org.apache.poi.hssf.usermodel.HSSFSheet resolves to a package An error occurred at line: 15 in the generated java file Only a type can be imported. org.apache.poi.hssf.usermodel.HSSFWorkbook resolves to a package An error occurred at line: 16 in the generated java file Only a type can be imported. org.apache.poi.hssf.usermodel.HSSFCell resolves to a package An error occurred at line: 17 in the generated java file Only a type can be imported. org.apache.poi.hssf.usermodel.HSSFRow resolves to a package An error occurred at line: 7 in the jsp file: /excel.jsp HSSFWorkbook cannot be resolved to a type 4: <%@ page import="org.apache.poi.hssf.usermodel.HSSFRow"%> 5: <%@ page import="java.io.*" %> 6: <%try{ 7: HSSFWorkbook wb = new HSSFWorkbook(); 8: HSSFSheet sheet = wb.createSheet("sheet"); 9: HSSFRow row = sheet.createRow((short)0); 10: HSSFCell cell = row.createCell((short)0); An error occurred at line: 7 in the jsp file: /excel.jsp HSSFWorkbook cannot be resolved to a type 4: <%@ page import="org.apache.poi.hssf.usermodel.HSSFRow"%> 5: <%@ page import="java.io.*" %> 6: <%try{ 7: HSSFWorkbook wb = new HSSFWorkbook(); 8: HSSFSheet sheet = wb.createSheet("sheet"); 9: HSSFRow row = sheet.createRow((short)0); 10: HSSFCell cell = row.createCell((short)0); An error occurred at line: 8 in the jsp file: /excel.jsp HSSFSheet cannot be resolved to a type 5: <%@ page import="java.io.*" %> 6: <%try{ 7: HSSFWorkbook wb = new HSSFWorkbook(); 8: HSSFSheet sheet = wb.createSheet("sheet"); 9: HSSFRow row = sheet.createRow((short)0); 10: HSSFCell cell = row.createCell((short)0); 11: cell.setCellValue(1); An error occurred at line: 9 in the jsp file: /excel.jsp HSSFRow cannot be resolved to a type 6: <%try{ 7: HSSFWorkbook wb = new HSSFWorkbook(); 8: HSSFSheet sheet = wb.createSheet("sheet"); 9: HSSFRow row = sheet.createRow((short)0); 10: HSSFCell cell = row.createCell((short)0); 11: cell.setCellValue(1); 12: row.createCell((short)1).setCellValue("Anusmita Singh"); An error occurred at line: 10 in the jsp file: /excel.jsp HSSFCell cannot be resolved to a type 7: HSSFWorkbook wb = new HSSFWorkbook(); 8: HSSFSheet sheet = wb.createSheet("sheet"); 9: HSSFRow row = sheet.createRow((short)0); 10: HSSFCell cell = row.createCell((short)0); 11: cell.setCellValue(1); 12: row.createCell((short)1).setCellValue("Anusmita Singh"); 13: row.createCell((short)2).setCellValue("Delhi"); Stacktrace: org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102) org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:331) org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:469) org.apache.jasper.compiler.Compiler.compile(Compiler.java:378) org.apache.jasper.compiler.Compiler.compile(Compiler.java:353) org.apache.jasper.compiler.Compiler.compile(Compiler.java:340) org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
DMCA.com