1. 程式人生 > >iReport製作EXCEL、PDF或者HTML檔案

iReport製作EXCEL、PDF或者HTML檔案

公司用到了iReport製作報表,正好,畢業論文上也得用到報表,也就稍稍學習了下。個人感覺還是有個人教你的話,這個入門的最佳捷徑。

二、這個iReport的使用教程就不用我教了吧,自個搜個教程去,呵呵。

三、SSH框架與iReport整合,採用的是將iReport編譯產生的jsper檔案放在SRC目錄下。

四、與jasperreport整合所需要的jar包有這些:jasperreports-5.1.2.jar、commons-digester-2.1.jar、groovy-all-2.0.1.jar、poi-3.7-20101029.jar等等

五、我將生成EXCEL、PDF或者HTML檔案的方法放在ReportServiceImpl類中,程式碼如下:

        public static Connection conn = DButil.getConnection();
	/**
	 * 匯出為html檔案
	 * @param response
	 */
	@Override
	public void exportHtml(HttpServletResponse response){
		
		try {
			response.setCharacterEncoding("UTF-8");  
			String path = ReportServiceImpl.class.getClassLoader().getResource("excelDemo.jasper").getPath();
			JasperReport jasperReport =(JasperReport)JRLoader.loadObject(path);
			JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null,conn);
			JRHtmlExporter exporter = new JRHtmlExporter();
			
			exporter.setParameter(JRHtmlExporterParameter.JASPER_PRINT, jasperPrint);
			exporter.setParameter(JRHtmlExporterParameter.OUTPUT_WRITER,response.getWriter());
			exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,Boolean.FALSE);
			exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "utf-8");
			exporter.exportReport();
		} catch (JRException e) {
			e.printStackTrace();
		} catch (IOException e){
			e.printStackTrace();
		}
	}

	/**
	 * 匯出為excel檔案
	 * @param response
	 */
	@Override
	public void exportExcel( HttpServletResponse response){
		try {
			response.setCharacterEncoding("UTF-8");  
			
			String path = ReportServiceImpl.class.getClassLoader().getResource("excelDemo.jasper").getPath();
			JasperReport jasperReport= (JasperReport)JRLoader.loadObject(path);
			JasperPrint jasperPrint=JasperFillManager.fillReport(jasperReport,null,conn);
			JRXlsExporter exporter=new JRXlsExporter();
			exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
			exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, response.getOutputStream());
			exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
			exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);

			response.setHeader("Content-Disposition", "attachment;filename=first.xls");
			response.setContentType("application/vnd_ms-excel");
			exporter.exportReport();
		} catch (JRException e) {
			e.printStackTrace();
		} catch (IOException e){
			e.printStackTrace();
		} 
	}
	
	
	/**
	 * 匯出為pdf檔案
	 * @param response
	 */
	@Override
	public void exportPdf(HttpServletResponse response) {
		try {
			String path = ReportServiceImpl.class.getClassLoader().getResource("excelDemo.jasper").getPath();
			JasperReport jasperReport= (JasperReport)JRLoader.loadObject(path);
			JasperPrint jasperPrint=JasperFillManager.fillReport(jasperReport,null,conn);
			JRPdfExporter exporter = new JRPdfExporter();  
			exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
			exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
			
			response.setHeader("Content-Disposition", "attachment;filename=first.pdf");
			response.setContentType("application/pdf");
			response.setCharacterEncoding("UTF-8");  
			exporter.exportReport();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


六、所遇到的問題

1.首先老是提示缺少類的問題,不過,下載了這個完整的jar包的就不會有這樣的問題了。

1.在匯出PDF時,出現了問題:無法顯示中文,有中文的地方都顯示為空白。

   解決辦法:

七、參考資料