SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 2665|回复: 0
打印 上一主题 下一主题

解决Jasperreport的web打印另一种方法

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2010-6-24 14:47:30 | 只看该作者

解决Jasperreport的web打印另一种方法

原文地址如下 [url=http://hi.baidu.com/three_uncle/blog/item/757bf9890c2c47be0e244425.html:1jf1okhp]http://hi.baidu.com/three_uncle/blog/item/757bf9890c2c47be0e244425.html[/url:1jf1okhp]

转载过来的格式比较混乱请参照原文

[jatoolsprinter|http://www.jatools.com/print] 昨天下了jasperreport的最新版本3.7.3看了下。弄了半天还是没能习惯他的报表制作方式。看了下他的打印是利用applet的来打印
的。而且还需要客户端安装 十几MB的 jre 关于这个好坏,每个人都有自己的看法。今天我这里要说的是利用jatoolsprinter控件来打印jasperreport的报表。控件大小只有100k左右 关于jasperreport的

报表制作这里不做说明,我是在环境中跑了下jasperreport的demo运行界面如下
[img:1jf1okhp]http://bbs.jatools.com/attachments/month_1006/20100624_25438a55b6727a6fd5a3SeFHx2hVc2fz.png[/img:1jf1okhp]

运行JSP Example 如下图

[img:1jf1okhp]http://bbs.jatools.com/attachments/month_1006/20100624_8018119283af4c88962cHvaR4Xiypdit.png[/img:1jf1okhp]

下面我们对demo中的html.jsp进行修改 用[url=http://www.jatools.com/print/:1jf1okhp][url=http://www.jatools.com/print/][color=#810081:1jf1okhp]jatoolsprinter[/color:1jf1okhp][/url:1jf1okhp][/url]来打印Jasperreport 的报


蓝色部分为控件需要的内容
[color=#0000ff:1jf1okhp]<%@ page contentType="text/html; charset=gb2312" language="java" %>[/color:1jf1okhp]
<%@ page errorPage="error.jsp" %>
<%@ page import="datasource.*" %>
<%@ page import="net.sf.jasperreports.engine.*" %>
<%@ page import="net.sf.jasperreports.engine.util.*" %>
<%@ page import="net.sf.jasperreports.engine.export.*" %>
<%@ page import="net.sf.jasperreports.j2ee.servlets.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
[color=#0000ff:1jf1okhp]<html>
<head>[/color:1jf1okhp]
[color=#0000ff:1jf1okhp][color=#000000]<!-- [b:1jf1okhp]插入打印控件[/b:1jf1okhp] -->[/color:1jf1okhp]
<OBJECT ID="jatoolsPrinter" CLASSID="CLSID:B43D3361-D975-4BE2-87FE-057188254255"
codebase="jatoolsPrinter.cab#version=5,3,0,0"></OBJECT>
</head>
<script type="text/javascript">
function doPrint(how)
{

//打印文档对象
var myDoc ={
documents: document, // 打印页面(div)们在本文档中
copyrights : '杰创软件拥有版权 [/color][url][color=#0000ff:1jf1okhp]www.jatools.com'[/color:1jf1okhp][/url] // [b:1jf1okhp]版权声明,必须
[/b:1jf1okhp]
[color=#0000ff:1jf1okhp] };

// 调用打印方法
if(how == '打印预览...')
jatoolsPrinter.printPreview(myDoc,true ); // 打印预览

else if(how == '打印...')
jatoolsPrinter.print(myDoc ,true); // 打印前弹出打印设置对话框

else
jatoolsPrinter.print(myDoc ,false); // 不弹出对话框打印
}
</script>
<body>[/color:1jf1okhp]
[color=#0000ff:1jf1okhp]
<input type="button" value="打印预览...">
<input type="button" value="打印...">
<input type="button" value="打印">

[/color:1jf1okhp]

<%
File reportFile = new File(application.getRealPath("/reports/WebappReport.jasper"));
if (!reportFile.exists())
throw new JRRuntimeException("File WebappReport.jasper not found. The report design must be compiled first.");
JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());
Map parameters = new HashMap();
parameters.put("ReportTitle", "Address Report");
parameters.put("BaseDir", reportFile.getParentFile());

JasperPrint jasperPrint =
JasperFillManager.fillReport(
jasperReport,
parameters,
new WebappDataSource()
);

JRHtmlExporter exporter = new JRHtmlExporter();
session.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.exportReport();
%>
[color=#0000ff:1jf1okhp]</body>
</html>[/color:1jf1okhp]

因为[url=http://www.jatools.com/print/:1jf1okhp][url=http://www.jatools.com/print/][color=#810081:1jf1okhp]jatoolsprinter[/color:1jf1okhp][/url:1jf1okhp][/url]的打印是根据 <div>来区分打印的页的。而Jasperreport生成的

html页面中又没有包含这些信息,所以我们就需要修改下Jasperreport的源码
由生成的报表页面的源码中我们可以看到Jasperreport是利用来区分页数的我们可以找到[color=#ff0000:1jf1okhp]package net.sf.jasperreports.engine.export [/color:1jf1okhp][color=#000000:1jf1okhp]下的[color=#ff0000]

JRHtmlExporter.java[/color:1jf1okhp] 找到代码中的[color=#ff0000:1jf1okhp]exportReportToWriter[/color:1jf1okhp]方法里的如下片段[/color]
[color=#ff0000:1jf1okhp]writer.write("<a name=\"" + JR_PAGE_ANCHOR_PREFIX + reportIndex + "_" + (pageIndex + 1) + "\">\n");[/color:1jf1okhp]
[color=#000000:1jf1okhp]在这段代码之后加上[/color:1jf1okhp]
[color=#0000ff:1jf1okhp]writer.write(" <div>\n");[/color:1jf1okhp]
[color=#000000:1jf1okhp]然后找到源码中的[/color:1jf1okhp]
[color=#ff0000:1jf1okhp]exportPage(page);[/color:1jf1okhp]
[color=#000000:1jf1okhp]在他之后加上div的结束标志[/color:1jf1okhp]
[color=#0000ff:1jf1okhp]writer.write("<div id='page"+ (pageIndex + 1)+"'>\n");[/color:1jf1okhp]
[color=#000000:1jf1okhp]以上代码蓝色部分为后添加的内容[/color:1jf1okhp]


[img:1jf1okhp]http://bbs.jatools.com/attachments/month_1006/20100624_bf81e83671c250f55f13qHAVokhpCG7F.png[/img:1jf1okhp]
[color=#000000:1jf1okhp]修改之后的代码如下[/color:1jf1okhp]
[color=#000000:1jf1okhp]writer.write("<a name=\"" + JR_PAGE_ANCHOR_PREFIX + reportIndex + "_" + (pageIndex + 1) + "\">\n");

[color=#0000ff]writer.write(" <div>\n");
[/color:1jf1okhp] [/color]
[color=#000000:1jf1okhp] /* */
exportPage(page);

[/color:1jf1okhp][color=#0000ff:1jf1okhp]writer.write("</div>
\n");[/color:1jf1okhp]
[color=#000000:1jf1okhp]修改完这些之后我们就把[color=#0000ff]jatoolsPrinter.cab[/color:1jf1okhp]放在项目中html.jsp [/color]
[color=#000000:1jf1okhp][color=#0000ff]<OBJECT ID="jatoolsPrinter" CLASSID="CLSID:B43D3361-D975-4BE2-87FE-057188254255"
codebase="jatoolsPrinter.cab#version=5,3,0,0"></OBJECT>[/color:1jf1okhp]
[/color]
[color=#000000:1jf1okhp]codebase指定的位置这里指定的是和html.jsp同一路径[/color:1jf1okhp]

[img:1jf1okhp]http://bbs.jatools.com/attachments/month_1006/20100624_562b0d5d1e008cd8ce9af98Z9OnrZN1n.png[/img:1jf1okhp]

[color=#000000:1jf1okhp]现在打开IE 运行 demo[/color:1jf1okhp]
[color=#000000:1jf1okhp]第一次运行会提示安装jatoolsprinter控件 选择安装ActiveX控件[/color:1jf1okhp]

[img:1jf1okhp]http://bbs.jatools.com/attachments/month_1006/20100624_5b11948a5e76e5ec03faO08C40o0Aci9.png[/img:1jf1okhp]

[color=#000000:1jf1okhp]点击安装[/color:1jf1okhp]

[img:1jf1okhp]http://bbs.jatools.com/attachments/month_1006/20100624_f0ab06385185f4c4f21c5U1LmKrfKSCL.png[/img:1jf1okhp]

[color=#000000:1jf1okhp]点击打印预览效果如下[/color:1jf1okhp]

[img:1jf1okhp]http://bbs.jatools.com/attachments/month_1006/20100624_16c3a623bb64e6dbeebfQVaOqJLX8CBb.png[/img:1jf1okhp]

到此就可以在web页面中直接打印Jasperreport 报表了。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|SAS中文论坛  

GMT+8, 2024-12-22 09:38 , Processed in 0.153838 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表