-
-
-
-
- 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 代碼
以上的例子應(yīng)當(dāng)很清楚的寫(xiě)明了如何生成數(shù)據(jù)源。
對(duì)于數(shù)據(jù)源的填充,筆者使用了二個(gè)類(lèi),分別用來(lái)對(duì)應(yīng)使用Connction及JavaBean Collection進(jìn)行填充。
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("您傳入的模板文件格式不對(duì),請(qǐng)傳入以.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("模板文件格式不對(duì),請(qǐng)傳入以.jasper為后綴的文件!");
- if (con == null)
- throw new JasperReportException("Conncetion不應(yīng)當(dāng)為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("在進(jìn)行數(shù)據(jù)填充時(shí)發(fā)生了錯(cuò)誤中,請(qǐng)檢查是否是數(shù)據(jù)庫(kù)連接錯(cuò)誤或者是用來(lái)填充的參數(shù)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("您傳入的模板文件格式不對(duì),請(qǐng)傳入以.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("模板文件格式不對(duì),請(qǐng)傳入以.jasper為后綴的文件!");
- if (dataSource == null)
- throw new JasperReportException("DataSource不應(yīng)當(dāng)為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("在進(jìn)行數(shù)據(jù)填充時(shí)發(fā)生了錯(cuò)誤中,請(qǐng)檢查是否是數(shù)據(jù)庫(kù)連接錯(cuò)誤或者是用來(lái)填充的參數(shù)map有誤!");
- }
-
- }
- }
-
java 代碼
其中使用的JasperReportException為筆者定義的異常,用來(lái)統(tǒng)一處理報(bào)表異常。
5、報(bào)表產(chǎn)生
報(bào)表產(chǎn)生是程序中最終可見(jiàn)的一部分,在jasperReport的demo中,大部分中使用了jasperReport的net.sf.jasperreports.j2ee.servlets.*中的類(lèi)來(lái)生成。其實(shí)這也算是開(kāi)源的產(chǎn)品的一個(gè)問(wèn)題,其實(shí)jasperReport提供的report的工具,只能算是demo級(jí)別的,不能算是產(chǎn)品級(jí)別的。相信很多的朋友在使用的時(shí)候就碰上無(wú)法生成的問(wèn)題。筆者在此基礎(chǔ)上封裝了一下。
由于在一個(gè)里面不能貼太多的代碼,故放置代碼在此處。
上接: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("在導(dǎo)出Html格式報(bào)表時(shí)發(fā)生錯(cuò)誤!");
- } finally {
- if (out != null) {
- try {
- out.close();
- } catch (Exception e) {
- }
- }
- }
- } catch (IOException ioe) {
- ioe.printStackTrace();
- throw new JasperReportException("從Response中取得PrintWriter時(shí)發(fā)生錯(cuò)誤!");
- }
- }
-
-
-
-
-
-
-
-
-
-
-
- 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("在導(dǎo)出Html格式報(bào)表時(shí)發(fā)生錯(cuò)誤!");
- } finally {
- if (out != null) {
- try {
- out.close();
- } catch (Exception e) {
- }
- }
- }
- } catch (IOException ioe) {
- ioe.printStackTrace();
- throw new JasperReportException("從Response中取得PrintWriter時(shí)發(fā)生錯(cuò)誤!");
- }
- }
-
- }
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中沒(méi)有找到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報(bào)表時(shí)發(fā)生錯(cuò)誤!");
- }
-
- finally {
- if (ouputStream != null) {
- try {
- ouputStream.close();
- } catch (IOException ex) {
- }
- }
- }
- } catch (IOException ioe) {
- ioe.printStackTrace();
- throw new JasperReportException("從Response中取得OutputStream時(shí)發(fā)生錯(cuò)誤!");
- }
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
- 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中沒(méi)有找到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報(bào)表時(shí)發(fā)生錯(cuò)誤!");
- }
-
- finally {
- if (ouputStream != null) {
- try {
- ouputStream.close();
- } catch (IOException ex) {
- }
- }
- }
- } catch (IOException ioe) {
- ioe.printStackTrace();
- throw new JasperReportException("從Response中取得OutputStream時(shí)發(fā)生錯(cuò)誤!");
- }
-
- }
-
- 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();
- }
- }
由于在一個(gè)里面不能貼太多的代碼,故放置代碼在此處。
上接: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中沒(méi)有找到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("在導(dǎo)出pdf格式報(bào)表時(shí)發(fā)生錯(cuò)誤");
- } finally {
- if (ouputStream != null) {
- try {
- ouputStream.close();
- } catch (IOException ex) {
- }
- }
- }
- } catch (IOException ioe) {
- ioe.printStackTrace();
- throw new JasperReportException("從Response中取得OutputStream時(shí)發(fā)生錯(cuò)誤!");
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
- 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中沒(méi)有找到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("在導(dǎo)出pdf格式報(bào)表時(shí)發(fā)生錯(cuò)誤");
- } finally {
- if (ouputStream != null) {
- try {
- ouputStream.close();
- } catch (IOException ex) {
- }
- }
- }
- } catch (IOException ioe) {
- ioe.printStackTrace();
- throw new JasperReportException("從Response中取得OutputStream時(shí)發(fā)生錯(cuò)誤!");
- }
- }
-
- }