Crystal Report Viewer Web Control的安裝、設定與使用簡介

  • 4886
  • 0
  • 2016-07-26

 

 

 

 

下載網址:
http://scn.sap.com/docs/DOC-7824

安裝元件:
log4net.dll:位於C:\Windows\assembly之下,請注意該元件的strong name源自SAP,而非Apache,所以不可以由NuGet取得。
CrystalDecisions.Web:由NuGet取得。
CrystalDecisions.Shared:由NuGet取得。
CrystalDecisions.CrystalReports.Engine:由NuGet取得。

Web.Config的設定:
 在<system.web>之內加入下列區段,以利在各個Web Form內使用自訂標籤cr和Crystal Report Viewer Web Control:
 

    <pages>
      <controls>
        <add tagPrefix="cr" namespace="CrystalDecisions.Web" assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
      </controls>
    </pages>

在<configuration>區段之內的<configSections>之下加入下列區段以利定義自訂組態區段。
 

    <sectionGroup name="businessObjects">
      <sectionGroup name="crystalReports">
        <section name="rptBuildProvider" type="CrystalDecisions.Shared.RptBuildProviderHandler, CrystalDecisions.Shared, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304, Custom=null" />
        <section name="crystalReportViewer" type="System.Configuration.NameValueSectionHandler" />
      </sectionGroup>
    </sectionGroup>

在<configuration>區段內,在</configSections>之後加入下列區段:
 

  <businessObjects>
    <crystalReports>
      <rptBuildProvider>
        <add embedRptInResource="true" />
      </rptBuildProvider>
      <!--
      <crystalReportViewer>
        <add key="ResourceUri" value="~/aspnet_client/system_web/4_0_30319/crystalreportviewers13" />
      </crystalReportViewer>
      -->
    </crystalReports>
  </businessObjects>

確認網站根目錄下確實有下列資料夾:
/aspnet_client/system_web/4_0_30319/crystalreportviewers13

IIS設定:
若log4net.dll是64bit的版本,則將Application Pool的Enable 32bit Application設為false。

關鍵程式碼:
將DataSet中的資料套入範本Report1.rpt

private ReportDocument ConfigureReportDocument(DataSet ds) 
{ 
  ReportDocument doc = new ReportDocument(); 
  doc.Load(Server.MapPath(@".\App_Data\Report1.rpt")); 
  doc.SetDataSource(ds); return doc; 
}

 

將ReportDocument中存放的報表與資料以rpt檔案的方式供使用者下載
private void Print(ReportDocument doc) { 
  MemoryStream mem = new MemoryStream(); 
  doc.ExportToStream(ExportFormatType.CrystalReport).CopyTo(mem); 
  Response.Clear(); 
  Response.Buffer = true; 
  Response.ContentType = "application/octet-stream"; 
  Response.AddHeader("Content-Disposition", "attachment; filename=Report.rpt"); 
  Response.BinaryWrite(mem.ToArray()); 
}
 
將ReportDocument中的資料在Server端的印表機印出:
protected void Button1_Click(object sender, EventArgs e) 
{ 
  DataSet ds = (DataSet)Session["DataSet"]; 
  ReportDocument doc = new ReportDocument(); 
  doc.Load(Server.MapPath(@".\App_Data\Report1.rpt")); 
  doc.SetDataSource(ds); 
  try 
  { 
    doc.PrintToPrinter(1, true, 0, 0); 
  } 
  catch(System.Drawing.Printing.InvalidPrinterException ex) 
  { 
    Session["Error"] = ex.Message.ToString(); 
    Response.Redirect("Error.aspx"); 
  } 
}

 

補充資料:
[1]製作xsd與透過xsd製作Crystal Report範本的方式:
Create a Crystal Report based on an XML schema (XSD) file
http://www.thescarms.com/dotnet/XSDDatasource.aspx

[2]Crystal Reports 13 ( Visual Studio 2010 ) Issue with IIS - ( bobj is undefined JavaScript Error and Crystal Report Viewer not displaying )
http://www.aspsnippets.com/Articles/Crystal-Reports-13-Visual-Studio-2010-Issue-with-IIS---bobj-is-undefined-JavaScript-Error-and-Crystal-Report-Viewer-not-displaying.aspx