如何将JSP中的数据导出到Excel表格中?
我正在 JSP 中将数据导出到 Excel 工作表。
<%@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.");
%>
我收到错误如下
HSSFWorkbook 无法解析为类型
或
只能导入一种类型。 org.apache.poi.hssf.usermodel.HSSFSheet 解析为一个包。
这是如何引起的以及如何解决?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这只是一个编译错误。类路径中缺少提到的类。在这种特殊情况下,您需要确保已删除 Apache 所需的 JAR 文件POI HSSF 位于 Web 应用程序的
/WEB-INF/lib
文件夹(它是 Web 应用程序类路径的一部分)中。这个问题与JSP 无关。在所有 Java 代码实际上所属的普通 Java 类中,您会遇到完全相同的问题。
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.
无需任何附加库的简单 Excel 导出:
ResultSet
写为逗号分隔数据JSP代码;请注意 JSP 中没有换行符,因为换行符会弄乱 CSV 到 Excel 的转换:
A simple Excel export without any additional libs:
ResultSet
out as comma-delimited data in the JSPJSP Code; note the absence of line breaks in the JSP as those will mess up the CSV to Excel translation: