gpt4 book ai didi

java - 如何使用jsp显示excel避免 "No input source supplied to the exporter"。?

转载 作者:行者123 更新时间:2023-11-30 02:58:25 24 4
gpt4 key购买 nike

我编写了一些代码来在网站中显示 Excel。

但是我遇到了这个异常

net.sf.jasperreports.engine.JRRuntimeException: No input source supplied to the exporter.

我的代码

在jsp中导入

<%@ page import="java.io.*"%>
<%@ page import="java.sql.Connection"%>
<%@ page import="java.sql.DriverManager"%>
<%@ page import="java.util.HashMap"%>
<%@ page import="java.util.Map"%>
<%@ page import="net.sf.jasperreports.engine.*"%>
<%@ page import="java.io.ByteArrayOutputStream"%>
<%@ page import="net.sf.jasperreports.view.JasperViewer"%>
<%@ page import="net.sf.jasperreports.engine.export.*"%>

导出到excel的jsp代码

        <%
Connection conn = null;
String no1 = request.getParameter("no1");
String no2 = request.getParameter("no2");

System.out.println("get value " + no1 + " " +no2);
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ams2"
,"root","passwd1234");
File reportFile = new File (application.getRealPath("//jasper//report//Blank_A4_2.jasper"));
Map parameters = new HashMap();

parameters.put("no1",no1);
parameters.put("no2",no2);
System.out.println("123 "+parameters);

ByteArrayOutputStream xlsReport = new ByteArrayOutputStream();
JRXlsExporter exporter = new JRXlsExporter();
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, xlsReport);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE, "C:\\");
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "sample.xls");
exporter.exportReport();

byte bytes[] = new byte[10];
bytes = xlsReport.toByteArray();
response.setContentType("application/vnd.ms-excel");

response.setContentLength(bytes.length);
xlsReport.close();
ServletOutputStream outStream = response.getOutputStream();
outStream.write(bytes,0,bytes.length);
outStream.flush();
outStream.close();

} catch (Exception ex) {
out.println("Error " + ex);
}
%>

如何解决这个问题?

最佳答案

如果您使用的不是非常旧的 JasperReports 版本,那么您正在使用已弃用的方法,最重要的是您没有将 JasperPrint 传递给导出器。

No input source supplied to the exporter.

您需要使用JasperFillManager.fillReport填写报告

示例代码(jasper report v5 或更高版本)

JasperDesign jasperDesign = JRXmlLoader.load(new FileInputStream(reportFile));
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperDesign, parameters, conn);

JRXlsExporter exporter = new JRXlsExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint)); //The JasperPrint, filled report
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(xlsReport)); //Your ByteArrayOutputStream

SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
configuration.setOnePagePerSheet(true);
configuration.setDetectCellType(true);
configuration.set //The other properties you like to set
exporter.setConfiguration(configuration);

exporter.exportReport();

关于java - 如何使用jsp显示excel避免 "No input source supplied to the exporter"。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36615470/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com