2 回答
TA贡献1811条经验 获得超5个赞
按照以下步骤尝试一下。
转到 iReport 中的存储库选项卡
选择保存输入控件的文件夹
右键单击该文件夹,选择添加 -> inputcontrol
在 id 字段中,输入您的参数名称(必须与 $P{} 之间的值完全相同)
给它一个提示名称
在输入控件详细信息选项卡中,选择单个值,然后选择数据类型(您可以在 iReport 中以相同的方式创建数据类型)
然后转到报表文件夹,在输入控制图上右键单击并选择链接现有输入控件,然后选择刚刚创建的输入控件。
TA贡献1840条经验 获得超5个赞
但是,我找到了解决方案。
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 时,将下载报告
添加回答
举报