如何使用jsp显示excel避免“没有提供给导出器的输入源”。 [英] How to show excel using jsp avoiding "No input source supplied to the exporter".?

查看:117
本文介绍了如何使用jsp显示excel避免“没有提供给导出器的输入源”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一些代码来在网站上显示excel。

I have written some code to show excel in a website.

但是我得到了这个例外

net.sf.jasperreports.engine.JRRuntimeException:没有输入源
提供给导出器。

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 传递给出口商。

If you are not using a very old version of JasperReports, you are using deprecated methods and most importantly you are not passing the JasperPrint to the exporter.


没有提供给出口商的输入源。

No input source supplied to the exporter.

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

示例代码(jasper报告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();

这篇关于如何使用jsp显示excel避免“没有提供给导出器的输入源”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆