iReport 編輯的報表,在jsp中匯出到PDF的代碼

來源:互聯網
上載者:User

 

此處使用了Java BeanCollection作為DataSource。

 

JSP頁面代碼:

 

<%@ page errorPage="error.jsp" %>
<%@ page import="reportDatasource.*" %>
<%@ page import="net.sf.jasperreports.engine.*" %>
<%@ page import="net.sf.jasperreports.engine.util.*" %>
<%@ page import="net.sf.jasperreports.engine.export.*" %>
<%@ page import="net.sf.jasperreports.j2ee.servlets.*" %>
<%@ page import="java.util.*" %><%@ page import="java.io.*" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="javax.servlet.*" %>
<%@page language="java" contentType="application/x-msdownload"    pageEncoding="UTF-8"%>
<%
      String requestFileName = "LineReport";//報表的名稱
      String reportFileName = requestFileName +".jasper";
      String fileDownName = "XXX報表";
      Map parameters = new HashMap();
     // parameters.put("ReportTitle", "Address Report"); //向報表傳遞參數   
      JasperPrint jasperPrint = JavaBeanListDataSoureFactory.createJasperPrintByJavaBeanList(reportFileName, parameters, LocationValueBeanFactory.getPageLoadTimeHistoryLineReportBeanCollection());        
    
       //輸出
       PathClassPath classpath = new PathClassPath();
      String stringpath =  application.getRealPath("/reports/"+reportFileName);
     
      System.out.print("reportFileName="+stringpath);
    
      File sourceFile  =new File(stringpath);
      long start = System.currentTimeMillis();
    
      System.err.println("PDF creation time : " + (System.currentTimeMillis() - start));

      //下載
      response.reset();//
      response.setContentType("application/x-download");   
      String filedisplay = fileDownName+".pdf";
      filedisplay = URLEncoder.encode(filedisplay,"UTF-8");
      response.addHeader("Content-Disposition","attachment;filename=" + filedisplay);

      OutputStream outp = null;
    
      try
      {
          outp = response.getOutputStream();
      
           JasperExportManager.exportReportToPdfStream(jasperPrint, outp);
           
          outp.flush();
      }
      catch(Exception e)
      {
          System.out.println("Error!");
          e.printStackTrace();
      }
      finally
      {

          if(outp != null)
          {
              outp.close();
              outp = null;
          }
          //如下處理 getOutputStream() has already been called for this response 異常
      
          out.clear();
          out = pageContext.pushBody();
      }
%>

 

 

 其中:

LocationValueBeanFactory.getPageLoadTimeHistoryLineReportBeanCollection() 

為返回一個實體的數組的List:

 Arrays.asList( LocationValueBean [] data); 

 

 其中:

 JavaBeanListDataSoureFactory.createJasperPrintByJavaBeanList ()

代碼為:

 //本類可能會用到的包:

 //

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JRRuntimeException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.j2ee.servlets.*;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
//

 

 //

       public static JasperPrint  createJasperPrintByJavaBeanList(String reportFileName,Map parameters,Collection collection)
        {
           
            String path = "";
            Properties prop = new Properties();
            try {
                InputStream in = JavaBeanListDataSoureFactory.class.getClassLoader().getResourceAsStream("report.properties");
                prop.load(in);
                path = prop.getProperty("site.reportjasper.path");
                in.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            JRHtmlExporter exporter = new JRHtmlExporter();
            JasperPrint jasperPrint = null;
             //PathClassPath classpath = new PathClassPath();
            //ReadConfigInfo conf = new ReadConfigInfo();
             String reportFileNameResult = path+reportFileName;
            File reportFile = new File(reportFileNameResult);
            if (!reportFile.exists())
                throw new JRRuntimeException("File "+reportFileName+" not found. The report design must be compiled first.");
            parameters.put("ReportTitle", "Address Report");
            parameters.put("BaseDir", reportFile.getParentFile());
            try {
                
                jasperPrint = 
                    JasperFillManager.fillReport(
                        reportFileNameResult, 
                        parameters, 
                        new JRBeanCollectionDataSource(collection)
                        );
                
            } catch (JRException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return jasperPrint;
        }

 

 

PS:部落格園中的插入代碼依然有問題啊,有的時候最上面一行會從代碼塊中跑出來,有的時候最下面一行會跑出來,我的環境:Win7 x64+Chrome 21.01180.89

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.