折腾 VS2008 + ReportViewer + CrystalReports + PrintControl.cab 实现水晶报表本地打印
曾经在VS2005时,找了很多资料折腾了一个通宵把网页本地打印折腾出来了,微软的有些东西虽然好用吧,报表这个总是弄不好一样,非常想念Microsoft Visual FoxPro 的报表,大概都是10年前的事情了,用那个VFP做报表,又好用又简单,功能也很强大,特别是排版非常容易,不知道微软都在搞啥网页报表就是折腾总是会遇到很多问题,IE8以前的版本的网页打印也很若,非常闹心,想实现一个最简单的网页打印功能就好比上天一样折腾人,也可能是我的水平有问题吧,不过我们很多同事也弄不好这个。
接下来直接谈我的折腾过程:
1. VS2008 安装好后,应该是自带一个 CrystalReports ,总感觉 CrystalReports 不是微软的,好像是被SAP收购了一样,具体我不是很清楚,而且各种版本特别多,有时候到底要引用哪个版本也是让人头疼一些。
2. 想实现本地打印,我总感觉需要下载一个微软的 ReportViewer,而且是For VS2008的,也有For VS2005的,我也没测试过这个是否是必要的组件,装上就可以了,这个是在网页里显示报表用的。
3. PrintControl 比较折腾人,也后很多版本的,For VS2008 需要在网络上下载,写明链接的地方非常少,http://support.businessobjects.com/CRforVS2005/PrintControl.cab 这个地址是 For VS2005的,我自己想象了一下把2005修改为2008后,能下载 For VS2008 具体地址如下 http://support.businessobjects.com/CRforVS2005/PrintControl.cab
4. 写一个html文件,自动安装ActiveX控件,这样报表就可以在本地打印了,若还不行,别忘记重新启动IE,重新启动电脑,还不行,就升级到IE8,再不行,先休息一下,再到网络上找找其他资料吧。
<html>
<object id="CrystalPrintControl" classid="CLSID:83A3D1E4-ADC6-434D-9B61-B8CBA6183441"
codebase="http://www.cnblogs.com/../UploadFiles/ActiveX/PrintControl.cab" version="10,5,1,2285"
viewastext>
</object>
</html>
这里就是 classid、version 等的配置比较闹心,因为不同的版本,这些号码都不一样,若对不上,就会崩溃了,
先通过这个网页,可以先把客户端打印的控件装上。
5. 接着就是配置 web.cong 了,我把我的配置文件贴上来,有需要的朋友可以参考,这里需要注意的是
<section name="printControl" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, Custom=null" />
这里的Version也有很多种类的,我按最新的10.??多少一用,就会出错,我又退回来用这个,程序就正常了。
Code
<?xml version="1.0"?>
<configuration>
<configSections>
<!--magicAjax配置-->
<section name="magicAjax" type="MagicAjax.Configuration.MagicAjaxSectionHandler, MagicAjax"/>
<!--结束magicAjax配置-->
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
</sectionGroup>
</sectionGroup>
</sectionGroup>
<sectionGroup name="businessObjects">
<sectionGroup name="crystalReports">
<section name="printControl" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, Custom=null" />
</sectionGroup>
</sectionGroup>
</configSections>
<businessObjects>
<crystalReports>
<printControl>
<add key="url" value="http://192.168.10.89/UploadFiles/ActiveX/PrintControl.cab" />
</printControl>
</crystalReports>
</businessObjects>
<appSettings>
<add key="DbConnectionString" value="Server=192.168.10.88;Database=LinAnWater;Uid=sa;Pwd=sa;max pool size=30;min pool size=10" />
<add key="RecordLog" value="True" />
<add key="ChartImageHandler" value="storage=file;timeout=20;dir=c:\TempImageFiles\;" />
<add key="CrystalImageCleaner-AutoStart" value="true" />
<add key="CrystalImageCleaner-Sleep" value="60000" />
<add key="CrystalImageCleaner-Age" value="120000" />
<add key="LeaderEmail" value="mailto:luyungang33@gmail.com" />
</appSettings>
<connectionStrings/>
<!--magicAjax配置-->
<magicAjax scriptPath="~/JavaScript" outputCompareMode="HashCode" tracing="false">
<pageStore mode="NoStore" unloadStoredPage="false" cacheTimeout="5" maxConcurrentPages="5" maxPagesLimitAlert="false"/>
</magicAjax>
<!--结束magicAjax配置-->
<system.web>
<httpRuntime maxRequestLength="400000" useFullyQualifiedRedirectUrl="true" executionTimeout="45"/>
<!--
设置 compilation debug="true" 可将调试符号插入
已编译的页面中。但由于这会
影响性能,因此只在开发过程中将此值
设置为 true。
-->
<compilation debug="true">
<assemblies>
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="CrystalDecisions.Web, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/>
<add assembly="CrystalDecisions.Shared, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/>
<add assembly="CrystalDecisions.ReportSource, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/>
<add assembly="CrystalDecisions.CrystalReports.Engine, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/>
<add assembly="CrystalDecisions.ReportAppServer.ClientDoc, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
<add assembly="CrystalDecisions.Enterprise.Framework, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
<add assembly="CrystalDecisions.Enterprise.InfoStore, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
<add assembly="Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="Microsoft.ReportViewer.Common, Version=9.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
</assemblies>
</compilation>
<authentication mode="Windows"/>
<customErrors mode="Off"/>
<pages>
<controls>
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add tagPrefix="asp" namespace="System.Web.UI.DataVisualization.Charting" assembly="System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</controls>
</pages>
<httpHandlers>
<remove path="*.asmx" verb="*"/>
<add path="*.asmx" verb="*" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
<add path="CrystalImageHandler.aspx" verb="GET" type="CrystalDecisions.Web.CrystalImageHandler, CrystalDecisions.Web, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
<add path="*_AppService.axd" verb="*" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
<add path="ScriptResource.axd" verb="GET,HEAD" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
<add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" validate="false"/>
<add verb="GET" path="CrystalImageHandler.aspx" type="CrystalDecisions.Web.CrystalImageHandler, CrystalDecisions.Web, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
<add path="ChartImg.axd" verb="GET,HEAD" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
<add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro" />
</httpHandlers>
<identity impersonate="false"/>
<httpModules>
<!--magicAjax配置-->
<add name="MagicAjaxModule" type="MagicAjax.MagicAjaxModule, MagicAjax"/>
<!--结束magicAjax配置-->
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</httpModules>
</system.web>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<providerOption name="CompilerVersion" value="v3.5"/>
<providerOption name="WarnAsError" value="false"/>
</compiler>
</compilers>
</system.codedom>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules>
<remove name="ScriptModule"/>
<add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</modules>
<handlers>
<remove name="WebServiceHandlerFactory-Integrated"/>
<remove name="ScriptHandlerFactory"/>
<remove name="ScriptHandlerFactoryAppServices"/>
<remove name="ScriptResource"/>
<remove name="ChartImageHandler"/>
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ChartImageHandler" preCondition="integratedMode" verb="GET,HEAD" path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add name="CrystalImageHandler.aspx_GET" verb="GET" path="CrystalImageHandler.aspx" type="CrystalDecisions.Web.CrystalImageHandler, CrystalDecisions.Web, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" preCondition="integratedMode"/>
</handlers>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
6. 网页部分的代码参考如下:
<%@ Page Language="C#" AutoEventWireup="true" Inherits="Water.Web.ReXianPriview"
CodeBehind="ReXianPriview.aspx.cs" %>
<%@ Register Assembly="CrystalDecisions.Web, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"
Namespace="CrystalDecisions.Web" TagPrefix="CR" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>打印报表</title>
</head>
<body topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0" style="overflow: scroll;
overflow-x: hidden">
<form id="form1" runat="server">
<CR:CrystalReportViewer ID="ReportViewer" runat="server" Height="50px" Width="350px"
DisplayGroupTree="False" EnableDatabaseLogonPrompt="False" HasCrystalLogo="False"
HasDrillUpButton="False" HasGotoPageButton="False" HasPageNavigationButtons="False"
HasSearchButton="False" HasToggleGroupTreeButton="False" HasViewList="False"
EnableParameterPrompt="False" PrintMode="ActiveX"
HasRefreshButton="True" />
<asp:HiddenField ID="txtID" runat="server" />
</form>
</body>
</html>
7. 后台参考代码如下:
Code
//------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2009 , JiriSoft , Ltd .
//------------------------------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
namespace Water.Web
{
using Water.Model;
using Water.Business;
using DotNet.Common.Model;
using DotNet.Common.Utilities;
using DotNet.Common.DbUtilities;
using DotNet.Common.Business;
using DotNet.Common.Service;
using DotNet.Common;
/// <remarks>
/// ReXianPriview
/// 预览报表
///
/// 修改记录
/// 版本:1.0 2007.01.09 [JiRiGaLa] 创建。
///
/// 版本:1.0
///
/// <author>
/// <name>JiRiGaLa</name>
/// <date>2007.01.09</date>
/// </author>
/// </remarks>
public partial class ReXianPriview : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
this.DoPageLoad();
}
#region private void DoPageLoad() 页面初次加载时的动作
/// <summary>
/// 页面初次加载时的动作
/// </summary>
private void DoPageLoad()
{
ReportDocument reportDocument = new ReportDocument();
String reportPath = "~/Modules/Water/12KeFu/";
String reportFile = Server.MapPath(reportPath) + "ReXianDan.rpt";
reportDocument.Load(reportFile);
reportDocument.SetDataSource(this.DTPriview);
this.ReportViewer.ReportSource = reportDocument;
}
#endregion
public DataTable DTPriview
{
get
{
return this.GetFromSession("_DTReXianPriview") as DataTable;
}
set
{
this.AddSession("_DTReXianPriview", value);
}
}
}
}
8. 若报表预览时,打印按钮、导出按钮没有出现,很可能是服务器上的配套图标文件放错位置了,需要注意
\aspnet_client\system_web\2_0_50727\CrystalReportWebFormViewer4\?
这些文件需要放在 wwwroot 下,就是IIS的根目录下才可以,否则也无法直接打印输出到本地。
网上有人讲:水晶报表显示 工具条上的 导出、打印、刷新按钮 都是显示为 一个个的 小红叉 似的。点击以后打开一个当前页的新窗口,却没有生成为PDF或者打印。 但是在程序 调试和 在浏览器中查看 的状态下都是正常的。都折腾好几天了,还是没解决问题。这个问题,就是上面的事情导致的,按我讲的折腾一下,就应该会好的。
技术再厉害,遇到问题都需要摸索,这次是第二次折腾这个,以前折腾过的经验很多都派上了用处,还比较顺利一些,
但是还是用了4-5个小时才把问题都解决好,所以我把经验写下来,分享给将来需要的人查阅,尽量少浪费宝贵的生命,时间就是金钱,我们不是弄不出来,或者做不出来,而是看能否在最短的时间里见效,只要给我时间,我什么管理系统都能做出来,但是等我做出来,我的客户都倒闭了,还要这个东西干啥用啊,黄瓜菜都凉了。
以上文章,希望能对有所需要的人起一点儿帮助作用,或者遇到麻烦了,可以参考一下,还有一些人不知道如何
将报表进行本地打印的,报表可以在本地导出各种格式的文件,有大部分人不知道报表可以本地打印的。
将权限管理、工作流管理做到我能力的极致,一个人只能做好那么很少的几件事情。