如何将JSP中的数据导出到Excel表格中?

发布于 2024-12-04 20:51:19 字数 1205 浏览 0 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以为你会在 2024-12-11 20:51:19

HSSFWorkbook 无法解析为类型

这只是一个编译错误。类路径中缺少提到的类。在这种特殊情况下,您需要确保已删除 Apache 所需的 JAR 文件POI HSSF 位于 Web 应用程序的 /WEB-INF/lib 文件夹(它是 Web 应用程序类路径的一部分)中。

这个问题与JSP 无关。在所有 Java 代码实际上所属的普通 Java 类中,您会遇到完全相同的问题。

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.

未蓝澄海的烟 2024-12-11 20:51:19

无需任何附加库的简单 Excel 导出:

  1. 在 JSP 中将 ResultSet 写为逗号分隔数据
  2. 在对 Excel 的响应中设置 ContentType。

JSP代码;请注意 JSP 中没有换行符,因为换行符会弄乱 CSV 到 Excel 的转换:

<%@ 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}" />"

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}" />"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文