struts2+jasperReport生成各种形式的报表
/** * 導出html形式報表 * @param request 請求對象 * @param response 響應對象 * @param parameters 設置報表的參數 * @param jrxmlFilePath 報表的jrxml文件路徑 * @param jasperFilePath 報表的jasper文件路徑 * @param resultList 數據列表(List),用於構造數據源 */@SuppressWarnings("unchecked")public static void exportHtmlReport(HttpServletRequest request, HttpServletResponse response, Map parameters, String jrxmlFilePath, String jasperFilePath, List resultList) { JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(resultList); try { JasperPrint jasperPrint = new JasperPrintWithJavaBean(parameters, jrxmlFilePath, jasperFilePath, dataSource).getJasperPrint(); JRHtmlExporter exporter = new JRHtmlExporter(); request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream()); exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "/servlets/image?image=");// exporter.setParameter(JRExporterParameter.PAGE_INDEX, pageIndex); exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,false); //設置瀏覽器的圖片不緩存 exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8"); //設置html顯示字體過小的問題 exporter.setParameter(JRHtmlExporterParameter.SIZE_UNIT, "pt"); exporter.exportReport(); } catch(Exception e) { e.printStackTrace(); } finally { try { if(response.getOutputStream() != null) response.getOutputStream().close(); } catch (IOException e) { e.printStackTrace(); } }}?这个方法是直接在页面上显示报表,所以struts2的配置如下:
<action name="exportFareConditionBudgetByFare" method="exportFareConditionBudgetByFare"> <result name="success" type="jasper"> <param name="location">/jrxml/budget/fare_con_budget_fare.jasper</param> <param name="dataSource">budgetList</param> <param name="format">HTML</param> <!-- <param name="imageServletUrl"> <![CDATA[/image?image=]]> </param>--> </result> </action>
?
/** * 生成pdf形式報表文件 * @param parameters 設置報表的參數 * @param jrxmlFilePath 報表的jrxml文件路徑 * @param jasperFilePath 報表的jasper文件路徑 * @param resultList 數據列表(List),用於構造數據源 * @return 返回生成的pdf文件的絕對路徑 */ @SuppressWarnings("unchecked") public static String exportPdfReport(Map parameters, String jrxmlFilePath, String jasperFilePath, List resultList) { JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(resultList); try { JasperPrint jasperPrint = new JasperPrintWithJavaBean(parameters, jrxmlFilePath, jasperFilePath, dataSource).getJasperPrint(); JasperExportManager.exportReportToPdfFile(jasperPrint, getFileName(jrxmlFilePath, ".jrxml")+".pdf"); return getFileName(jrxmlFilePath, ".jrxml")+".pdf"; } catch(Exception e) { e.printStackTrace(); return null; } } ?这个方法是直接生成pdf文件并提示用户下载,用的是ajax的提交方式,所以struts2的配置如下:
<action name="generateReportFile" method="generateReportFile"> <result name="success" type="json"> </result> </action>
?
??
/** * 生成excel形式報表文件 * @param parameters 設置報表的參數 * @param jrxmlFilePath 報表的jrxml文件路徑 * @param jasperFilePath 報表的jasper文件路徑 * @param resultList 數據列表(List),用於構造數據源 * @return 返回生成的excel文件的絕對路徑 */ @SuppressWarnings("unchecked") public static String exportXlsReport(Map parameters, String jrxmlFilePath, String jasperFilePath, List resultList) { JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(resultList); try { JasperPrint jasperPrint = new JasperPrintWithJavaBean(parameters, jrxmlFilePath, jasperFilePath, dataSource).getJasperPrint(); // JRXlsExporter xlsExporter = new JRXlsExporter(); JRAbstractExporter exporter = new JExcelApiExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, getFileName(jrxmlFilePath, ".jrxml")+".xls"); //刪除最後一條記錄的空行 exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS, Boolean.TRUE); exporter.setParameter(JRXlsExporterParameter.IGNORE_PAGE_MARGINS, Boolean.TRUE); exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE); //删除多余的ColumnHeader exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); //顯示邊框 exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE); exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8"); exporter.exportReport(); return getFileName(jrxmlFilePath, ".jrxml")+".xls"; } catch(Exception e) { e.printStackTrace(); return null; } } ?这个方法是直接生成excel文件并提示用户下载,用的是ajax的提交方式,所以struts2的配置如下:
<action name="generateReportFile" method="generateReportFile"> <result name="success" type="json"> </result> </action>
???? 注意:JExcelApiExporter这个类需要用到jxl的包,用poi的不可以。?
???
/** * 打印報表 */ public static void print(HttpServletRequest request, HttpServletResponse response,Map parameters, String jrxmlFilePath, String jasperFilePath, List resultList){ String filePath=exportPdfReport(parameters,jrxmlFilePath,jasperFilePath,resultList); File returnFile=new File(filePath); response.setHeader("Content-disposition", "filename="+getFileName(jrxmlFilePath, ".jrxml")+".pdf"); response.setHeader("Content-Type", "application/pdf"); BufferedInputStream bis = null;//读excel BufferedOutputStream bos = null;//输出 try{ //读取excel文件 bis = new BufferedInputStream(new FileInputStream(returnFile)); //写入response的输出流中 bos=new java.io.BufferedOutputStream(response.getOutputStream()); byte[] buff = new byte[2048];/*设置缓存*/ int bytesRead; while(-1!= (bytesRead = bis.read(buff, 0, buff.length))){ bos.write(buff, 0, bytesRead); } }catch(Exception e){ e.printStackTrace(); }finally{ if (bis != null) try { bis.close(); } catch (IOException e) { e.printStackTrace(); } if (bos != null) try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } ?这个方法是打印报表的,直接把生成的pdf文件以流的形式输出到页面,然后利用adobe的pdf工具来打印。需要客户端安装adobe的pdf工具,你也可以利用其他的方式来打印,如IE内置的一个插件,在此不详细描述。
?