- jimmy.shine
- 等级:
- 性别:
- 文章: 100
- 积分: 190
- 来自: 北京
|
由于在一个里面不能贴太多的代码,故放置代码在此处。
上接:http://jimmy-shine.iteye.com/blog/123595
PDF格式的:
java 代码
-
-
-
-
- package cn.com.reachway.framework.report.export;
-
- import java.io.IOException;
- import java.io.OutputStream;
- import java.net.URLEncoder;
- import java.sql.Connection;
- import java.util.List;
- import java.util.Map;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import net.sf.jasperreports.engine.JRDataSource;
- import net.sf.jasperreports.engine.JRException;
- import net.sf.jasperreports.engine.JRExporterParameter;
- import net.sf.jasperreports.engine.JasperPrint;
- import net.sf.jasperreports.engine.export.JRPdfExporter;
- import net.sf.jasperreports.j2ee.servlets.BaseHttpServlet;
- import cn.com.reachway.framework.exception.JasperReportException;
- import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithConnection;
- import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithDataSource;
-
-
-
-
- public class PDFExport {
-
-
-
-
-
-
-
-
-
-
-
-
- public void export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,
- Connection con, String fileName) throws JasperReportException {
-
- JasperPrint jasperPrint = new JasperPrintWithConnection(reportFilePath, params, con).getJasperPrint();
-
- request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
-
- List jasperPrintList = BaseHttpServlet.getJasperPrintList(request);
-
- if (jasperPrintList == null) {
- throw new JasperReportException("在Http Session中没有找到JasperPrint List");
- }
- try {
- OutputStream ouputStream = response.getOutputStream();
- try {
- response.setContentType("application/pdf");
- response.setCharacterEncoding("UTF-8");
- if (fileName == null || fileName.equals(""))
- response.setHeader("Content-Disposition", "inline; filename=\"noTitle.pdf\"");
- else
- response.setHeader("Content-Disposition", "inline; filename=\""
- + URLEncoder.encode(fileName, "UTF-8") + ".pdf\"");
-
- JRPdfExporter exporter = new JRPdfExporter();
-
- exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
-
- exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
- exporter.exportReport();
- } catch (JRException e) {
- e.printStackTrace();
- throw new JasperReportException("在导出pdf格式报表时发生错误");
- } finally {
- if (ouputStream != null) {
- try {
- ouputStream.close();
- } catch (IOException ex) {
- }
- }
- }
- } catch (IOException ioe) {
- ioe.printStackTrace();
- throw new JasperReportException("从Response中取得OutputStream时发生错误!");
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public void export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,
- JRDataSource dataSource, String fileName) throws JasperReportException {
-
- JasperPrint jasperPrint = new JasperPrintWithDataSource(reportFilePath, params, dataSource).getJasperPrint();
-
- request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
-
- List jasperPrintList = BaseHttpServlet.getJasperPrintList(request);
-
- if (jasperPrintList == null) {
- throw new JasperReportException("在Http Session中没有找到JasperPrint List");
- }
- try {
- OutputStream ouputStream = response.getOutputStream();
- try {
- response.setContentType("application/pdf");
- response.setCharacterEncoding("UTF-8");
- if (fileName == null || fileName.equals(""))
- response.setHeader("Content-Disposition", "inline; filename=\"noTitle.pdf\"");
- else
- response.setHeader("Content-Disposition", "inline; filename=\""
- + URLEncoder.encode(fileName, "UTF-8") + ".pdf\"");
-
- JRPdfExporter exporter = new JRPdfExporter();
-
- exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
-
- exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
- exporter.exportReport();
- } catch (JRException e) {
- e.printStackTrace();
- throw new JasperReportException("在导出pdf格式报表时发生错误");
- } finally {
- if (ouputStream != null) {
- try {
- ouputStream.close();
- } catch (IOException ex) {
- }
- }
- }
- } catch (IOException ioe) {
- ioe.printStackTrace();
- throw new JasperReportException("从Response中取得OutputStream时发生错误!");
- }
- }
-
- }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|
- glassprogrammer
- 等级:
- 性别:
- 文章: 113
- 积分: 389
- 来自: 上海
|
引用 # /**
# * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.
# * @author Jimmy.Shine 2007-5-12
# */
这个。。。,貌似放出来不太合适吧
|
返回顶楼 |
|
|
- palmer
- 等级: 初级会员
- 性别:
- 文章: 39
- 积分: 40
- 来自: Edmonton
|
Thank you very much!
|
返回顶楼 |
|
|
- xcg1688
- 等级: 初级会员
- 文章: 1
- 积分: 30
- 来自: ...
|
非常感谢,不过。。。。。。作为刚入门的我,还是没有运行起来,没有得到一个pdf,能不能麻烦您好人做到底?
|
返回顶楼 |
|
|
- flash
- 等级: 初级会员
- 文章: 41
- 积分: 30
- 来自: ...
|
生成pdf的时候,字体设置好麻烦.动不动就出问题.所以还是用excel了.
|
返回顶楼 |
|
|
- lygydl
- 等级: 初级会员
- 性别:
- 文章: 2
- 积分: 30
- 来自: 苏州
|
非常感谢,不过。。。。。。作为刚入门的我,还是没有运行起来,如何架设环境?没有得到一个pdf,能不能麻烦您好人做到底?
|
返回顶楼 |
|
|
- 泡泡
- 等级: 初级会员
- 性别:
- 文章: 61
- 积分: 94
- 来自: 杭州
|
现在Crystal Report有个开发版对Java是免费的,比JasperReport好用多了
|
返回顶楼 |
|
|