Jasper模板導出爲Excel、PDF

/**  
 * @Description: 將模板導出爲Excel
 * @author: 
 * @date: 2018年3月22日 下午8:57:24
 * @param pagename 模板文件名稱
 * @param params 導出參數
 * @param javaBean 報表數據源實體類
 * @param request 請求對象
 * @param response 響應對象
 * @throws JRException Jasper異常
 * @throws IOException IO異常
 */
@Override
public void exportTemplateToExcel(String pagename, Map<String, Object> params, Object javaBean,
      HttpServletRequest request, HttpServletResponse response) throws JRException, IOException {
   List<Object> list = new ArrayList<Object>();
   list.add(javaBean);
   // 獲取模板
   JasperReport jasperReport = getJasperReport(pagename);
   // 建立數據源
   JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);
   // 設置jasper文件夾的路徑
   params = setJasperPath(params);
   // 填充模版
   JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
   // 生成文件名
   String fileName = UUID.randomUUID().toString().toUpperCase() + ".xlsx";
   // 資源文件完整路徑
   String completePath = getPdfUrl(request, fileName);
   // 服務器文件臨時路徑
   String templatePath = getTempFoleder() + "/" + fileName;
   // 設置response參數,用於打開下載頁面
   response.reset();
   response.setContentType("application/x-msdownload;");
   response.setHeader("Content-Disposition",
         "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO-8859-1"));
   // 建立Jasper Excel導出類
   JRXlsxExporter exporter = new JRXlsxExporter();
   // 設置導出配置項
   SimpleXlsxReportConfiguration conf = new SimpleXlsxReportConfiguration();
   conf.setWhitePageBackground(false);
   conf.setDetectCellType(true);
   exporter.setConfiguration(conf);
   // 設置輸入項
   ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
   exporter.setExporterInput(exporterInput);
   // 設置輸出項
   OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(templatePath);
   exporter.setExporterOutput(exporterOutput);
   //導出報表
   exporter.exportReport();
   // 重定向到資源文件url
   response.sendRedirect(completePath);
   exporterOutput.close();
}

   /**
    * 導出模板爲PDF文件
    * @param pagename
    * @param params
    * @param javaBean
    * @param request
    * @param response
    * @throws JRException
    * @throws IOException
    */
   @Override
   public void exportTemplateToPdf(String pagename, Map<String, Object> params, Object javaBean,
                                   HttpServletRequest request, HttpServletResponse response) throws JRException, IOException {
       List<Object> list = new ArrayList<Object>();
       list.add(javaBean);
       // 獲取模板
       JasperReport jasperReport = getJasperReport(pagename);
       // 建立數據源
       JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);
       // 設置jasper文件夾的路徑
       params = setJasperPath(params);
       // 填充模版
       JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
       // 生成文件名
       String fileName = UUID.randomUUID().toString().toUpperCase() + ".pdf";
       // 資源文件完整路徑
       String completePath = getPdfUrl(request, fileName);
       // 服務器文件臨時路徑
       String templatePath = getTempFoleder() + "/" + fileName;
       // 設置response參數,用於打開下載頁面
       response.reset();
       response.setContentType("application/pdf;");
       response.setHeader("Content-Disposition",
           "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO-8859-1"));
       //定義報表輸出源
       JRPdfExporter exporter = new JRPdfExporter();
       // 設置輸入項
       ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
       exporter.setExporterInput(exporterInput);
       // 設置輸出項
       OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(templatePath);
       exporter.setExporterOutput(exporterOutput);
       //導出報表
       exporter.exportReport();
       // 重定向到資源文件url
       response.sendRedirect(completePath);
       exporterOutput.close();
   }
相關文章
相關標籤/搜索