1、先用ireport工具新建jrxml文件,再制作报表样式!
2、可以通过在控件的右键上设置边框,这是为了产生表格效果
好了 初步工作已经有了,我们经过编译后得到了一个jasper文件,那么我们怎么用在J2EE的项目中呢?
3、将下列信息配置在web.xml
ImageServlet net.sf.jasperreports.j2ee.servlets.ImageServlet ImageServlet /servlets/image
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 + "." + TASK_XLS);
exporter.exportReport();
} else if (TASK_HTML.equals(fileType)) {
JRHtmlExporter export = new JRHtmlExporter();
export.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
export.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,exportFilePath + "." + TASK_HTML);
export.exportReport();
}else if(TASK_PDF.equals(fileType)){
JRPdfExporter export = new JRPdfExporter();
export.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
export.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,exportFilePath + "." + TASK_PDF);
export.exportReport();
}
} catch (JRException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
export("xls");
}
}
如何在jsp中使用呢? 如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@ page import="net.sf.jasperreports.engine.*" %>
<%@ page import="net.sf.jasperreports.engine.util.*" %>
<%@ page import="net.sf.jasperreports.engine.export.*" %>
<%@ page import="net.sf.jasperreports.j2ee.servlets.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@page import="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'index.jsp' starting page
<%
Map parameterMap = new HashMap();
parameterMap.put("ItemProjecID", "10010");
parameterMap.put("SupervisionID", "1000000001");
parameterMap.put("ConformityCode", "201109102218");
//提供jasper所需要的parameters
List list = new ArrayList();
Map dataList = new HashMap();
dataList.put("workAreaName", "995400116528");
dataList.put("UnitProjectType", "2009111");
dataList.put("UnitProjectAddress", "test");
dataList.put("UnitProjectSubType", "111409015");
dataList.put("Scale", "2009年汇理财稳利系列F7计划");
dataList.put("BargainStartDate", "USD钞");//提供jasper所需要的Fileds
list.add(dataList);
try {
File reportFile = new File(application.getRealPath("/report/bj.jasper"));
JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,
parameterMap, new JRBeanCollectionDataSource(list));
JRHtmlExporter exporter = new JRHtmlExporter();
session.setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "./servlets/image?image=");
exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
exporter.exportReport();
} catch (Exception e) {
e.printStackTrace();
}
%>
搞定!
附件中是一个小例子,大家可以参考!