2 回答

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超5個(gè)贊
按照以下步驟嘗試一下。
轉(zhuǎn)到 iReport 中的存儲(chǔ)庫選項(xiàng)卡
選擇保存輸入控件的文件夾
右鍵單擊該文件夾,選擇添加 -> inputcontrol
在 id 字段中,輸入您的參數(shù)名稱(必須與 $P{} 之間的值完全相同)
給它一個(gè)提示名稱
在輸入控件詳細(xì)信息選項(xiàng)卡中,選擇單個(gè)值,然后選擇數(shù)據(jù)類型(您可以在 iReport 中以相同的方式創(chuàng)建數(shù)據(jù)類型)
然后轉(zhuǎn)到報(bào)表文件夾,在輸入控制圖上右鍵單擊并選擇鏈接現(xiàn)有輸入控件,然后選擇剛剛創(chuàng)建的輸入控件。

TA貢獻(xiàn)1840條經(jīng)驗(yàn) 獲得超5個(gè)贊
但是,我找到了解決方案。
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);
}
}
}
調(diào)用此 servlet 時(shí),將下載報(bào)告
添加回答
舉報(bào)