日期:2014-05-16 浏览次数:21488 次
/**
* 導出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" class="com.boc.web.action.financialfare.statistics.BudgetMonitoringConditionAction" 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" class="com.boc.web.action.financialfare.statistics.BudgetMonitoringConditionAction" method="generateReportFile">
<result name="success" type="json">
</result>
</action><