0

I am exporting data to an Excel sheet in JSP.

<%@page import="java.io.*"%> <%@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"%> <% String inj1=request.getParameter("inj"); String ob=request.getParameter("ob"); HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("Excel Sheet"); HSSFRow rowhead = sheet.createRow((short)0); rowhead.createCell((short)0).setCellValue("Injections"); rowhead.createCell((short)1).setCellValue("OB"); HSSFRow row = sheet.createRow((short)1); row.createCell((short)0).setCellValue(inj1); row.createCell((short)1).setCellValue(ob); FileOutputStream fileOut = new FileOutputStream("c:\\Injection_Details.xls"); wb.write(fileOut); fileOut.close(); out.println("Data is saved in excel file."); %> 

I am getting errors as

HSSFWorkbook cannot be resolved to a type

or

Only a type can be imported. org.apache.poi.hssf.usermodel.HSSFSheet resolves to a package.

How is this caused and how can I solve it?

1
  • Don't put code like this in your JSP. It belongs in plain Java Code. If you aren't using it already pick up a proper Java Framework, for example Spring MVC. If you already use one, learn to use it properly. Commented Jun 19, 2014 at 7:09

2 Answers 2

1

HSSFWorkbook cannot be resolved to a type

That's just a compilation error. The mentioned class is missing in the classpath. In this particular case, you need to ensure that you've dropped the necessary JAR file(s) of Apache POI HSSF in your webapp's /WEB-INF/lib folder (which is part of the webapp's classpath).

This problem has nothing to do with JSPs. You would have exactly the same problem in a normal Java class where all that Java code actually belongs.

Sign up to request clarification or add additional context in comments.

1 Comment

But I tried this code and it ran successfully but the problem is that if I access database values and then try as given in this code then it wont work. Why I unable to understand. So still do you think that its a problem with the missing jar file.
0

A simple Excel export without any additional libs:

  1. Write your ResultSet out as comma-delimited data in the JSP
  2. Set the ContentType on the response to Excel.

JSP Code; note the absence of line breaks in the JSP as those will mess up the CSV to Excel translation:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><% response.setContentType("application/vnd.ms-excel");response.setHeader("Content-disposition","attachment;filename=export.csv"); %> message_name,subject,sent_count,open_count,original_href,link_uuid,link_click_count <c:forEach var="item" items="${myResultSet}">"<c:out value="${item.field1}" />","<c:out value="${item.field2}" />","<c:out value="${item.field3}" />" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.