日期:2014-05-17 浏览次数:21138 次
1、先用ireport工具新建jrxml文件,再制作报表样式!
?
?
2、可以通过在控件的右键上设置边框,这是为了产生表格效果
?
?
好了 初步工作已经有了,我们经过编译后得到了一个jasper文件,那么我们怎么用在J2EE的项目中呢?
?
3、将下列信息配置在web.xml
<servlet> <servlet-name>ImageServlet</servlet-name> <servlet-class>net.sf.jasperreports.j2ee.servlets.ImageServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ImageServlet</servlet-name> <url-pattern>/servlets/image</url-pattern> </servlet-mapping>
?
?4、导入运行jasper所要用到的5个jar包,jar包在下面的附件里。可以下载获得
?
5、配置完成后如何使用呢?? 先学习如何在java类中使用 ,生成excel 、html、pdf等格式
package com.test; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; import net.sf.jasperreports.engine.export.JRHtmlExporter; import net.sf.jasperreports.engine.export.JRPdfExporter; import net.sf.jasperreports.engine.export.JRXlsExporter; import net.sf.jasperreports.engine.export.JRXlsExporterParameter; import net.sf.jasperreports.engine.util.JRLoader; @SuppressWarnings("unchecked") public class Test3 { private static final String TASK_PDF = "pdf"; private static final String TASK_XML = "xml"; private static final String TASK_XML_EMBED = "xmlEmbed"; private static final String TASK_HTML = "html"; private static final String TASK_RTF = "rtf"; private static final String TASK_XLS = "xls"; private static final String TASK_JXL = "jxl"; private static final String TASK_CSV = "csv"; private static final String TASK_ODT = "odt"; private static final String file = "D:\\workspase\\reportTest\\WebRoot\\report\\report3.jasper"; private static final String exportFilePath = "f:\\report3"; private static List getDataList() { List list = new ArrayList(); for (int i = 0; i < 5; i++) { Map dataList = new HashMap(); dataList.put("serialNo", "995400116528" + i); dataList.put("reqDate", "2009111" + i); dataList.put("businessType", "认购"); dataList.put("productCode", "111409015" + i); dataList.put("productName", "2009年汇理财稳利系列F7计划"); dataList.put("currencyType", "USD钞"); dataList.put("reqMoney", "5300.00"); dataList.put("transStatus", "正常"); list.add(dataList); } return list; } private static Map getParameterMap() { Map parameterMap = new HashMap(); parameterMap.put("CUSTOMER_NAME", "赵六"); parameterMap.put("CUSTOMER_ACCOUNT", "98430100829415"); parameterMap.put("TRANS_BANK", "9502"); parameterMap.put("TRANS_DATE", "20091118"); parameterMap.put("TRANS_OPERATOR", "11001157"); return parameterMap; } public static void export(String fileType) { // 填充报表数据 File reportFile = new File(file); JasperReport jasperReport = null; JasperPrint jasperPrint = null; try { jasperReport = (JasperReport) JRLoader.loadObject(reportFile .getPath()); jasperPrint = JasperFillManager.fillReport(jasperReport, getParameterMap(), new JRBeanCollectionDataSource( getDataList())); // 导出报表文件 if (TASK_XLS.equals(fileType)) { JRXlsExporter exporter = new JRXlsExporter(); exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.TRUE); exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, exportFilePath + "." +