是否可以将 jasper 报告与 java 一起使用,使其像浏览器 window 中的弹出窗口一样?

Is it possible to use jasper report with java to make it like a popup coming in the browser window?

我正在使用 java 的 Jasper 报告。我想让导出文件在作为 Web 应用程序启动时询问下载位置。

我现在做的是。我正在为要在某个位置导出的文件(PDF、DOCX、XLS)提供路径。我需要的是,浏览器应该像 firefox 一样弹出并询问要下载的文件位置。

我已经在 java 应用程序中完成了,方法是提供路径,

Connection conn = null;
    ResultSet rs = null;
    JasperReport jasperReport = null;
    JasperPrint print = null;
    String filename = "Report.pdf";
    String query = "{CALL get_report_data()}";
    try {
        if (conn == null) 
        {
            String hostName = "localhost";
            String dbName = "test";
            String userName = "root";
            String password = "root";
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                String connectionURL = "jdbc:mysql://" + hostName + ":3306/" + dbName;
                conn = DriverManager.getConnection(connectionURL, userName, password);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
        CallableStatement cstmt = conn.prepareCall(query);

        rs = cstmt.executeQuery();

        JRResultSetDataSource resultSetDataSource = new JRResultSetDataSource(rs);

        jasperReport = JasperCompileManager
                .compileReport("E:\Eclipse 2019-03 Workspace\Report\static_land_report.jrxml");

        Map<String, Object> parameters = new HashMap<String, Object>();

        print = JasperFillManager.fillReport(jasperReport, parameters, resultSetDataSource);

JasperExportManager.exportReportToPdfFile(打印,"E:\Eclipse 2019-03 Workspace\Report\static_land_report.pdf");

现在,我希望在网络应用程序中完成上述代码,并且不提供路径。

按照下面的方法试一试 steps.I 从 jaspersoft community 找到的。

  • 转到 iReport 中的存储库选项卡

  • Select 保存输入控件的文件夹

  • 右击文件夹,select添加->inputcontrol

  • 在 id 字段中,输入您的参数名称(它必须与 $P{}

  • 之间的值完全相同
  • 给它一个提示名称

  • 输入控件详情页签,select单个值,select数据类型(在iReport中可以用同样的方式创建数据类型)

  • 然后转到您的报表文件夹,在输入控件映射上右键单击并 select link 一个现有的输入控件,然后 select 你刚刚创建的输入控件。

感谢您与我们联系。但是,我找到了解决办法。

package com.report.java;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;

public class PdfReportDownload extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public PdfReportDownload() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try 
        {
            generatePdfReport(response);
        } 
        catch (Exception e) {
            e.printStackTrace();
        }

    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    public void generatePdfReport(HttpServletResponse response) 
    {
        try 
        {
            Connection conn=null;
            ResultSet rs=null;
            JasperReport jasperReport = null;
            String query ="{CALL get_report_data()}";
            try 
            {   
                if(conn==null)
                {
                    try 
                    {
                        conn= MySQLConnection.getConnection();

                    } 
                    catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }   
            CallableStatement cstmt = conn.prepareCall(query);

            rs = cstmt.executeQuery();

            JRResultSetDataSource resultSetDataSource = new JRResultSetDataSource(rs);  

            jasperReport = JasperCompileManager.compileReport("E:\Eclipse 2019-03 Workspace\Report_Download\static_land_report.jrxml");
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap <String, Object>(), resultSetDataSource);

            JRPdfExporter pdfExporter = new JRPdfExporter();
            pdfExporter.setExporterInput(new SimpleExporterInput(jasperPrint));
            ByteArrayOutputStream pdfReportStream = new ByteArrayOutputStream();
            pdfExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(pdfReportStream));
            pdfExporter.exportReport();

            response.setContentType("application/pdf");
            response.setHeader("Content-Length", String.valueOf(pdfReportStream.size()));
            response.addHeader("Content-Disposition", "attachment; filename=Report.pdf;");

            OutputStream responseOutputStream = response.getOutputStream();
            responseOutputStream.write(pdfReportStream.toByteArray());
            responseOutputStream.close();
            pdfReportStream.close();
        } 
        catch (Exception e) 
        {
                System.out.println(e);
        }
        }
        catch(Exception e) 
        {
            System.out.println(e);
        }
    }

}

调用此 servlet 时,报告会下载