C# WEBFORM报表打印问题!

ivanho 2010-05-13 09:05:31
protected void Button1_Click(object sender, EventArgs e)
{

......//为以下参数打开数据库取值;

ReportDocument ocr = new ReportDocument();
ocr.Load(Server.MapPath("~/SYYHPDJB.rpt"));
//第一个参数是报表中的变量,第二个是传递给报表的变量
ocr.SetParameterValue("djzsbh", djzsbh);
ocr.SetParameterValue("hphm", hphm);
ocr.SetParameterValue("hpzl", hpzl);
ocr.SetParameterValue("xm", xm);
ocr.SetParameterValue("zsdz", zsdz);
ocr.SetParameterValue("yzbh", yzbh);
ocr.SetParameterValue("lxdh", lxdh);
ocr.SetParameterValue("sfzhm", sfzhm);
ocr.SetParameterValue("sfzm", sfzm);
ocr.SetParameterValue("sfzhm1", sfzhm1);
ocr.SetParameterValue("sfzm1", sfzm1);
ocr.SetParameterValue("xm1", xm1);
ocr.SetParameterValue("djzsbh", djzsbh);
//ocr.PrintOptions.PrinterName = @"\\Zshz-44f05c55cf\Canon iP1900 series";
ocr.PrintToPrinter(1, false, 0, 0);
}

在VS里运行得很好,代码也没有报错,怪就怪在当发布成为网站后,报表不能打印了。为什么呢?

请各大侠们“拔刀相助”


另:因为需求原因,机器不能连入互联网,为了解决这个问题,我在
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\ASP.NETClientFiles\CrystalReportWebFormViewer4\html
里的“crystalprinthost.html”这个文件进行了修改,令客户端能通过服务器下载“PrintControl.cab”包。
后来想想,会不会是因为我是直接把参数值SET到报表里,再直接从默认打印机打印,从而令JS不能调用“PrintControl.cab”包导致不能打印呢???

...全文
853 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunygd 2012-02-17
  • 打赏
  • 举报
回复
網站打印.樓主最后解決了沒呢?
东莞寻香苑 2010-05-14
  • 打赏
  • 举报
回复
看来越来越复杂了,现在用一个替代的方案,如果不行,真的没有门了

这段代码可以放到以前代码的LOAD事件后面,连CrystalReportView控件也不用了,整个页面当成水晶报表预览(整个页面就是一个打开的PDF文件),客户端可能都要装PDF

......//为以下参数打开数据库取值;

ReportDocument ocr = new ReportDocument();
ocr.Load(Server.MapPath("~/SYYHPDJB.rpt"));
//第一个参数是报表中的变量,第二个是传递给报表的变量
ocr.SetParameterValue("djzsbh", djzsbh);
ocr.SetParameterValue("hphm", hphm);
ocr.SetParameterValue("hpzl", hpzl);
ocr.SetParameterValue("xm", xm);
ocr.SetParameterValue("zsdz", zsdz);
ocr.SetParameterValue("yzbh", yzbh);
ocr.SetParameterValue("lxdh", lxdh);
ocr.SetParameterValue("sfzhm", sfzhm);
ocr.SetParameterValue("sfzm", sfzm);
ocr.SetParameterValue("sfzhm1", sfzhm1);
ocr.SetParameterValue("sfzm1", sfzm1);
ocr.SetParameterValue("xm1", xm1);
ocr.SetParameterValue("djzsbh", djzsbh);
//ocr.PrintOptions.PrinterName = @"\\Zshz-44f05c55cf\Canon iP1900 series";
//ocr.PrintToPrinter(1, false, 0, 0);

//以下设置导出水晶报表的格式,位置及文件类型,一般不用word,excel,而用PDF是因为导出质量更好些
ExportOptions myExportOptions = new ExportOptions();
DiskFileDestinationOptions myDiskFileDestinationOptions = new DiskFileDestinationOptions();
//exports文件夹要有权限并存在
string myFileName = @"C:\exports\" + Session.SessionID.ToString() + ".pdf";
myDiskFileDestinationOptions.DiskFileName = myFileName;
PdfRtfWordFormatOptions myPdfRtfWordFormatOptions = new PdfRtfWordFormatOptions();
myExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
myExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
myDiskFileDestinationOptions.DiskFileName = myFileName;
myExportOptions.ExportDestinationOptions = myDiskFileDestinationOptions;
myExportOptions.ExportFormatOptions = myPdfRtfWordFormatOptions;

ocr.Export(myExportOptions);

//你也可以通什么办法,直接通过客户代码直接打印PDF文件,以取代以下几句Response
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.WriteFile(myFileName);
Response.Flush();
Response.Close();

//删除临时PDF文件
System.IO.File.Delete(myFileName);
阿泰 2010-05-14
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 ivanho 的回复:]

难道就真不能实现客户端自动选择打印机进行打印???麻烦各高手们贡献出可行的DEMO供参考,万分感谢。。
[/Quote]

目前单纯依靠水晶报表工具栏是没法实现客户端自动选择打印机进行打印的

因为从安全性上来讲,默认是不允许页面元素直接访问硬件设备的。
ivanho 2010-05-14
  • 打赏
  • 举报
回复
数据集??这边是直接打开数据库,然后给参数直接赋值的,不存在数据集.

但要是按你的想法,去增加数据,可否说得详细点.或者写一个DEMO
东莞寻香苑 2010-05-14
  • 打赏
  • 举报
回复
protected void Button1_Click(object sender, EventArgs e)
{

......//为以下参数打开数据库取值;

ReportDocument ocr = new ReportDocument();
ocr.Load(Server.MapPath("~/SYYHPDJB.rpt"));
ocr.ocr.SetDataSource(ds);
//第一个参数是报表中的变量,第二个是传递给报表的变量
ocr.SetParameterValue("djzsbh", djzsbh);
ocr.SetParameterValue("hphm", hphm);
ocr.SetParameterValue("hpzl", hpzl);
ocr.SetParameterValue("xm", xm);
ocr.SetParameterValue("zsdz", zsdz);
ocr.SetParameterValue("yzbh", yzbh);
ocr.SetParameterValue("lxdh", lxdh);
ocr.SetParameterValue("sfzhm", sfzhm);
ocr.SetParameterValue("sfzm", sfzm);
ocr.SetParameterValue("sfzhm1", sfzhm1);
ocr.SetParameterValue("sfzm1", sfzm1);
ocr.SetParameterValue("xm1", xm1);
ocr.SetParameterValue("djzsbh", djzsbh);
//ocr.PrintOptions.PrinterName = @"\\Zshz-44f05c55cf\Canon iP1900 series";
ocr.PrintToPrinter(1, false, 0, 0);
}
//红色部分必须要的,ds是你的数据集,加载报表后必须给报表的数据传值
ivanho 2010-05-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 babyt 的回复:]
ocr.PrintToPrinter(1, false, 0, 0);

是从服务器发起的打印命令,根本用不到PrintControl.cab的
PrintControl.cab 是从客户端浏览器发起的打印命令


//ocr.PrintOptions.PrinterName = @"\\Zshz-44f05c55cf\Canon iP1900 series";

这句为啥要注释掉……
[/Quote]

对呀,就是客户端那边,因为不能连入互联网,如果要在报表PrintMode="ActiveX"模式下打印,就必定要下载"PrintControl.cab "包.

至于注释掉那句,是因为想测试下,ocr.PrintToPrinter(1, false, 0, 0);
是否会在没有指定默认打印机,而自动选择默认打印机打印...
ivanho 2010-05-14
  • 打赏
  • 举报
回复
纵综“泰哥”和“csui2008”两位高手的方法,都确定是可以通过服务端进行打印(代码修改后,设置相关权限,能实现打印了。)

但遗憾的是,唉。。。客户那边还有一个网集,打印机通不了网集呀。。。所以我还是选择回头了。(就让他们选择印机呗。)

况且,打印机的数量很多,型号不一,要是都对他们管理进来,代码量非常多,(就是要先确定是那个部门来进行打印,再确定是那个机器名,最后才指定打印机,相信要是做到这样,都“智能”了。唉。。。)

不过话说回来,以前通过
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\ASP.NETClientFiles\CrystalReportWebFormViewer4\html
里的“crystalprinthost.html”这个文件进行修改,是可以实现客户端进行默认打印机打印的,但是系统改版后不适用了。。。郁闷中。。。(奇怪的是用旧版还是能行的)

难道就真不能实现客户端自动选择打印机进行打印???麻烦各高手们贡献出可行的DEMO供参考,万分感谢。。。

阿泰 2010-05-14
  • 打赏
  • 举报
回复
你自己做的一个按钮,那肯定是服务器端打印了。
所以那个cab根本用不上

而且肯定是服务器端打印

注:服务器端打印,打印机不一定非是连在服务器上的,但是必须要作为共享打印机装在服务器上
打印任务从服务器发出,传递给目标打印机

如果用的是工具栏自带的打印功能,那就直接是从本地的打印机发起的打印任务,跟服务器无关

你的代码
//这句必须
ocr.PrintOptions.PrinterName = @"\\Zshz-44f05c55cf\Canon iP1900 series";
ocr.PrintToPrinter(1, false, 0, 0);

你在自己机器上能打,是因为你调试时用的是admin账号,有全部权限
但是IIS上则是用的internet来宾账号,权限很悠闲,一般也没有访问打印机的权限
所以你还要开放打印机的权限
ivanho 2010-05-14
  • 打赏
  • 举报
回复
to:csui2008

我测了一下在本地是没有问题,发布成网站后,客户端打印是有问题的
ocr.PrintToPrinter(1, false, 0, 0);是从服务器端发出命令打印的,
//ocr.PrintOptions.PrinterName = @"\\Zshz-44f05c55cf\Canon iP1900 series";这句是必须的
如果PrintName是本机的,将直接从服务端的打印机打印
如果PrintName是网络打印机,必须保证有访问权限,或把网络打印机映射在服务器,设置比较烦.

是否一定要开放那句代码?

另外,"如果PrintName是本机的,将直接从服务端的打印机打印"
应该是从本地的打印机打印吧!!!
ivanho 2010-05-14
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 babyt 的回复:]
再问一下,请详细描述下“报表不能打印”的症状。

1:点击工具栏的打印按钮,无反应。
2:点击工具栏按钮,可以弹出一个小页面,页面上有个小叉
3:可以打印,但是打印出来时空白
4:有报错信息

具体是那一种?
[/Quote]


是第一种,但不是工具栏按钮(被我屏蔽了),是我自己做的一个按钮.
阿泰 2010-05-14
  • 打赏
  • 举报
回复
再问一下,请详细描述下“报表不能打印”的症状。

1:点击工具栏的打印按钮,无反应。
2:点击工具栏按钮,可以弹出一个小页面,页面上有个小叉
3:可以打印,但是打印出来时空白
4:有报错信息

具体是那一种?
东莞寻香苑 2010-05-14
  • 打赏
  • 举报
回复
我测了一下在本地是没有问题,发布成网站后,客户端打印是有问题的
ocr.PrintToPrinter(1, false, 0, 0);是从服务器端发出命令打印的,
//ocr.PrintOptions.PrinterName = @"\\Zshz-44f05c55cf\Canon iP1900 series";这句是必须的
如果PrintName是本机的,将直接从服务端的打印机打印
如果PrintName是网络打印机,必须保证有访问权限,或把网络打印机映射在服务器,设置比较烦.

PrintToPrinter()可参考:
One limitation of this method is that a printer name must be specified. You can set the default printer at design time in the report, and you can change the printer name at run time by setting the ReportDocument.PrintOptions.PrinterName property (the PrintOptions are also where you can assign page margins, portrait/landscape, etc.). Keep in mind that this method prints from the server itself, not from the client machine. This means that any printer you wish to use must be accessible from the server. You cannot print to a client's desktop printer using this method unless that printer is shared on the network and mapped to the server.

If your LAN has networked printers, you can make some guesses as to which printer to assign as the default in each report. For instance, if the Accounting Department has a departmental printer, that would be a good choice to use for the default in an accounting report. You can provide users the option to choose a networked printer by enumerating the printers mapped to the server and populating a drop-down list. The PrinterSettings.InstalledPrinters property returns a collection of installed printers, and can be bound to a DropDownList as below.

DropDownList1.DataSource = System.Drawing.Printing.PrinterSettings.InstalledPrinters
DropDownList1.DataBind()

Again, since this code is executing on the server, this will only enumerate the printers on the server mapped to the System user. If a printer does not appear in the drop-down list, you need to ensure that it is properly mapped to the System user (see below).

All printers and the .NET Framework need to be mapped to the System user in order for this to work. This is not a trivial task to configure. Printers can be mapped to the system account only by editing the registry, and since the Framework is mapped to the System user, you may need to reassign security permissions on files and folders. This process is outlined in the "Printing Web-Based Reports From the Server" article in the VS .NET help files (look up "Crystal Reports, printing" in the VS help index to link to the articles.) and also in Reference 3 (at the end of this article). The process is a little too intricate to cover here (leave comments if help is needed, and I can amend the article later).

另外的:如果只是你上面所写的这么简单,数据量不多的情况,也可考虑用JavaScript: window.Print或数量大的时候考虑用Export to PDF,但比较慢.

zhangyongtian_0810 2010-05-14
  • 打赏
  • 举报
回复
路过拿分...
阿泰 2010-05-13
  • 打赏
  • 举报
回复
ocr.PrintToPrinter(1, false, 0, 0);

是从服务器发起的打印命令,根本用不到PrintControl.cab的
PrintControl.cab 是从客户端浏览器发起的打印命令


//ocr.PrintOptions.PrinterName = @"\\Zshz-44f05c55cf\Canon iP1900 series";

这句为啥要注释掉呢?

4,820

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 图表区
社区管理员
  • 图表区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧