-
-
-
-
- package cn.com.reachway.framework.report.dataSource;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import net.sf.jasperreports.engine.JRException;
- import net.sf.jasperreports.engine.JRField;
-
-
-
-
- public class ReportDataSourceSample extends ReportDataSource {
-
- private List docs = new ArrayList();
-
- private int index = -1;
-
- public ReportDataSourceSample() {
- for (int i = 0; i < 10; i++) {
- Document doc = new Document("ViewTimes is:" + i, i);
- this.docs.add(doc);
- }
- }
-
-
- private class Document {
- private String name;
-
- private int viewTimes;
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getViewTimes() {
- return viewTimes;
- }
-
- public void setViewTimes(int viewTimes) {
- this.viewTimes = viewTimes;
- }
-
- public Document() {
- }
-
- public Document(String name, int viewTimes) {
- this.name = name;
- this.viewTimes = viewTimes;
- }
- }
-
- public List getDocs() {
- return docs;
- }
-
- public void setDocs(List docs) {
- this.docs = docs;
- }
-
- @Override
- public Object getFieldValue(JRField jrField) throws JRException {
-
- String fieldName = jrField.getName();
- Document doc = this.docs.get(index);
- if ("name".equals(fieldName)) {
- return doc.getName();
- }
- if ("viewTimes".equals(fieldName)) {
- return doc.getViewTimes();
- }
- return null;
- }
-
- @Override
- public boolean next() throws JRException {
- index++;
- return (index < this.docs.size());
- }
- }
-
java 代码
以上的例子应当很清楚的写明了如何生成数据源。
对于数据源的填充,笔者使用了二个类,分别用来对应使用Connction及JavaBean Collection进行填充。
java 代码
-
-
-
-
- package cn.com.reachway.framework.report.jasperPrint;
-
- import java.io.File;
- import java.sql.Connection;
- import java.util.Map;
-
- import net.sf.jasperreports.engine.JRException;
- import net.sf.jasperreports.engine.JasperFillManager;
- import net.sf.jasperreports.engine.JasperPrint;
- import net.sf.jasperreports.engine.JasperReport;
- import net.sf.jasperreports.engine.util.JRLoader;
- import cn.com.reachway.framework.exception.JasperReportException;
-
-
-
-
- public class JasperPrintWithConnection {
-
- private Map params;
-
- private String reportFilePath;
-
- private Connection con;
-
- public Connection getCon() {
- return con;
- }
-
- public void setCon(Connection con) {
- this.con = con;
- }
-
- public Map getParams() {
- return params;
- }
-
- public void setParams(Map params) {
- this.params = params;
- }
-
- public String getReportFilePath() {
- return reportFilePath;
- }
-
- public void setReportFilePath(String reportFilePath) throws JasperReportException {
- if (reportFilePath == null || !reportFilePath.endsWith(".jasper"))
- throw new JasperReportException("您传入的模板文件格式不对,请传入以.jasper为后缀的文件!");
- this.reportFilePath = reportFilePath;
- }
-
- public JasperPrintWithConnection() {
- super();
- }
-
- public JasperPrintWithConnection(String reportFilePath, Map params, Connection con) throws JasperReportException {
- if (reportFilePath == null || !reportFilePath.endsWith(".jasper"))
- throw new JasperReportException("模板文件格式不对,请传入以.jasper为后缀的文件!");
- if (con == null)
- throw new JasperReportException("Conncetion不应当为null!");
- this.setReportFilePath(reportFilePath);
- this.setParams(params);
- this.setCon(con);
- }
-
-
-
-
-
-
- public JasperPrint getJasperPrint() throws JasperReportException {
- File reportFile = new File(this.reportFilePath);
- if (!reportFile.exists())
- throw new JasperReportException("传入的模板文件不存在!");
-
- try {
-
- JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile.getPath());
-
- JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, this.params, this.con);
- return jasperPrint;
-
- } catch (JRException jre) {
- jre.printStackTrace();
- throw new JasperReportException("在进行数据填充时发生了错误中,请检查是否是数据库连接错误或者是用来填充的参数map有误!");
- }
-
- }
- }
-
-
-
-
-
- package cn.com.reachway.framework.report.jasperPrint;
-
- import java.io.File;
- import java.util.Map;
-
- import net.sf.jasperreports.engine.JRDataSource;
- import net.sf.jasperreports.engine.JRException;
- import net.sf.jasperreports.engine.JasperFillManager;
- import net.sf.jasperreports.engine.JasperPrint;
- import net.sf.jasperreports.engine.JasperReport;
- import net.sf.jasperreports.engine.util.JRLoader;
- import cn.com.reachway.framework.exception.JasperReportException;
-
-
-
-
- public class JasperPrintWithDataSource {
-
- private Map params;
-
- private String reportFilePath;
-
- private JRDataSource dataSource;
-
- public JRDataSource getDataSource() {
- return dataSource;
- }
-
- public void setDataSource(JRDataSource dataSource) {
- this.dataSource = dataSource;
- }
-
- public Map getParams() {
- return params;
- }
-
- public void setParams(Map params) {
- this.params = params;
- }
-
- public String getReportFilePath() {
- return reportFilePath;
- }
-
- public void setReportFilePath(String reportFilePath) throws JasperReportException {
- if (reportFilePath == null || !reportFilePath.endsWith(".jasper"))
- throw new JasperReportException("您传入的模板文件格式不对,请传入以.jasper为后缀的文件!");
- this.reportFilePath = reportFilePath;
- }
-
- public JasperPrintWithDataSource() {
- super();
- }
-
- public JasperPrintWithDataSource(String reportFilePath, Map params, JRDataSource dataSource)
- throws JasperReportException {
- if (reportFilePath == null || !reportFilePath.endsWith(".jasper"))
- throw new JasperReportException("模板文件格式不对,请传入以.jasper为后缀的文件!");
- if (dataSource == null)
- throw new JasperReportException("DataSource不应当为null!");
- this.setReportFilePath(reportFilePath);
- this.setParams(params);
- this.setDataSource(dataSource);
- }
-
-
-
-
-
- public JasperPrint getJasperPrint() throws JasperReportException {
- File reportFile = new File(this.reportFilePath);
- if (!reportFile.exists())
- throw new JasperReportException("传入的模板文件不存在!");
-
- try {
-
- JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile.getPath());
-
- JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, this.params, this.dataSource);
- return jasperPrint;
-
- } catch (JRException jre) {
- jre.printStackTrace();
- throw new JasperReportException("在进行数据填充时发生了错误中,请检查是否是数据库连接错误或者是用来填充的参数map有误!");
- }
-
- }
- }
-
java 代码
其中使用的JasperReportException为笔者定义的异常,用来统一处理报表异常。
5、报表产生
报表产生是程序中最终可见的一部分,在jasperReport的demo中,大部分中使用了jasperReport的net.sf.jasperreports.j2ee.servlets.*中的类来生成。其实这也算是开源的产品的一个问题,其实jasperReport提供的report的工具,只能算是demo级别的,不能算是产品级别的。相信很多的朋友在使用的时候就碰上无法生成的问题。笔者在此基础上封装了一下。
由于在一个里面不能贴太多的代码,故放置代码在此处。
上接:http://jimmy-shine.javaeye.com/blog/78678
下接:http://jimmy-shine.javaeye.com/blog/123597
java 代码
-
-
-
-
- package cn.com.reachway.framework.report.export;
-
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.util.Map;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import net.sf.jasperreports.engine.JRDataSource;
- import net.sf.jasperreports.engine.JRExporterParameter;
- import net.sf.jasperreports.engine.JasperPrint;
- import net.sf.jasperreports.engine.export.JRHtmlExporter;
- import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
- import net.sf.jasperreports.j2ee.servlets.ImageServlet;
- import cn.com.reachway.framework.exception.JasperReportException;
- import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithConnection;
- import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithDataSource;
-
-
-
-
- public class HTMLExport {
-
-
-
-
-
-
-
-
-
-
-
- public void export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,
- Connection con) throws JasperReportException {
- try {
- PrintWriter out = response.getWriter();
- try {
- response.setContentType("text/html;charset=UTF-8");
- JasperPrint jasperPrint = new JasperPrintWithConnection(reportFilePath, params, con).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_WRITER, out);
- exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "./servlets/image?image=");
- exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
-
- exporter.exportReport();
- } catch (Exception e) {
- e.printStackTrace();
- throw new JasperReportException("在导出Html格式报表时发生错误!");
- } finally {
- if (out != null) {
- try {
- out.close();
- } catch (Exception e) {
- }
- }
- }
- } catch (IOException ioe) {
- ioe.printStackTrace();
- throw new JasperReportException("从Response中取得PrintWriter时发生错误!");
- }
- }
-
-
-
-
-
-
-
-
-
-
-
- public void export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,
- JRDataSource dataSource) throws JasperReportException {
- try {
- PrintWriter out = response.getWriter();
- try {
- response.setContentType("text/html;charset=UTF-8");
- JasperPrint jasperPrint = new JasperPrintWithDataSource(reportFilePath, params, 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_WRITER, out);
- exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "./servlets/image?image=");
- exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
-
- exporter.exportReport();
- } catch (Exception e) {
- e.printStackTrace();
- throw new JasperReportException("在导出Html格式报表时发生错误!");
- } finally {
- if (out != null) {
- try {
- out.close();
- } catch (Exception e) {
- }
- }
- }
- } catch (IOException ioe) {
- ioe.printStackTrace();
- throw new JasperReportException("从Response中取得PrintWriter时发生错误!");
- }
- }
-
- }
Excel格式的:
java 代码
-
-
-
-
- package cn.com.reachway.framework.report.export;
-
- import java.io.IOException;
- import java.io.OutputStream;
- import java.net.URLEncoder;
- import java.sql.Connection;
- import java.util.List;
- import java.util.Map;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import net.sf.jasperreports.engine.JRDataSource;
- import net.sf.jasperreports.engine.JRException;
- import net.sf.jasperreports.engine.JRExporterParameter;
- import net.sf.jasperreports.engine.JasperPrint;
- import net.sf.jasperreports.engine.export.JRXlsAbstractExporter;
- import net.sf.jasperreports.engine.export.JRXlsAbstractExporterParameter;
- import net.sf.jasperreports.j2ee.servlets.BaseHttpServlet;
- import cn.com.reachway.framework.exception.JasperReportException;
- import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithConnection;
- import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithDataSource;
-
-
-
-
- public abstract class BaseExcelExport {
-
-
-
-
-
-
-
-
-
-
-
- public void export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,
- Connection con, String fileName) throws JasperReportException {
- JasperPrint jasperPrint = new JasperPrintWithConnection(reportFilePath, params, con).getJasperPrint();
-
- request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
-
- List jasperPrintList = BaseHttpServlet.getJasperPrintList(request);
-
- if (jasperPrintList == null) {
- throw new JasperReportException("在Http Session中没有找到JasperPrint List");
- }
- try {
- OutputStream ouputStream = response.getOutputStream();
- try {
-
- response.setContentType("application/xls");
- response.setCharacterEncoding("UTF-8");
- if (fileName == null || fileName.equals(""))
- response.setHeader("Content-Disposition", "inline; filename=\"noTitle.xls\"");
- else {
- response.setHeader("Content-Disposition", "inline; filename=\""
- + URLEncoder.encode(fileName, "UTF-8") + ".xls\"");
-
- }
-
- JRXlsAbstractExporter exporter = getXlsExporter();
-
-
- exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
-
- exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
-
- exporter.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
- exporter.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
-
- exporter.exportReport();
- } catch (JRException e) {
- e.printStackTrace();
- throw new JasperReportException("在生成XLS报表时发生错误!");
- }
-
- finally {
- if (ouputStream != null) {
- try {
- ouputStream.close();
- } catch (IOException ex) {
- }
- }
- }
- } catch (IOException ioe) {
- ioe.printStackTrace();
- throw new JasperReportException("从Response中取得OutputStream时发生错误!");
- }
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public void export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,
- JRDataSource dataSource, String fileName) throws JasperReportException {
- JasperPrint jasperPrint = new JasperPrintWithDataSource(reportFilePath, params, dataSource).getJasperPrint();
-
- request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
-
- List jasperPrintList = BaseHttpServlet.getJasperPrintList(request);
-
- if (jasperPrintList == null) {
- throw new JasperReportException("在Http Session中没有找到JasperPrint List");
- }
- try {
- OutputStream ouputStream = response.getOutputStream();
- try {
-
- response.setContentType("application/xls");
- response.setCharacterEncoding("UTF-8");
- if (fileName == null || fileName.equals(""))
- response.setHeader("Content-Disposition", "inline; filename=\"noTitle.xls\"");
- else {
- response.setHeader("Content-Disposition", "inline; filename=\""
- + URLEncoder.encode(fileName, "UTF-8") + ".xls\"");
-
- }
-
- JRXlsAbstractExporter exporter = getXlsExporter();
-
-
- exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
-
- exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
-
- exporter.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
- exporter.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
-
- exporter.exportReport();
- } catch (JRException e) {
- e.printStackTrace();
- throw new JasperReportException("在生成XLS报表时发生错误!");
- }
-
- finally {
- if (ouputStream != null) {
- try {
- ouputStream.close();
- } catch (IOException ex) {
- }
- }
- }
- } catch (IOException ioe) {
- ioe.printStackTrace();
- throw new JasperReportException("从Response中取得OutputStream时发生错误!");
- }
-
- }
-
- protected abstract JRXlsAbstractExporter getXlsExporter();
-
- }
java 代码
-
-
-
-
- package cn.com.reachway.framework.report.export;
-
- import net.sf.jasperreports.engine.export.JRXlsAbstractExporter;
- import net.sf.jasperreports.engine.export.JRXlsExporter;
-
-
-
-
- public class XlsPOIExport extends BaseExcelExport {
-
- protected JRXlsAbstractExporter getXlsExporter() {
- return new JRXlsExporter();
- }
-
- }
-
java 代码
-
-
-
-
- package cn.com.reachway.framework.report.export;
-
- import net.sf.jasperreports.engine.export.JExcelApiExporter;
- import net.sf.jasperreports.engine.export.JRXlsAbstractExporter;
-
-
-
-
- public class XlsJExcelExport extends BaseExcelExport {
-
- protected JRXlsAbstractExporter getXlsExporter() {
- return new JExcelApiExporter();
- }
- }
由于在一个里面不能贴太多的代码,故放置代码在此处。
上接:http://jimmy-shine.javaeye.com/blog/123595
PDF格式的:
java 代码
-
-
-
-
- package cn.com.reachway.framework.report.export;
-
- import java.io.IOException;
- import java.io.OutputStream;
- import java.net.URLEncoder;
- import java.sql.Connection;
- import java.util.List;
- import java.util.Map;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import net.sf.jasperreports.engine.JRDataSource;
- import net.sf.jasperreports.engine.JRException;
- import net.sf.jasperreports.engine.JRExporterParameter;
- import net.sf.jasperreports.engine.JasperPrint;
- import net.sf.jasperreports.engine.export.JRPdfExporter;
- import net.sf.jasperreports.j2ee.servlets.BaseHttpServlet;
- import cn.com.reachway.framework.exception.JasperReportException;
- import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithConnection;
- import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithDataSource;
-
-
-
-
- public class PDFExport {
-
-
-
-
-
-
-
-
-
-
-
-
- public void export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,
- Connection con, String fileName) throws JasperReportException {
-
- JasperPrint jasperPrint = new JasperPrintWithConnection(reportFilePath, params, con).getJasperPrint();
-
- request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
-
- List jasperPrintList = BaseHttpServlet.getJasperPrintList(request);
-
- if (jasperPrintList == null) {
- throw new JasperReportException("在Http Session中没有找到JasperPrint List");
- }
- try {
- OutputStream ouputStream = response.getOutputStream();
- try {
- response.setContentType("application/pdf");
- response.setCharacterEncoding("UTF-8");
- if (fileName == null || fileName.equals(""))
- response.setHeader("Content-Disposition", "inline; filename=\"noTitle.pdf\"");
- else
- response.setHeader("Content-Disposition", "inline; filename=\""
- + URLEncoder.encode(fileName, "UTF-8") + ".pdf\"");
-
- JRPdfExporter exporter = new JRPdfExporter();
-
- exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
-
- exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
- exporter.exportReport();
- } catch (JRException e) {
- e.printStackTrace();
- throw new JasperReportException("在导出pdf格式报表时发生错误");
- } finally {
- if (ouputStream != null) {
- try {
- ouputStream.close();
- } catch (IOException ex) {
- }
- }
- }
- } catch (IOException ioe) {
- ioe.printStackTrace();
- throw new JasperReportException("从Response中取得OutputStream时发生错误!");
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public void export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,
- JRDataSource dataSource, String fileName) throws JasperReportException {
-
- JasperPrint jasperPrint = new JasperPrintWithDataSource(reportFilePath, params, dataSource).getJasperPrint();
-
- request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
-
- List jasperPrintList = BaseHttpServlet.getJasperPrintList(request);
-
- if (jasperPrintList == null) {
- throw new JasperReportException("在Http Session中没有找到JasperPrint List");
- }
- try {
- OutputStream ouputStream = response.getOutputStream();
- try {
- response.setContentType("application/pdf");
- response.setCharacterEncoding("UTF-8");
- if (fileName == null || fileName.equals(""))
- response.setHeader("Content-Disposition", "inline; filename=\"noTitle.pdf\"");
- else
- response.setHeader("Content-Disposition", "inline; filename=\""
- + URLEncoder.encode(fileName, "UTF-8") + ".pdf\"");
-
- JRPdfExporter exporter = new JRPdfExporter();
-
- exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
-
- exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
- exporter.exportReport();
- } catch (JRException e) {
- e.printStackTrace();
- throw new JasperReportException("在导出pdf格式报表时发生错误");
- } finally {
- if (ouputStream != null) {
- try {
- ouputStream.close();
- } catch (IOException ex) {
- }
- }
- }
- } catch (IOException ioe) {
- ioe.printStackTrace();
- throw new JasperReportException("从Response中取得OutputStream时发生错误!");
- }
- }
-
- }