- 浏览: 520100 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (299)
- Oracle(pl/sql_Erp_Pro*C) (69)
- 设计模式 (4)
- spring (23)
- ext (17)
- apache开源项目应用 (4)
- jquery (16)
- 生活琐事 (8)
- 下载资源 (23)
- mysql (2)
- Eclipse使用积累 (5)
- 报表类(报表/图表) (13)
- php (4)
- Web多彩文本框 (3)
- json (4)
- jqgrid (2)
- ant (2)
- java算法积累 (8)
- EL表达式/JSTL (4)
- poi (3)
- gwt (2)
- 爬网第一步 (2)
- javascript (17)
- Javaweb (8)
- tomcat (1)
- flex (1)
- Java&DB (3)
- J2SE (7)
- linux (3)
- 数据结构 (1)
- dot net (5)
- struts (1)
- ibatis (1)
- log4j (1)
- 项目管理 (1)
- Java native interface(jni,jacob......) (5)
- applet (1)
- VB.net/C#.net/JNI (20)
- css (1)
- Sqlite (1)
- servlet (1)
- REST (1)
最新评论
-
wenhurena:
能不能给一下解压密码roki.work.2017@gmail. ...
Ebs解体新書と学習資料1 -
liutao1600:
楼主写的太好了,每天学习~~
Spring_MVC(6)测试 -
liutao1600:
太好了,每天学习你的文章~~~
Spring_MVC(3)表单页面处理 -
liutao1600:
学习了,太好了
Spring_MVC(2)控制层处理 -
liutao1600:
学习了~~~
Spring_MVC(1)构建简单web应用
1)定制报表格式。
有二种方式,一种就是写jrxml文件,其实就是xml文件,只不过是后缀名不一样罢了。另一种方式更直接,就是生成一个JasperDesign类的实例,在japsperDesign中自己定义模板。jrxml文件也是通过一个JRXmlLoad加载过来,转成JasperDesign类的实例。也就是说写jrxml文件还需要进行解析,加载。现实中我们使用的报表一般格式比较固定,因而可以通过先使用iReport工具生成模板,再加载解析的方式。这种方式简单,而且可见性强。
2)填充数据。
在最新版(2007.4.30发布)的jasperReports1.3.3中,支持了很多的格式,包括了对于Hibernate的支持。填充数据对于我们经常使用的来说,一般是二种方式,一种方式是通过JDBC连接提供数据源,一种就是通过javaBean的集合提供数据源。当然还有web Service的xml文件提供的。我的建议是,如果你的程序中的统计直接使用Jdbc就可以完成,那么就使用jdbc数据源的方法,反之,使用 javaBean的集合是不错的选择,因为这样不会在意你的数据的来源,你也可以任意处理,比如说,要通过权限检查的数据才在报表中生成的话,就可以过滤到不符合权限的数据。
3)显示或打印。
显示,即将JasperReport生成的文件直接显示出来,打印所使用的方式其实就是生成文件,然后调用打印来对文件进行打印。
看起来是不是很简单,对,确实很简单。
笔者主要从事基于B/S模式的开发,相信大部分人的使用也是基于web进行使用。故笔者对于基于web的开发进行详细讲述。
1、工程环境配置。
将以下包加入WEB-INF/lib中。
commons-beanutils-1.7.jar;commons-collections-2.1.jar;commons-digester-1.7.jar;commons-logging-1.0.2.jar;commons-logging-api-1.0.2.jar;itext-1.3.1.jar;jasperreports-1.3.3.jar;jdt-compiler-3.1.1.jar;jxl-2.6.jar;png-encoder-1.5.jar;poi-2.0-final-20040126.jar
以上包是jasperReport必须。
2、iReport的安装
这个很简单,直接下一步就可以了。
3、使用iReport生成 .jasper文件
.jasper是经过编译的japserReport模板,即.jrxml编译后的。
对于iReport的使用网络上已经有了详细的中文的文档,具体可以下载附件查看。使用.jasper文件可以避免在系统中再进行编译,增加系统的压力。
4、数据填充
数据填充是相对于比较重要的一步,数据填充的目的是为了将数据填充进去,生成一个JapserPrint对象。笔者在之前已经阐述过,现在主要是基于关系型数据库(比较简单的,没有权限控制的),以及基于JavaBean Collection进行数据填充。进行数据填充的基本就是一定要实现JRDataSource,JRDataSource定义了二个方法,一个是指针移动的方法next(),另一个是取值的getFieldValue()方法,用来取值填充。
对于关系型数据库,即直接利用数据库的表来生成报表的,只需要传入一个Java Connection即可。JasperReport已经有了个默认的实现的JRDataSource的接口了。对于Java Conncetion的获得,笔者建议使用工厂方法或者使用Spring方式来获得。可以参照笔者写的如下:
源自:http://www.iteye.com/topic/78678
1. /**
2. * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
3. * @author Jimmy.Shine 2007-5-11
4. */
5. package cn.com.reachway.framework.report;
6.
7. import java.sql.Connection;
8. import java.sql.DriverManager;
9.
10. import cn.com.reachway.framework.exception.JasperReportException;
11.
12. /**
13. * Used for get a JDBC connection for Jasper Report
14. */
15. public class JDBCConnection {
16. /**
17. * Which Jdbc driver were used. Like: "net.sourceforge.jtds.jdbc.Driver"
18. */
19. private String jdbcDriver;
20. /**
21. * The jdbc url,define which database server,which db. Like:
22. * "jdbc:jtds:sqlserver://192.168.1.50:1433;DatabaseName=JAVA5;SelectMethod=Cursor"
23. */
24. private String jdbcUrl;
25.
26. private String dbUser;
27.
28. private String dbPassword;
29.
30. public String getDbPassword() {
31. return dbPassword;
32. }
33.
34. public void setDbPassword(String dbPassword) {
35. this.dbPassword = dbPassword;
36. }
37.
38. public String getDbUser() {
39. return dbUser;
40. }
41.
42. public void setDbUser(String dbUser) {
43. this.dbUser = dbUser;
44. }
45.
46. public String getJdbcDriver() {
47. return jdbcDriver;
48. }
49.
50. public void setJdbcDriver(String jdbcDriver) {
51. this.jdbcDriver = jdbcDriver;
52. }
53.
54. public String getJdbcUrl() {
55. return jdbcUrl;
56. }
57.
58. public void setJdbcUrl(String jdbcUrl) {
59. this.jdbcUrl = jdbcUrl;
60. }
61.
62. public JDBCConnection() {
63. super();
64. }
65. /**
66. * Get the Connection
67. * @return connection
68. * @throws JasperReportException
69. */
70. public Connection getConnection() throws JasperReportException {
71. Connection con;
72. try {
73. check();
74. Class.forName(this.jdbcDriver);
75. con = DriverManager.getConnection(this.jdbcUrl, this.dbUser, this.dbPassword);
76. return con;
77. } catch (Exception e) {
78. e.printStackTrace();
79. throw new JasperReportException("Get JDBC Connection Error");
80. }
81. }
82.
83. /**
84. * Check the Configure
85. * @throws JasperReportException
86. */
87. private void check() throws JasperReportException {
88. if (this.jdbcDriver == null || this.jdbcDriver.equals("") || this.jdbcUrl == null || this.jdbcUrl.equals("")
89. || this.dbUser == null || this.dbUser.equals("") || this.dbPassword == null) {
90. throw new JasperReportException("Jdbc Configure error!");
91. }
92. }
93.
94. }
95.
/**
* @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
* @author Jimmy.Shine 2007-5-11
*/
package cn.com.reachway.framework.report;
import java.sql.Connection;
import java.sql.DriverManager;
import cn.com.reachway.framework.exception.JasperReportException;
/**
* Used for get a JDBC connection for Jasper Report
*/
public class JDBCConnection {
/**
* Which Jdbc driver were used. Like: "net.sourceforge.jtds.jdbc.Driver"
*/
private String jdbcDriver;
/**
* The jdbc url,define which database server,which db. Like:
* "jdbc:jtds:sqlserver://192.168.1.50:1433;DatabaseName=JAVA5;SelectMethod=Cursor"
*/
private String jdbcUrl;
private String dbUser;
private String dbPassword;
public String getDbPassword() {
return dbPassword;
}
public void setDbPassword(String dbPassword) {
this.dbPassword = dbPassword;
}
public String getDbUser() {
return dbUser;
}
public void setDbUser(String dbUser) {
this.dbUser = dbUser;
}
public String getJdbcDriver() {
return jdbcDriver;
}
public void setJdbcDriver(String jdbcDriver) {
this.jdbcDriver = jdbcDriver;
}
public String getJdbcUrl() {
return jdbcUrl;
}
public void setJdbcUrl(String jdbcUrl) {
this.jdbcUrl = jdbcUrl;
}
public JDBCConnection() {
super();
}
/**
* Get the Connection
* @return connection
* @throws JasperReportException
*/
public Connection getConnection() throws JasperReportException {
Connection con;
try {
check();
Class.forName(this.jdbcDriver);
con = DriverManager.getConnection(this.jdbcUrl, this.dbUser, this.dbPassword);
return con;
} catch (Exception e) {
e.printStackTrace();
throw new JasperReportException("Get JDBC Connection Error");
}
}
/**
* Check the Configure
* @throws JasperReportException
*/
private void check() throws JasperReportException {
if (this.jdbcDriver == null || this.jdbcDriver.equals("") || this.jdbcUrl == null || this.jdbcUrl.equals("")
|| this.dbUser == null || this.dbUser.equals("") || this.dbPassword == null) {
throw new JasperReportException("Jdbc Configure error!");
}
}
}
1. /**
2. * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
3. * @author Jimmy.Shine 2007-5-14
4. */
5. package cn.com.reachway.framework.report.dataSource;
6.
7. import net.sf.jasperreports.engine.JRDataSource;
8. import net.sf.jasperreports.engine.JRException;
9. import net.sf.jasperreports.engine.JRField;
10.
11. /**
12. * 报表的dataSource的基类,所有利用DataSource产生报表的继承此类
13. */
14. public abstract class ReportDataSource implements JRDataSource {
15.
16. /**
17. * 取值
18. */
19. public abstract Object getFieldValue(JRField jrField) throws JRException;
20.
21. /**
22. * 指针移动
23. */
24. public abstract boolean next() throws JRException;
25.
26. }
27.
/**
* @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
* @author Jimmy.Shine 2007-5-14
*/
package cn.com.reachway.framework.report.dataSource;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;
/**
* 报表的dataSource的基类,所有利用DataSource产生报表的继承此类
*/
public abstract class ReportDataSource implements JRDataSource {
/**
* 取值
*/
public abstract Object getFieldValue(JRField jrField) throws JRException;
/**
* 指针移动
*/
public abstract boolean next() throws JRException;
}
这样可以共用配置文件中的jdbc连接的相关参数。
对于其它的数据源,JavaBean Collection,笔者采用了一个抽象类,使用抽象类的目的,是为了使开发者尽可能不用直接接触jasperReport(此为笔者自己的方式,笔者是PM,为了考虑底下的人的开发)。以下为抽象类以及笔者提供的一个sample:
1. /**
2. * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
3. * @author Jimmy.Shine 2007-5-14
4. */
5. package cn.com.reachway.framework.report.dataSource;
6.
7. import java.util.ArrayList;
8. import java.util.List;
9.
10. import net.sf.jasperreports.engine.JRException;
11. import net.sf.jasperreports.engine.JRField;
12.
13. /**
14. * 利用JavaBean Collection生成ReportDataSource的例子
15. */
16. public class ReportDataSourceSample extends ReportDataSource {
17.
18. private List docs = new ArrayList();
19.
20. private int index = -1;
21.
22. public ReportDataSourceSample() {
23. for (int i = 0; i < 10; i++) {
24. Document doc = new Document("ViewTimes is:" + i, i);
25. this.docs.add(doc);
26. }
27. }
28.
29. // 内部类,作为demo时使用
30. private class Document {
31. private String name;
32.
33. private int viewTimes;
34.
35. public String getName() {
36. return name;
37. }
38.
39. public void setName(String name) {
40. this.name = name;
41. }
42.
43. public int getViewTimes() {
44. return viewTimes;
45. }
46.
47. public void setViewTimes(int viewTimes) {
48. this.viewTimes = viewTimes;
49. }
50.
51. public Document() {
52. }
53.
54. public Document(String name, int viewTimes) {
55. this.name = name;
56. this.viewTimes = viewTimes;
57. }
58. }
59.
60. public List getDocs() {
61. return docs;
62. }
63.
64. public void setDocs(List docs) {
65. this.docs = docs;
66. }
67.
68. @Override
69. public Object getFieldValue(JRField jrField) throws JRException {
70.
71. String fieldName = jrField.getName();
72. Document doc = this.docs.get(index);
73. if ("name".equals(fieldName)) {
74. return doc.getName();
75. }
76. if ("viewTimes".equals(fieldName)) {
77. return doc.getViewTimes();
78. }
79. return null;
80. }
81.
82. @Override
83. public boolean next() throws JRException {
84. index++;
85. return (index < this.docs.size());
86. }
87. }
88.
/**
* @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
* @author Jimmy.Shine 2007-5-14
*/
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;
/**
* 利用JavaBean Collection生成ReportDataSource的例子
*/
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);
}
}
// 内部类,作为demo时使用
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());
}
}
以上的例子应当很清楚的写明了如何生成数据源。
对于数据源的填充,笔者使用了二个类,分别用来对应使用Connction及JavaBean Collection进行填充。
1. /**
2. * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
3. * @author Jimmy.Shine 2007-5-12
4. */
5. package cn.com.reachway.framework.report.jasperPrint;
6.
7. import java.io.File;
8. import java.sql.Connection;
9. import java.util.Map;
10.
11. import net.sf.jasperreports.engine.JRException;
12. import net.sf.jasperreports.engine.JasperFillManager;
13. import net.sf.jasperreports.engine.JasperPrint;
14. import net.sf.jasperreports.engine.JasperReport;
15. import net.sf.jasperreports.engine.util.JRLoader;
16. import cn.com.reachway.framework.exception.JasperReportException;
17.
18. /**
19. * 使用报表模板及数据等来生成JapserPrint
20. */
21. public class JasperPrintWithConnection {
22. /** 传入的参数 */
23. private Map params;
24. /** 模板文件的地址 */
25. private String reportFilePath;
26. /** JDBC connection */
27. private Connection con;
28.
29. public Connection getCon() {
30. return con;
31. }
32.
33. public void setCon(Connection con) {
34. this.con = con;
35. }
36.
37. public Map getParams() {
38. return params;
39. }
40.
41. public void setParams(Map params) {
42. this.params = params;
43. }
44.
45. public String getReportFilePath() {
46. return reportFilePath;
47. }
48.
49. public void setReportFilePath(String reportFilePath) throws JasperReportException {
50. if (reportFilePath == null || !reportFilePath.endsWith(".jasper"))
51. throw new JasperReportException("您传入的模板文件格式不对,请传入以.jasper为后缀的文件!");
52. this.reportFilePath = reportFilePath;
53. }
54.
55. public JasperPrintWithConnection() {
56. super();
57. }
58.
59. public JasperPrintWithConnection(String reportFilePath, Map params, Connection con) throws JasperReportException {
60. if (reportFilePath == null || !reportFilePath.endsWith(".jasper"))
61. throw new JasperReportException("模板文件格式不对,请传入以.jasper为后缀的文件!");
62. if (con == null)
63. throw new JasperReportException("Conncetion不应当为null!");
64. this.setReportFilePath(reportFilePath);
65. this.setParams(params);
66. this.setCon(con);
67. }
68.
69. /**
70. * 取得JasperPrint
71. * @return
72. * @throws JasperReportException
73. */
74. public JasperPrint getJasperPrint() throws JasperReportException {
75. File reportFile = new File(this.reportFilePath);
76. if (!reportFile.exists())
77. throw new JasperReportException("传入的模板文件不存在!");
78.
79. try {
80. // Load编译好的模板
81. JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile.getPath());
82. // 进行数据填充
83. JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, this.params, this.con);
84. return jasperPrint;
85.
86. } catch (JRException jre) {
87. jre.printStackTrace();
88. throw new JasperReportException("在进行数据填充时发生了错误中,请检查是否是数据库连接错误或者是用来填充的参数map有误!");
89. }
90.
91. }
92. }
93.
/**
* @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
* @author Jimmy.Shine 2007-5-12
*/
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;
/**
* 使用报表模板及数据等来生成JapserPrint
*/
public class JasperPrintWithConnection {
/** 传入的参数 */
private Map params;
/** 模板文件的地址 */
private String reportFilePath;
/** JDBC connection */
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);
}
/**
* 取得JasperPrint
* @return
* @throws JasperReportException
*/
public JasperPrint getJasperPrint() throws JasperReportException {
File reportFile = new File(this.reportFilePath);
if (!reportFile.exists())
throw new JasperReportException("传入的模板文件不存在!");
try {
// Load编译好的模板
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有误!");
}
}
}
1. /**
2. * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
3. * @author Jimmy.Shine 2007-5-14
4. */
5. package cn.com.reachway.framework.report.jasperPrint;
6.
7. import java.io.File;
8. import java.util.Map;
9.
10. import net.sf.jasperreports.engine.JRDataSource;
11. import net.sf.jasperreports.engine.JRException;
12. import net.sf.jasperreports.engine.JasperFillManager;
13. import net.sf.jasperreports.engine.JasperPrint;
14. import net.sf.jasperreports.engine.JasperReport;
15. import net.sf.jasperreports.engine.util.JRLoader;
16. import cn.com.reachway.framework.exception.JasperReportException;
17.
18. /**
19. *
20. */
21. public class JasperPrintWithDataSource {
22. /** 传入的参数 */
23. private Map params;
24. /** 模板文件的地址 */
25. private String reportFilePath;
26. /** dataSrouce */
27. private JRDataSource dataSource;
28.
29. public JRDataSource getDataSource() {
30. return dataSource;
31. }
32.
33. public void setDataSource(JRDataSource dataSource) {
34. this.dataSource = dataSource;
35. }
36.
37. public Map getParams() {
38. return params;
39. }
40.
41. public void setParams(Map params) {
42. this.params = params;
43. }
44.
45. public String getReportFilePath() {
46. return reportFilePath;
47. }
48.
49. public void setReportFilePath(String reportFilePath) throws JasperReportException {
50. if (reportFilePath == null || !reportFilePath.endsWith(".jasper"))
51. throw new JasperReportException("您传入的模板文件格式不对,请传入以.jasper为后缀的文件!");
52. this.reportFilePath = reportFilePath;
53. }
54.
55. public JasperPrintWithDataSource() {
56. super();
57. }
58.
59. public JasperPrintWithDataSource(String reportFilePath, Map params, JRDataSource dataSource)
60. throws JasperReportException {
61. if (reportFilePath == null || !reportFilePath.endsWith(".jasper"))
62. throw new JasperReportException("模板文件格式不对,请传入以.jasper为后缀的文件!");
63. if (dataSource == null)
64. throw new JasperReportException("DataSource不应当为null!");
65. this.setReportFilePath(reportFilePath);
66. this.setParams(params);
67. this.setDataSource(dataSource);
68. }
69. /**
70. * 取得JasperPrint
71. * @return
72. * @throws JasperReportException
73. */
74. public JasperPrint getJasperPrint() throws JasperReportException {
75. File reportFile = new File(this.reportFilePath);
76. if (!reportFile.exists())
77. throw new JasperReportException("传入的模板文件不存在!");
78.
79. try {
80. // Load编译好的模板
81. JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile.getPath());
82. // 进行数据填充
83. JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, this.params, this.dataSource);
84. return jasperPrint;
85.
86. } catch (JRException jre) {
87. jre.printStackTrace();
88. throw new JasperReportException("在进行数据填充时发生了错误中,请检查是否是数据库连接错误或者是用来填充的参数map有误!");
89. }
90.
91. }
92. }
HTML格式的:
1. /**
2. * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
3. * @author Jimmy.Shine 2007-5-12
4. */
5. package cn.com.reachway.framework.report.export;
6.
7. import java.io.IOException;
8. import java.io.PrintWriter;
9. import java.sql.Connection;
10. import java.util.Map;
11.
12. import javax.servlet.http.HttpServletRequest;
13. import javax.servlet.http.HttpServletResponse;
14.
15. import net.sf.jasperreports.engine.JRDataSource;
16. import net.sf.jasperreports.engine.JRExporterParameter;
17. import net.sf.jasperreports.engine.JasperPrint;
18. import net.sf.jasperreports.engine.export.JRHtmlExporter;
19. import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
20. import net.sf.jasperreports.j2ee.servlets.ImageServlet;
21. import cn.com.reachway.framework.exception.JasperReportException;
22. import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithConnection;
23. import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithDataSource;
24.
25. /**
26. * 利用报表生成HTML格式报表
27. */
28. public class HTMLExport {
29.
30. /**
31. * 导出报表
32. *
33. * @param request
34. * @param response
35. * @param reportFilePath
36. * @param params
37. * @param con
38. * @throws JasperReportException
39. */
40. public void export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,
41. Connection con) throws JasperReportException {
42. try {
43. PrintWriter out = response.getWriter();
44. try {
45. response.setContentType("text/html;charset=UTF-8");
46. JasperPrint jasperPrint = new JasperPrintWithConnection(reportFilePath, params, con).getJasperPrint();
47. // 使用JRHtmlExproter导出Html格式
48. JRHtmlExporter exporter = new JRHtmlExporter();
49. request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
50. exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
51. exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
52. exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "./servlets/image?image=");
53. exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
54. // 导出
55. exporter.exportReport();
56. } catch (Exception e) {
57. e.printStackTrace();
58. throw new JasperReportException("在导出Html格式报表时发生错误!");
59. } finally {
60. if (out != null) {
61. try {
62. out.close();
63. } catch (Exception e) {
64. }
65. }
66. }
67. } catch (IOException ioe) {
68. ioe.printStackTrace();
69. throw new JasperReportException("从Response中取得PrintWriter时发生错误!");
70. }
71. }
72.
73. /**
74. * 导出报表
75. *
76. * @param request
77. * @param response
78. * @param reportFilePath
79. * @param params
80. * @param dataSource
81. * @throws JasperReportException
82. */
83. public void export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,
84. JRDataSource dataSource) throws JasperReportException {
85. try {
86. PrintWriter out = response.getWriter();
87. try {
88. response.setContentType("text/html;charset=UTF-8");
89. JasperPrint jasperPrint = new JasperPrintWithDataSource(reportFilePath, params, dataSource)
90. .getJasperPrint();
91. // 使用JRHtmlExproter导出Html格式
92. JRHtmlExporter exporter = new JRHtmlExporter();
93. request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
94. exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
95. exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
96. exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "./servlets/image?image=");
97. exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
98. // 导出
99. exporter.exportReport();
100. } catch (Exception e) {
101. e.printStackTrace();
102. throw new JasperReportException("在导出Html格式报表时发生错误!");
103. } finally {
104. if (out != null) {
105. try {
106. out.close();
107. } catch (Exception e) {
108. }
109. }
110. }
111. } catch (IOException ioe) {
112. ioe.printStackTrace();
113. throw new JasperReportException("从Response中取得PrintWriter时发生错误!");
114. }
115. }
116.
117. }
/**
* @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
* @author Jimmy.Shine 2007-5-12
*/
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;
/**
* 利用报表生成HTML格式报表
*/
public class HTMLExport {
/**
* 导出报表
*
* @param request
* @param response
* @param reportFilePath
* @param params
* @param con
* @throws JasperReportException
*/
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();
// 使用JRHtmlExproter导出Html格式
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时发生错误!");
}
}
/**
* 导出报表
*
* @param request
* @param response
* @param reportFilePath
* @param params
* @param dataSource
* @throws JasperReportException
*/
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();
// 使用JRHtmlExproter导出Html格式
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格式的:
1. /**
2. * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
3. * @author Jimmy.Shine 2007-5-13
4. */
5. package cn.com.reachway.framework.report.export;
6.
7. import java.io.IOException;
8. import java.io.OutputStream;
9. import java.net.URLEncoder;
10. import java.sql.Connection;
11. import java.util.List;
12. import java.util.Map;
13.
14. import javax.servlet.http.HttpServletRequest;
15. import javax.servlet.http.HttpServletResponse;
16.
17. import net.sf.jasperreports.engine.JRDataSource;
18. import net.sf.jasperreports.engine.JRException;
19. import net.sf.jasperreports.engine.JRExporterParameter;
20. import net.sf.jasperreports.engine.JasperPrint;
21. import net.sf.jasperreports.engine.export.JRXlsAbstractExporter;
22. import net.sf.jasperreports.engine.export.JRXlsAbstractExporterParameter;
23. import net.sf.jasperreports.j2ee.servlets.BaseHttpServlet;
24. import cn.com.reachway.framework.exception.JasperReportException;
25. import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithConnection;
26. import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithDataSource;
27.
28. /**
29. * 利用报表生成Xls的基类
30. */
31. public abstract class BaseExcelExport {
32. /**
33. * 导出报表
34. *
35. * @param request
36. * @param response
37. * @param reportFilePath
38. * @param params
39. * @param con
40. * @param fileName
41. * @throws JasperReportException
42. */
43. public void export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,
44. Connection con, String fileName) throws JasperReportException {
45. JasperPrint jasperPrint = new JasperPrintWithConnection(reportFilePath, params, con).getJasperPrint();
46. // 将填充完的japserPrint放入session中。
47. request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
48. // 拿到japserPrintList
49. List jasperPrintList = BaseHttpServlet.getJasperPrintList(request);
50. // 若没有JasperPrintList,则抛出异常
51. if (jasperPrintList == null) {
52. throw new JasperReportException("在Http Session中没有找到JasperPrint List");
53. }
54. try {
55. OutputStream ouputStream = response.getOutputStream();
56. try {
57.
58. response.setContentType("application/xls");
59. response.setCharacterEncoding("UTF-8");
60. if (fileName == null || fileName.equals(""))
61. response.setHeader("Content-Disposition", "inline; filename=\"noTitle.xls\"");
62. else {
63. response.setHeader("Content-Disposition", "inline; filename=\""
64. + URLEncoder.encode(fileName, "UTF-8") + ".xls\"");
65.
66. }
67. // Xls格式的导出器 JRXlsAbstractExport
68. JRXlsAbstractExporter exporter = getXlsExporter();
69.
70. // 在导出器中放入要导出的japserPrintList
71. exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
72.
73. exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
74. // 设置Xls的属性
75. exporter.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
76. exporter.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
77. // 导出
78. exporter.exportReport();
79. } catch (JRException e) {
80. e.printStackTrace();
81. throw new JasperReportException("在生成XLS报表时发生错误!");
82. }
83.
84. finally {
85. if (ouputStream != null) {
86. try {
87. ouputStream.close();
88. } catch (IOException ex) {
89. }
90. }
91. }
92. } catch (IOException ioe) {
93. ioe.printStackTrace();
94. throw new JasperReportException("从Response中取得OutputStream时发生错误!");
95. }
96.
97. }
98.
99. /**
100. * 导出报表
101. *
102. * @param request
103. * @param response
104. * @param reportFilePath
105. * @param params
106. * @param dataSource
107. * @param fileName
108. * @throws JasperReportException
109. */
110. public void export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,
111. JRDataSource dataSource, String fileName) throws JasperReportException {
112. JasperPrint jasperPrint = new JasperPrintWithDataSource(reportFilePath, params, dataSource).getJasperPrint();
113. // 将填充完的japserPrint放入session中。
114. request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
115. // 拿到japserPrintList
116. List jasperPrintList = BaseHttpServlet.getJasperPrintList(request);
117. // 若没有JasperPrintList,则抛出异常
118. if (jasperPrintList == null) {
119. throw new JasperReportException("在Http Session中没有找到JasperPrint List");
120. }
121. try {
122. OutputStream ouputStream = response.getOutputStream();
123. try {
124.
125. response.setContentType("application/xls");
126. response.setCharacterEncoding("UTF-8");
127. if (fileName == null || fileName.equals(""))
128. response.setHeader("Content-Disposition", "inline; filename=\"noTitle.xls\"");
129. else {
130. response.setHeader("Content-Disposition", "inline; filename=\""
131. + URLEncoder.encode(fileName, "UTF-8") + ".xls\"");
132.
133. }
134. // Xls格式的导出器 JRXlsAbstractExport
135. JRXlsAbstractExporter exporter = getXlsExporter();
136.
137. // 在导出器中放入要导出的japserPrintList
138. exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
139.
140. exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
141. // 设置Xls的属性
142. exporter.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
143. exporter.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
144. // 导出
145. exporter.exportReport();
146. } catch (JRException e) {
147. e.printStackTrace();
148. throw new JasperReportException("在生成XLS报表时发生错误!");
149. }
150.
151. finally {
152. if (ouputStream != null) {
153. try {
154. ouputStream.close();
155. } catch (IOException ex) {
156. }
157. }
158. }
159. } catch (IOException ioe) {
160. ioe.printStackTrace();
161. throw new JasperReportException("从Response中取得OutputStream时发生错误!");
162. }
163.
164. }
165.
166. protected abstract JRXlsAbstractExporter getXlsExporter();
167.
168. }
/**
* @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
* @author Jimmy.Shine 2007-5-13
*/
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;
/**
* 利用报表生成Xls的基类
*/
public abstract class BaseExcelExport {
/**
* 导出报表
*
* @param request
* @param response
* @param reportFilePath
* @param params
* @param con
* @param fileName
* @throws JasperReportException
*/
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();
// 将填充完的japserPrint放入session中。
request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
// 拿到japserPrintList
List jasperPrintList = BaseHttpServlet.getJasperPrintList(request);
// 若没有JasperPrintList,则抛出异常
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\"");
}
// Xls格式的导出器 JRXlsAbstractExport
JRXlsAbstractExporter exporter = getXlsExporter();
// 在导出器中放入要导出的japserPrintList
exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
// 设置Xls的属性
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时发生错误!");
}
}
/**
* 导出报表
*
* @param request
* @param response
* @param reportFilePath
* @param params
* @param dataSource
* @param fileName
* @throws JasperReportException
*/
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();
// 将填充完的japserPrint放入session中。
request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
// 拿到japserPrintList
List jasperPrintList = BaseHttpServlet.getJasperPrintList(request);
// 若没有JasperPrintList,则抛出异常
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\"");
}
// Xls格式的导出器 JRXlsAbstractExport
JRXlsAbstractExporter exporter = getXlsExporter();
// 在导出器中放入要导出的japserPrintList
exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
// 设置Xls的属性
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();
}
1. /**
2. * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
3. * @author Jimmy.Shine 2007-5-13
4. */
5. package cn.com.reachway.framework.report.export;
6.
7. import net.sf.jasperreports.engine.export.JRXlsAbstractExporter;
8. import net.sf.jasperreports.engine.export.JRXlsExporter;
9.
10. /**
11. * 利用报表使用POI生成XLS报表
12. */
13. public class XlsPOIExport extends BaseExcelExport {
14.
15. protected JRXlsAbstractExporter getXlsExporter() {
16. return new JRXlsExporter();
17. }
18.
19. }
20.
/**
* @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
* @author Jimmy.Shine 2007-5-13
*/
package cn.com.reachway.framework.report.export;
import net.sf.jasperreports.engine.export.JRXlsAbstractExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
/**
* 利用报表使用POI生成XLS报表
*/
public class XlsPOIExport extends BaseExcelExport {
protected JRXlsAbstractExporter getXlsExporter() {
return new JRXlsExporter();
}
}
1. /**
2. * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
3. * @author Jimmy.Shine 2007-5-13
4. */
5. package cn.com.reachway.framework.report.export;
6.
7. import net.sf.jasperreports.engine.export.JExcelApiExporter;
8. import net.sf.jasperreports.engine.export.JRXlsAbstractExporter;
9.
10. /**
11. * 利用报表使用JExcel生成xls报表
12. */
13. public class XlsJExcelExport extends BaseExcelExport {
14.
15. protected JRXlsAbstractExporter getXlsExporter() {
16. return new JExcelApiExporter();
17. }
18. }
PDF格式的:
1. /**
2. * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
3. * @author Jimmy.Shine 2007-5-12
4. */
5. package cn.com.reachway.framework.report.export;
6.
7. import java.io.IOException;
8. import java.io.OutputStream;
9. import java.net.URLEncoder;
10. import java.sql.Connection;
11. import java.util.List;
12. import java.util.Map;
13.
14. import javax.servlet.http.HttpServletRequest;
15. import javax.servlet.http.HttpServletResponse;
16.
17. import net.sf.jasperreports.engine.JRDataSource;
18. import net.sf.jasperreports.engine.JRException;
19. import net.sf.jasperreports.engine.JRExporterParameter;
20. import net.sf.jasperreports.engine.JasperPrint;
21. import net.sf.jasperreports.engine.export.JRPdfExporter;
22. import net.sf.jasperreports.j2ee.servlets.BaseHttpServlet;
23. import cn.com.reachway.framework.exception.JasperReportException;
24. import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithConnection;
25. import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithDataSource;
26.
27. /**
28. *
29. */
30. public class PDFExport {
31.
32. /**
33. * 导出报表
34. *
35. * @param request
36. * @param response
37. * @param reportFilePath
38. * @param params
39. * @param con
40. * @param fileName
41. * @throws JasperReportException
42. */
43. public void export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,
44. Connection con, String fileName) throws JasperReportException {
45.
46. JasperPrint jasperPrint = new JasperPrintWithConnection(reportFilePath, params, con).getJasperPrint();
47. // 将填充完的japserPrint放入session中。
48. request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
49. // 拿到japserPrintList
50. List jasperPrintList = BaseHttpServlet.getJasperPrintList(request);
51. // 若没有JasperPrintList,则抛出异常
52. if (jasperPrintList == null) {
53. throw new JasperReportException("在Http Session中没有找到JasperPrint List");
54. }
55. try {
56. OutputStream ouputStream = response.getOutputStream();
57. try {
58. response.setContentType("application/pdf");
59. response.setCharacterEncoding("UTF-8");
60. if (fileName == null || fileName.equals(""))
61. response.setHeader("Content-Disposition", "inline; filename=\"noTitle.pdf\"");
62. else
63. response.setHeader("Content-Disposition", "inline; filename=\""
64. + URLEncoder.encode(fileName, "UTF-8") + ".pdf\"");
65. // 使用JRPdfExproter导出器导出pdf
66. JRPdfExporter exporter = new JRPdfExporter();
67. // 设置JasperPrintList
68. exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
69.
70. exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
71. exporter.exportRepo
有二种方式,一种就是写jrxml文件,其实就是xml文件,只不过是后缀名不一样罢了。另一种方式更直接,就是生成一个JasperDesign类的实例,在japsperDesign中自己定义模板。jrxml文件也是通过一个JRXmlLoad加载过来,转成JasperDesign类的实例。也就是说写jrxml文件还需要进行解析,加载。现实中我们使用的报表一般格式比较固定,因而可以通过先使用iReport工具生成模板,再加载解析的方式。这种方式简单,而且可见性强。
2)填充数据。
在最新版(2007.4.30发布)的jasperReports1.3.3中,支持了很多的格式,包括了对于Hibernate的支持。填充数据对于我们经常使用的来说,一般是二种方式,一种方式是通过JDBC连接提供数据源,一种就是通过javaBean的集合提供数据源。当然还有web Service的xml文件提供的。我的建议是,如果你的程序中的统计直接使用Jdbc就可以完成,那么就使用jdbc数据源的方法,反之,使用 javaBean的集合是不错的选择,因为这样不会在意你的数据的来源,你也可以任意处理,比如说,要通过权限检查的数据才在报表中生成的话,就可以过滤到不符合权限的数据。
3)显示或打印。
显示,即将JasperReport生成的文件直接显示出来,打印所使用的方式其实就是生成文件,然后调用打印来对文件进行打印。
看起来是不是很简单,对,确实很简单。
笔者主要从事基于B/S模式的开发,相信大部分人的使用也是基于web进行使用。故笔者对于基于web的开发进行详细讲述。
1、工程环境配置。
将以下包加入WEB-INF/lib中。
commons-beanutils-1.7.jar;commons-collections-2.1.jar;commons-digester-1.7.jar;commons-logging-1.0.2.jar;commons-logging-api-1.0.2.jar;itext-1.3.1.jar;jasperreports-1.3.3.jar;jdt-compiler-3.1.1.jar;jxl-2.6.jar;png-encoder-1.5.jar;poi-2.0-final-20040126.jar
以上包是jasperReport必须。
2、iReport的安装
这个很简单,直接下一步就可以了。
3、使用iReport生成 .jasper文件
.jasper是经过编译的japserReport模板,即.jrxml编译后的。
对于iReport的使用网络上已经有了详细的中文的文档,具体可以下载附件查看。使用.jasper文件可以避免在系统中再进行编译,增加系统的压力。
4、数据填充
数据填充是相对于比较重要的一步,数据填充的目的是为了将数据填充进去,生成一个JapserPrint对象。笔者在之前已经阐述过,现在主要是基于关系型数据库(比较简单的,没有权限控制的),以及基于JavaBean Collection进行数据填充。进行数据填充的基本就是一定要实现JRDataSource,JRDataSource定义了二个方法,一个是指针移动的方法next(),另一个是取值的getFieldValue()方法,用来取值填充。
对于关系型数据库,即直接利用数据库的表来生成报表的,只需要传入一个Java Connection即可。JasperReport已经有了个默认的实现的JRDataSource的接口了。对于Java Conncetion的获得,笔者建议使用工厂方法或者使用Spring方式来获得。可以参照笔者写的如下:
源自:http://www.iteye.com/topic/78678
1. /**
2. * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
3. * @author Jimmy.Shine 2007-5-11
4. */
5. package cn.com.reachway.framework.report;
6.
7. import java.sql.Connection;
8. import java.sql.DriverManager;
9.
10. import cn.com.reachway.framework.exception.JasperReportException;
11.
12. /**
13. * Used for get a JDBC connection for Jasper Report
14. */
15. public class JDBCConnection {
16. /**
17. * Which Jdbc driver were used. Like: "net.sourceforge.jtds.jdbc.Driver"
18. */
19. private String jdbcDriver;
20. /**
21. * The jdbc url,define which database server,which db. Like:
22. * "jdbc:jtds:sqlserver://192.168.1.50:1433;DatabaseName=JAVA5;SelectMethod=Cursor"
23. */
24. private String jdbcUrl;
25.
26. private String dbUser;
27.
28. private String dbPassword;
29.
30. public String getDbPassword() {
31. return dbPassword;
32. }
33.
34. public void setDbPassword(String dbPassword) {
35. this.dbPassword = dbPassword;
36. }
37.
38. public String getDbUser() {
39. return dbUser;
40. }
41.
42. public void setDbUser(String dbUser) {
43. this.dbUser = dbUser;
44. }
45.
46. public String getJdbcDriver() {
47. return jdbcDriver;
48. }
49.
50. public void setJdbcDriver(String jdbcDriver) {
51. this.jdbcDriver = jdbcDriver;
52. }
53.
54. public String getJdbcUrl() {
55. return jdbcUrl;
56. }
57.
58. public void setJdbcUrl(String jdbcUrl) {
59. this.jdbcUrl = jdbcUrl;
60. }
61.
62. public JDBCConnection() {
63. super();
64. }
65. /**
66. * Get the Connection
67. * @return connection
68. * @throws JasperReportException
69. */
70. public Connection getConnection() throws JasperReportException {
71. Connection con;
72. try {
73. check();
74. Class.forName(this.jdbcDriver);
75. con = DriverManager.getConnection(this.jdbcUrl, this.dbUser, this.dbPassword);
76. return con;
77. } catch (Exception e) {
78. e.printStackTrace();
79. throw new JasperReportException("Get JDBC Connection Error");
80. }
81. }
82.
83. /**
84. * Check the Configure
85. * @throws JasperReportException
86. */
87. private void check() throws JasperReportException {
88. if (this.jdbcDriver == null || this.jdbcDriver.equals("") || this.jdbcUrl == null || this.jdbcUrl.equals("")
89. || this.dbUser == null || this.dbUser.equals("") || this.dbPassword == null) {
90. throw new JasperReportException("Jdbc Configure error!");
91. }
92. }
93.
94. }
95.
/**
* @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
* @author Jimmy.Shine 2007-5-11
*/
package cn.com.reachway.framework.report;
import java.sql.Connection;
import java.sql.DriverManager;
import cn.com.reachway.framework.exception.JasperReportException;
/**
* Used for get a JDBC connection for Jasper Report
*/
public class JDBCConnection {
/**
* Which Jdbc driver were used. Like: "net.sourceforge.jtds.jdbc.Driver"
*/
private String jdbcDriver;
/**
* The jdbc url,define which database server,which db. Like:
* "jdbc:jtds:sqlserver://192.168.1.50:1433;DatabaseName=JAVA5;SelectMethod=Cursor"
*/
private String jdbcUrl;
private String dbUser;
private String dbPassword;
public String getDbPassword() {
return dbPassword;
}
public void setDbPassword(String dbPassword) {
this.dbPassword = dbPassword;
}
public String getDbUser() {
return dbUser;
}
public void setDbUser(String dbUser) {
this.dbUser = dbUser;
}
public String getJdbcDriver() {
return jdbcDriver;
}
public void setJdbcDriver(String jdbcDriver) {
this.jdbcDriver = jdbcDriver;
}
public String getJdbcUrl() {
return jdbcUrl;
}
public void setJdbcUrl(String jdbcUrl) {
this.jdbcUrl = jdbcUrl;
}
public JDBCConnection() {
super();
}
/**
* Get the Connection
* @return connection
* @throws JasperReportException
*/
public Connection getConnection() throws JasperReportException {
Connection con;
try {
check();
Class.forName(this.jdbcDriver);
con = DriverManager.getConnection(this.jdbcUrl, this.dbUser, this.dbPassword);
return con;
} catch (Exception e) {
e.printStackTrace();
throw new JasperReportException("Get JDBC Connection Error");
}
}
/**
* Check the Configure
* @throws JasperReportException
*/
private void check() throws JasperReportException {
if (this.jdbcDriver == null || this.jdbcDriver.equals("") || this.jdbcUrl == null || this.jdbcUrl.equals("")
|| this.dbUser == null || this.dbUser.equals("") || this.dbPassword == null) {
throw new JasperReportException("Jdbc Configure error!");
}
}
}
1. /**
2. * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
3. * @author Jimmy.Shine 2007-5-14
4. */
5. package cn.com.reachway.framework.report.dataSource;
6.
7. import net.sf.jasperreports.engine.JRDataSource;
8. import net.sf.jasperreports.engine.JRException;
9. import net.sf.jasperreports.engine.JRField;
10.
11. /**
12. * 报表的dataSource的基类,所有利用DataSource产生报表的继承此类
13. */
14. public abstract class ReportDataSource implements JRDataSource {
15.
16. /**
17. * 取值
18. */
19. public abstract Object getFieldValue(JRField jrField) throws JRException;
20.
21. /**
22. * 指针移动
23. */
24. public abstract boolean next() throws JRException;
25.
26. }
27.
/**
* @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
* @author Jimmy.Shine 2007-5-14
*/
package cn.com.reachway.framework.report.dataSource;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;
/**
* 报表的dataSource的基类,所有利用DataSource产生报表的继承此类
*/
public abstract class ReportDataSource implements JRDataSource {
/**
* 取值
*/
public abstract Object getFieldValue(JRField jrField) throws JRException;
/**
* 指针移动
*/
public abstract boolean next() throws JRException;
}
这样可以共用配置文件中的jdbc连接的相关参数。
对于其它的数据源,JavaBean Collection,笔者采用了一个抽象类,使用抽象类的目的,是为了使开发者尽可能不用直接接触jasperReport(此为笔者自己的方式,笔者是PM,为了考虑底下的人的开发)。以下为抽象类以及笔者提供的一个sample:
1. /**
2. * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
3. * @author Jimmy.Shine 2007-5-14
4. */
5. package cn.com.reachway.framework.report.dataSource;
6.
7. import java.util.ArrayList;
8. import java.util.List;
9.
10. import net.sf.jasperreports.engine.JRException;
11. import net.sf.jasperreports.engine.JRField;
12.
13. /**
14. * 利用JavaBean Collection生成ReportDataSource的例子
15. */
16. public class ReportDataSourceSample extends ReportDataSource {
17.
18. private List docs = new ArrayList();
19.
20. private int index = -1;
21.
22. public ReportDataSourceSample() {
23. for (int i = 0; i < 10; i++) {
24. Document doc = new Document("ViewTimes is:" + i, i);
25. this.docs.add(doc);
26. }
27. }
28.
29. // 内部类,作为demo时使用
30. private class Document {
31. private String name;
32.
33. private int viewTimes;
34.
35. public String getName() {
36. return name;
37. }
38.
39. public void setName(String name) {
40. this.name = name;
41. }
42.
43. public int getViewTimes() {
44. return viewTimes;
45. }
46.
47. public void setViewTimes(int viewTimes) {
48. this.viewTimes = viewTimes;
49. }
50.
51. public Document() {
52. }
53.
54. public Document(String name, int viewTimes) {
55. this.name = name;
56. this.viewTimes = viewTimes;
57. }
58. }
59.
60. public List getDocs() {
61. return docs;
62. }
63.
64. public void setDocs(List docs) {
65. this.docs = docs;
66. }
67.
68. @Override
69. public Object getFieldValue(JRField jrField) throws JRException {
70.
71. String fieldName = jrField.getName();
72. Document doc = this.docs.get(index);
73. if ("name".equals(fieldName)) {
74. return doc.getName();
75. }
76. if ("viewTimes".equals(fieldName)) {
77. return doc.getViewTimes();
78. }
79. return null;
80. }
81.
82. @Override
83. public boolean next() throws JRException {
84. index++;
85. return (index < this.docs.size());
86. }
87. }
88.
/**
* @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
* @author Jimmy.Shine 2007-5-14
*/
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;
/**
* 利用JavaBean Collection生成ReportDataSource的例子
*/
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);
}
}
// 内部类,作为demo时使用
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());
}
}
以上的例子应当很清楚的写明了如何生成数据源。
对于数据源的填充,笔者使用了二个类,分别用来对应使用Connction及JavaBean Collection进行填充。
1. /**
2. * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
3. * @author Jimmy.Shine 2007-5-12
4. */
5. package cn.com.reachway.framework.report.jasperPrint;
6.
7. import java.io.File;
8. import java.sql.Connection;
9. import java.util.Map;
10.
11. import net.sf.jasperreports.engine.JRException;
12. import net.sf.jasperreports.engine.JasperFillManager;
13. import net.sf.jasperreports.engine.JasperPrint;
14. import net.sf.jasperreports.engine.JasperReport;
15. import net.sf.jasperreports.engine.util.JRLoader;
16. import cn.com.reachway.framework.exception.JasperReportException;
17.
18. /**
19. * 使用报表模板及数据等来生成JapserPrint
20. */
21. public class JasperPrintWithConnection {
22. /** 传入的参数 */
23. private Map params;
24. /** 模板文件的地址 */
25. private String reportFilePath;
26. /** JDBC connection */
27. private Connection con;
28.
29. public Connection getCon() {
30. return con;
31. }
32.
33. public void setCon(Connection con) {
34. this.con = con;
35. }
36.
37. public Map getParams() {
38. return params;
39. }
40.
41. public void setParams(Map params) {
42. this.params = params;
43. }
44.
45. public String getReportFilePath() {
46. return reportFilePath;
47. }
48.
49. public void setReportFilePath(String reportFilePath) throws JasperReportException {
50. if (reportFilePath == null || !reportFilePath.endsWith(".jasper"))
51. throw new JasperReportException("您传入的模板文件格式不对,请传入以.jasper为后缀的文件!");
52. this.reportFilePath = reportFilePath;
53. }
54.
55. public JasperPrintWithConnection() {
56. super();
57. }
58.
59. public JasperPrintWithConnection(String reportFilePath, Map params, Connection con) throws JasperReportException {
60. if (reportFilePath == null || !reportFilePath.endsWith(".jasper"))
61. throw new JasperReportException("模板文件格式不对,请传入以.jasper为后缀的文件!");
62. if (con == null)
63. throw new JasperReportException("Conncetion不应当为null!");
64. this.setReportFilePath(reportFilePath);
65. this.setParams(params);
66. this.setCon(con);
67. }
68.
69. /**
70. * 取得JasperPrint
71. * @return
72. * @throws JasperReportException
73. */
74. public JasperPrint getJasperPrint() throws JasperReportException {
75. File reportFile = new File(this.reportFilePath);
76. if (!reportFile.exists())
77. throw new JasperReportException("传入的模板文件不存在!");
78.
79. try {
80. // Load编译好的模板
81. JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile.getPath());
82. // 进行数据填充
83. JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, this.params, this.con);
84. return jasperPrint;
85.
86. } catch (JRException jre) {
87. jre.printStackTrace();
88. throw new JasperReportException("在进行数据填充时发生了错误中,请检查是否是数据库连接错误或者是用来填充的参数map有误!");
89. }
90.
91. }
92. }
93.
/**
* @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
* @author Jimmy.Shine 2007-5-12
*/
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;
/**
* 使用报表模板及数据等来生成JapserPrint
*/
public class JasperPrintWithConnection {
/** 传入的参数 */
private Map params;
/** 模板文件的地址 */
private String reportFilePath;
/** JDBC connection */
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);
}
/**
* 取得JasperPrint
* @return
* @throws JasperReportException
*/
public JasperPrint getJasperPrint() throws JasperReportException {
File reportFile = new File(this.reportFilePath);
if (!reportFile.exists())
throw new JasperReportException("传入的模板文件不存在!");
try {
// Load编译好的模板
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有误!");
}
}
}
1. /**
2. * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
3. * @author Jimmy.Shine 2007-5-14
4. */
5. package cn.com.reachway.framework.report.jasperPrint;
6.
7. import java.io.File;
8. import java.util.Map;
9.
10. import net.sf.jasperreports.engine.JRDataSource;
11. import net.sf.jasperreports.engine.JRException;
12. import net.sf.jasperreports.engine.JasperFillManager;
13. import net.sf.jasperreports.engine.JasperPrint;
14. import net.sf.jasperreports.engine.JasperReport;
15. import net.sf.jasperreports.engine.util.JRLoader;
16. import cn.com.reachway.framework.exception.JasperReportException;
17.
18. /**
19. *
20. */
21. public class JasperPrintWithDataSource {
22. /** 传入的参数 */
23. private Map params;
24. /** 模板文件的地址 */
25. private String reportFilePath;
26. /** dataSrouce */
27. private JRDataSource dataSource;
28.
29. public JRDataSource getDataSource() {
30. return dataSource;
31. }
32.
33. public void setDataSource(JRDataSource dataSource) {
34. this.dataSource = dataSource;
35. }
36.
37. public Map getParams() {
38. return params;
39. }
40.
41. public void setParams(Map params) {
42. this.params = params;
43. }
44.
45. public String getReportFilePath() {
46. return reportFilePath;
47. }
48.
49. public void setReportFilePath(String reportFilePath) throws JasperReportException {
50. if (reportFilePath == null || !reportFilePath.endsWith(".jasper"))
51. throw new JasperReportException("您传入的模板文件格式不对,请传入以.jasper为后缀的文件!");
52. this.reportFilePath = reportFilePath;
53. }
54.
55. public JasperPrintWithDataSource() {
56. super();
57. }
58.
59. public JasperPrintWithDataSource(String reportFilePath, Map params, JRDataSource dataSource)
60. throws JasperReportException {
61. if (reportFilePath == null || !reportFilePath.endsWith(".jasper"))
62. throw new JasperReportException("模板文件格式不对,请传入以.jasper为后缀的文件!");
63. if (dataSource == null)
64. throw new JasperReportException("DataSource不应当为null!");
65. this.setReportFilePath(reportFilePath);
66. this.setParams(params);
67. this.setDataSource(dataSource);
68. }
69. /**
70. * 取得JasperPrint
71. * @return
72. * @throws JasperReportException
73. */
74. public JasperPrint getJasperPrint() throws JasperReportException {
75. File reportFile = new File(this.reportFilePath);
76. if (!reportFile.exists())
77. throw new JasperReportException("传入的模板文件不存在!");
78.
79. try {
80. // Load编译好的模板
81. JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile.getPath());
82. // 进行数据填充
83. JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, this.params, this.dataSource);
84. return jasperPrint;
85.
86. } catch (JRException jre) {
87. jre.printStackTrace();
88. throw new JasperReportException("在进行数据填充时发生了错误中,请检查是否是数据库连接错误或者是用来填充的参数map有误!");
89. }
90.
91. }
92. }
HTML格式的:
1. /**
2. * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
3. * @author Jimmy.Shine 2007-5-12
4. */
5. package cn.com.reachway.framework.report.export;
6.
7. import java.io.IOException;
8. import java.io.PrintWriter;
9. import java.sql.Connection;
10. import java.util.Map;
11.
12. import javax.servlet.http.HttpServletRequest;
13. import javax.servlet.http.HttpServletResponse;
14.
15. import net.sf.jasperreports.engine.JRDataSource;
16. import net.sf.jasperreports.engine.JRExporterParameter;
17. import net.sf.jasperreports.engine.JasperPrint;
18. import net.sf.jasperreports.engine.export.JRHtmlExporter;
19. import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
20. import net.sf.jasperreports.j2ee.servlets.ImageServlet;
21. import cn.com.reachway.framework.exception.JasperReportException;
22. import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithConnection;
23. import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithDataSource;
24.
25. /**
26. * 利用报表生成HTML格式报表
27. */
28. public class HTMLExport {
29.
30. /**
31. * 导出报表
32. *
33. * @param request
34. * @param response
35. * @param reportFilePath
36. * @param params
37. * @param con
38. * @throws JasperReportException
39. */
40. public void export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,
41. Connection con) throws JasperReportException {
42. try {
43. PrintWriter out = response.getWriter();
44. try {
45. response.setContentType("text/html;charset=UTF-8");
46. JasperPrint jasperPrint = new JasperPrintWithConnection(reportFilePath, params, con).getJasperPrint();
47. // 使用JRHtmlExproter导出Html格式
48. JRHtmlExporter exporter = new JRHtmlExporter();
49. request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
50. exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
51. exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
52. exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "./servlets/image?image=");
53. exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
54. // 导出
55. exporter.exportReport();
56. } catch (Exception e) {
57. e.printStackTrace();
58. throw new JasperReportException("在导出Html格式报表时发生错误!");
59. } finally {
60. if (out != null) {
61. try {
62. out.close();
63. } catch (Exception e) {
64. }
65. }
66. }
67. } catch (IOException ioe) {
68. ioe.printStackTrace();
69. throw new JasperReportException("从Response中取得PrintWriter时发生错误!");
70. }
71. }
72.
73. /**
74. * 导出报表
75. *
76. * @param request
77. * @param response
78. * @param reportFilePath
79. * @param params
80. * @param dataSource
81. * @throws JasperReportException
82. */
83. public void export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,
84. JRDataSource dataSource) throws JasperReportException {
85. try {
86. PrintWriter out = response.getWriter();
87. try {
88. response.setContentType("text/html;charset=UTF-8");
89. JasperPrint jasperPrint = new JasperPrintWithDataSource(reportFilePath, params, dataSource)
90. .getJasperPrint();
91. // 使用JRHtmlExproter导出Html格式
92. JRHtmlExporter exporter = new JRHtmlExporter();
93. request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
94. exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
95. exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
96. exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "./servlets/image?image=");
97. exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
98. // 导出
99. exporter.exportReport();
100. } catch (Exception e) {
101. e.printStackTrace();
102. throw new JasperReportException("在导出Html格式报表时发生错误!");
103. } finally {
104. if (out != null) {
105. try {
106. out.close();
107. } catch (Exception e) {
108. }
109. }
110. }
111. } catch (IOException ioe) {
112. ioe.printStackTrace();
113. throw new JasperReportException("从Response中取得PrintWriter时发生错误!");
114. }
115. }
116.
117. }
/**
* @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
* @author Jimmy.Shine 2007-5-12
*/
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;
/**
* 利用报表生成HTML格式报表
*/
public class HTMLExport {
/**
* 导出报表
*
* @param request
* @param response
* @param reportFilePath
* @param params
* @param con
* @throws JasperReportException
*/
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();
// 使用JRHtmlExproter导出Html格式
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时发生错误!");
}
}
/**
* 导出报表
*
* @param request
* @param response
* @param reportFilePath
* @param params
* @param dataSource
* @throws JasperReportException
*/
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();
// 使用JRHtmlExproter导出Html格式
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格式的:
1. /**
2. * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
3. * @author Jimmy.Shine 2007-5-13
4. */
5. package cn.com.reachway.framework.report.export;
6.
7. import java.io.IOException;
8. import java.io.OutputStream;
9. import java.net.URLEncoder;
10. import java.sql.Connection;
11. import java.util.List;
12. import java.util.Map;
13.
14. import javax.servlet.http.HttpServletRequest;
15. import javax.servlet.http.HttpServletResponse;
16.
17. import net.sf.jasperreports.engine.JRDataSource;
18. import net.sf.jasperreports.engine.JRException;
19. import net.sf.jasperreports.engine.JRExporterParameter;
20. import net.sf.jasperreports.engine.JasperPrint;
21. import net.sf.jasperreports.engine.export.JRXlsAbstractExporter;
22. import net.sf.jasperreports.engine.export.JRXlsAbstractExporterParameter;
23. import net.sf.jasperreports.j2ee.servlets.BaseHttpServlet;
24. import cn.com.reachway.framework.exception.JasperReportException;
25. import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithConnection;
26. import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithDataSource;
27.
28. /**
29. * 利用报表生成Xls的基类
30. */
31. public abstract class BaseExcelExport {
32. /**
33. * 导出报表
34. *
35. * @param request
36. * @param response
37. * @param reportFilePath
38. * @param params
39. * @param con
40. * @param fileName
41. * @throws JasperReportException
42. */
43. public void export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,
44. Connection con, String fileName) throws JasperReportException {
45. JasperPrint jasperPrint = new JasperPrintWithConnection(reportFilePath, params, con).getJasperPrint();
46. // 将填充完的japserPrint放入session中。
47. request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
48. // 拿到japserPrintList
49. List jasperPrintList = BaseHttpServlet.getJasperPrintList(request);
50. // 若没有JasperPrintList,则抛出异常
51. if (jasperPrintList == null) {
52. throw new JasperReportException("在Http Session中没有找到JasperPrint List");
53. }
54. try {
55. OutputStream ouputStream = response.getOutputStream();
56. try {
57.
58. response.setContentType("application/xls");
59. response.setCharacterEncoding("UTF-8");
60. if (fileName == null || fileName.equals(""))
61. response.setHeader("Content-Disposition", "inline; filename=\"noTitle.xls\"");
62. else {
63. response.setHeader("Content-Disposition", "inline; filename=\""
64. + URLEncoder.encode(fileName, "UTF-8") + ".xls\"");
65.
66. }
67. // Xls格式的导出器 JRXlsAbstractExport
68. JRXlsAbstractExporter exporter = getXlsExporter();
69.
70. // 在导出器中放入要导出的japserPrintList
71. exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
72.
73. exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
74. // 设置Xls的属性
75. exporter.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
76. exporter.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
77. // 导出
78. exporter.exportReport();
79. } catch (JRException e) {
80. e.printStackTrace();
81. throw new JasperReportException("在生成XLS报表时发生错误!");
82. }
83.
84. finally {
85. if (ouputStream != null) {
86. try {
87. ouputStream.close();
88. } catch (IOException ex) {
89. }
90. }
91. }
92. } catch (IOException ioe) {
93. ioe.printStackTrace();
94. throw new JasperReportException("从Response中取得OutputStream时发生错误!");
95. }
96.
97. }
98.
99. /**
100. * 导出报表
101. *
102. * @param request
103. * @param response
104. * @param reportFilePath
105. * @param params
106. * @param dataSource
107. * @param fileName
108. * @throws JasperReportException
109. */
110. public void export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,
111. JRDataSource dataSource, String fileName) throws JasperReportException {
112. JasperPrint jasperPrint = new JasperPrintWithDataSource(reportFilePath, params, dataSource).getJasperPrint();
113. // 将填充完的japserPrint放入session中。
114. request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
115. // 拿到japserPrintList
116. List jasperPrintList = BaseHttpServlet.getJasperPrintList(request);
117. // 若没有JasperPrintList,则抛出异常
118. if (jasperPrintList == null) {
119. throw new JasperReportException("在Http Session中没有找到JasperPrint List");
120. }
121. try {
122. OutputStream ouputStream = response.getOutputStream();
123. try {
124.
125. response.setContentType("application/xls");
126. response.setCharacterEncoding("UTF-8");
127. if (fileName == null || fileName.equals(""))
128. response.setHeader("Content-Disposition", "inline; filename=\"noTitle.xls\"");
129. else {
130. response.setHeader("Content-Disposition", "inline; filename=\""
131. + URLEncoder.encode(fileName, "UTF-8") + ".xls\"");
132.
133. }
134. // Xls格式的导出器 JRXlsAbstractExport
135. JRXlsAbstractExporter exporter = getXlsExporter();
136.
137. // 在导出器中放入要导出的japserPrintList
138. exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
139.
140. exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
141. // 设置Xls的属性
142. exporter.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
143. exporter.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
144. // 导出
145. exporter.exportReport();
146. } catch (JRException e) {
147. e.printStackTrace();
148. throw new JasperReportException("在生成XLS报表时发生错误!");
149. }
150.
151. finally {
152. if (ouputStream != null) {
153. try {
154. ouputStream.close();
155. } catch (IOException ex) {
156. }
157. }
158. }
159. } catch (IOException ioe) {
160. ioe.printStackTrace();
161. throw new JasperReportException("从Response中取得OutputStream时发生错误!");
162. }
163.
164. }
165.
166. protected abstract JRXlsAbstractExporter getXlsExporter();
167.
168. }
/**
* @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
* @author Jimmy.Shine 2007-5-13
*/
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;
/**
* 利用报表生成Xls的基类
*/
public abstract class BaseExcelExport {
/**
* 导出报表
*
* @param request
* @param response
* @param reportFilePath
* @param params
* @param con
* @param fileName
* @throws JasperReportException
*/
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();
// 将填充完的japserPrint放入session中。
request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
// 拿到japserPrintList
List jasperPrintList = BaseHttpServlet.getJasperPrintList(request);
// 若没有JasperPrintList,则抛出异常
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\"");
}
// Xls格式的导出器 JRXlsAbstractExport
JRXlsAbstractExporter exporter = getXlsExporter();
// 在导出器中放入要导出的japserPrintList
exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
// 设置Xls的属性
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时发生错误!");
}
}
/**
* 导出报表
*
* @param request
* @param response
* @param reportFilePath
* @param params
* @param dataSource
* @param fileName
* @throws JasperReportException
*/
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();
// 将填充完的japserPrint放入session中。
request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
// 拿到japserPrintList
List jasperPrintList = BaseHttpServlet.getJasperPrintList(request);
// 若没有JasperPrintList,则抛出异常
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\"");
}
// Xls格式的导出器 JRXlsAbstractExport
JRXlsAbstractExporter exporter = getXlsExporter();
// 在导出器中放入要导出的japserPrintList
exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
// 设置Xls的属性
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();
}
1. /**
2. * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
3. * @author Jimmy.Shine 2007-5-13
4. */
5. package cn.com.reachway.framework.report.export;
6.
7. import net.sf.jasperreports.engine.export.JRXlsAbstractExporter;
8. import net.sf.jasperreports.engine.export.JRXlsExporter;
9.
10. /**
11. * 利用报表使用POI生成XLS报表
12. */
13. public class XlsPOIExport extends BaseExcelExport {
14.
15. protected JRXlsAbstractExporter getXlsExporter() {
16. return new JRXlsExporter();
17. }
18.
19. }
20.
/**
* @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
* @author Jimmy.Shine 2007-5-13
*/
package cn.com.reachway.framework.report.export;
import net.sf.jasperreports.engine.export.JRXlsAbstractExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
/**
* 利用报表使用POI生成XLS报表
*/
public class XlsPOIExport extends BaseExcelExport {
protected JRXlsAbstractExporter getXlsExporter() {
return new JRXlsExporter();
}
}
1. /**
2. * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
3. * @author Jimmy.Shine 2007-5-13
4. */
5. package cn.com.reachway.framework.report.export;
6.
7. import net.sf.jasperreports.engine.export.JExcelApiExporter;
8. import net.sf.jasperreports.engine.export.JRXlsAbstractExporter;
9.
10. /**
11. * 利用报表使用JExcel生成xls报表
12. */
13. public class XlsJExcelExport extends BaseExcelExport {
14.
15. protected JRXlsAbstractExporter getXlsExporter() {
16. return new JExcelApiExporter();
17. }
18. }
PDF格式的:
1. /**
2. * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
3. * @author Jimmy.Shine 2007-5-12
4. */
5. package cn.com.reachway.framework.report.export;
6.
7. import java.io.IOException;
8. import java.io.OutputStream;
9. import java.net.URLEncoder;
10. import java.sql.Connection;
11. import java.util.List;
12. import java.util.Map;
13.
14. import javax.servlet.http.HttpServletRequest;
15. import javax.servlet.http.HttpServletResponse;
16.
17. import net.sf.jasperreports.engine.JRDataSource;
18. import net.sf.jasperreports.engine.JRException;
19. import net.sf.jasperreports.engine.JRExporterParameter;
20. import net.sf.jasperreports.engine.JasperPrint;
21. import net.sf.jasperreports.engine.export.JRPdfExporter;
22. import net.sf.jasperreports.j2ee.servlets.BaseHttpServlet;
23. import cn.com.reachway.framework.exception.JasperReportException;
24. import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithConnection;
25. import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithDataSource;
26.
27. /**
28. *
29. */
30. public class PDFExport {
31.
32. /**
33. * 导出报表
34. *
35. * @param request
36. * @param response
37. * @param reportFilePath
38. * @param params
39. * @param con
40. * @param fileName
41. * @throws JasperReportException
42. */
43. public void export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,
44. Connection con, String fileName) throws JasperReportException {
45.
46. JasperPrint jasperPrint = new JasperPrintWithConnection(reportFilePath, params, con).getJasperPrint();
47. // 将填充完的japserPrint放入session中。
48. request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
49. // 拿到japserPrintList
50. List jasperPrintList = BaseHttpServlet.getJasperPrintList(request);
51. // 若没有JasperPrintList,则抛出异常
52. if (jasperPrintList == null) {
53. throw new JasperReportException("在Http Session中没有找到JasperPrint List");
54. }
55. try {
56. OutputStream ouputStream = response.getOutputStream();
57. try {
58. response.setContentType("application/pdf");
59. response.setCharacterEncoding("UTF-8");
60. if (fileName == null || fileName.equals(""))
61. response.setHeader("Content-Disposition", "inline; filename=\"noTitle.pdf\"");
62. else
63. response.setHeader("Content-Disposition", "inline; filename=\""
64. + URLEncoder.encode(fileName, "UTF-8") + ".pdf\"");
65. // 使用JRPdfExproter导出器导出pdf
66. JRPdfExporter exporter = new JRPdfExporter();
67. // 设置JasperPrintList
68. exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
69.
70. exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
71. exporter.exportRepo
发表评论
-
VB操纵Excel
2010-10-07 22:42 1605全面控制 Excel 首先创建 Excel ... -
转:java处理报表工具比较
2010-10-07 10:30 964试共同条件: 数据总数 ... -
转:open flash chart(ofc)图表
2010-09-24 16:40 2821Java开源项目中制 ... -
Birt Report(Eclipse)
2010-09-21 13:01 1044英文http://www.eclipse.org/birt/p ... -
amchart配置文件学习
2010-08-23 17:22 1244margins控制留白 background里的file ... -
amcharts教程
2010-08-23 09:50 10417Joomla使用过程中使用 ... -
jFreechart柱图常用设置
2010-06-29 14:39 1426<%@ page contentType=&q ... -
JFreeChart教程二
2010-06-29 10:45 1368package test; import java.aw ... -
JFreeChart教程
2010-06-28 16:37 1470《转》 JFreeChart教程( ... -
jxl
2010-06-09 21:58 1805现在正在做的项目中涉及大量的Excel文件导出导入操作,都是使 ... -
按照指定模板生成excel写的workbook
2009-05-07 13:40 1879一篇文章 Java操作Excel之 ... -
转载:excel出力
2009-03-27 17:13 894public void toexcel(HashMap has ...
相关推荐
【标题】:“(三)Ireport制作报表(欢迎加群组http://jaspereport.group.iteye.com)” 在IT行业中,报表制作是数据可视化和业务分析的关键环节。IReport是一款强大的开源报表设计工具,它允许用户创建复杂的报告布局...
《JasperReport中文用户手册》是一份详尽的指南,旨在帮助初学者快速掌握Jaspersoft iReport的使用方法,...通过深入学习这份手册,用户将能够充分利用JasperReport的强大功能,创建出美观、实用、高效率的数据报表。
**jasperReports大部分类库** jasperReports是一款强大的开源报表工具,专为Java环境设计,用于生成静态和动态报表。这个压缩包包含了jasperReports的大部分类库,虽然受到上传大小限制,但它可能比其他非官方源更...
JasperReport是一款强大的开源报表工具,它允许开发者创建复杂、美观的报表,并能与各种应用程序集成,...通过学习和掌握这两者,你将能够提升你的Java应用程序的报表处理能力,为用户提供更专业、多样的数据展现方式。
### Spring MVC 整合 JasperReport 实践指南 #### 概述 本文旨在详细介绍如何将Spring MVC框架与JasperReport报表工具进行有效集成的过程,并解决在此...希望本指南能帮助到正在学习或使用JasperReport的开发者们。
JasperReport是一个强大、灵活的报表生成工具,是开放源代码组织sf.net中的一个java 报表打印工程。...该库完全由Java写成,可以用于在各种Java应用程序,包括J2EE,Web应用程序中生成动态内容。它的主要目的是辅助生成...
通过学习和运行这个示例,你可以更好地理解JasperReports的工作原理,并能动手实践报表的创建和导出。 总结来说,JasperReports是一个强大而灵活的报表工具,它通过iReport提供直观的图形化设计界面,使得非程序员...
主界面:extjs/jasper/pillars.jsp 最近研究了ireport画图形的功能,感觉中间走了很多歪路。想大家不要在重复走别人走过的歪路特此上传源码,供大家参考。还有我用的是oracle数据库 作者:castiel
31. **Spring3 相关资料** - 未提供具体链接,但可以推测这部分可能包含关于Spring框架的知识点,例如: - Spring框架的基本概念和架构 - Spring IoC容器的使用方法 - Spring MVC的实现原理 - Spring AOP的应用...
SpringBoot是一款轻量级的Java应用框架,它简化了在Spring框架上构建应用程序的过程。而JasperReport是一款强大的...通过学习和掌握这一技术,你可以提升企业应用的报表功能,提供更直观、更易读的业务数据分析结果。
### JasperReport中使用多个数据源的方法详解 #### 场景背景 在报表开发过程中,有时我们需要在一个报表中展示不同来源的数据。例如,在同一份报告中同时呈现交叉表和饼状图,而这两部分数据可能源自不同的数据库表...
提起开源的报表工具,不能不说说jaspereport,jasperreport作为著名的开源报表工具,在java程序员中一直享有盛名。但由于其设计主要是针对西式的简单报表,设计起复杂的中式报表来,有点力不从心,所以,在国内的...
jasperreport创建多个List数据源报表 通多个detail实现多个table效果,简单 快捷
**正文** `pyreportjasper` 是一个Python库,它为开发者提供了在Python环境中利用JasperReports库生成专业报告的功能。... 在Python Web开发中,如Flask和Django框架的应用场景下,`pyreportjasper` 可以作为一个高效...
[EN] JasperReports导出器的实现,该导出器从报告中生成Word 2007文档(docx格式)。 该库与JasperReports默认导出器之间的区别是,其任务是在Word文档中生成较少数量的表,即基于文本段落而不是表来获取文档,易于...
提起开源的报表工具,不能不说说jaspereport,jasperreport作为著名的开源报表工具,在java程序员中一直享有盛名。但由于其设计主要是针对西式的简单报表,设计起复杂的中式报表来,有点力不从心,所以,在国内的...