折騰 VS2008 + ReportViewer + CrystalReports + PrintControl.cab 實現(xiàn)水晶報表本地打印
曾經(jīng)在VS2005時,找了很多資料折騰了一個通宵把網(wǎng)頁本地打印折騰出來了,微軟的有些東西雖然好用吧,報表這個總是弄不好一樣,非常想念Microsoft Visual FoxPro 的報表,大概都是10年前的事情了,用那個VFP做報表,又好用又簡單,功能也很強大,特別是排版非常容易,不知道微軟都在搞啥網(wǎng)頁報表就是折騰總是會遇到很多問題,IE8以前的版本的網(wǎng)頁打印也很若,非常鬧心,想實現(xiàn)一個最簡單的網(wǎng)頁打印功能就好比上天一樣折騰人,也可能是我的水平有問題吧,不過我們很多同事也弄不好這個。
接下來直接談我的折騰過程:
1. VS2008 安裝好后,應(yīng)該是自帶一個 CrystalReports ,總感覺 CrystalReports 不是微軟的,好像是被SAP收購了一樣,具體我不是很清楚,而且各種版本特別多,有時候到底要引用哪個版本也是讓人頭疼一些。
2. 想實現(xiàn)本地打印,我總感覺需要下載一個微軟的 ReportViewer,而且是For VS2008的,也有For VS2005的,我也沒測試過這個是否是必要的組件,裝上就可以了,這個是在網(wǎng)頁里顯示報表用的。
3. PrintControl 比較折騰人,也后很多版本的,F(xiàn)or VS2008 需要在網(wǎng)絡(luò)上下載,寫明鏈接的地方非常少,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,再不行,先休息一下,再到網(wǎng)絡(luò)上找找其他資料吧。
<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 等的配置比較鬧心,因為不同的版本,這些號碼都不一樣,若對不上,就會崩潰了,
先通過這個網(wǎng)頁,可以先把客戶端打印的控件裝上。
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"/>
<!--結(jié)束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>
<!--結(jié)束magicAjax配置-->
<system.web>
<httpRuntime maxRequestLength="400000" useFullyQualifiedRedirectUrl="true" executionTimeout="45"/>
<!--
設(shè)置 compilation debug="true" 可將調(diào)試符號插入
已編譯的頁面中。但由于這會
影響性能,因此只在開發(fā)過程中將此值
設(shè)置為 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"/>
<!--結(jié)束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. 網(wǎng)頁部分的代碼參考如下:
<%@ 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
/// 預(yù)覽報表
///
/// 修改記錄
/// 版本:1.0 2007.01.09 [JiRiGaLa] 創(chuàng)建。
///
/// 版本: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. 若報表預(yù)覽時,打印按鈕、導(dǎo)出按鈕沒有出現(xiàn),很可能是服務(wù)器上的配套圖標文件放錯位置了,需要注意
\aspnet_client\system_web\2_0_50727\CrystalReportWebFormViewer4\?
這些文件需要放在 wwwroot 下,就是IIS的根目錄下才可以,否則也無法直接打印輸出到本地。
網(wǎng)上有人講:水晶報表顯示 工具條上的 導(dǎo)出、打印、刷新按鈕 都是顯示為 一個個的 小紅叉 似的。點擊以后打開一個當前頁的新窗口,卻沒有生成為PDF或者打印。 但是在程序 調(diào)試和 在瀏覽器中查看 的狀態(tài)下都是正常的。都折騰好幾天了,還是沒解決問題。這個問題,就是上面的事情導(dǎo)致的,按我講的折騰一下,就應(yīng)該會好的。
技術(shù)再厲害,遇到問題都需要摸索,這次是第二次折騰這個,以前折騰過的經(jīng)驗很多都派上了用處,還比較順利一些,
但是還是用了4-5個小時才把問題都解決好,所以我把經(jīng)驗寫下來,分享給將來需要的人查閱,盡量少浪費寶貴的生命,時間就是金錢,我們不是弄不出來,或者做不出來,而是看能否在最短的時間里見效,只要給我時間,我什么管理系統(tǒng)都能做出來,但是等我做出來,我的客戶都倒閉了,還要這個東西干啥用啊,黃瓜菜都涼了。
以上文章,希望能對有所需要的人起一點兒幫助作用,或者遇到麻煩了,可以參考一下,還有一些人不知道如何
將報表進行本地打印的,報表可以在本地導(dǎo)出各種格式的文件,有大部分人不知道報表可以本地打印的。
將權(quán)限管理、工作流管理做到我能力的極致,一個人只能做好那么很少的幾件事情。