2014-01-23 54 views
1

我已經安裝了鏈接到所有這些帖子的CR_for_VS_13_0_4.exe程序,但我不知道用什麼代碼來實際啓動「.rpt」文件。我正在使用:如何從VS2010啓動Crystal Reports查看器?

process.start("myfile.rpt") 

但它啓動報表設計器,而不是查看器。我如何確保它在運行我的應用程序的客戶端上啓動查看器?

謝謝。

回答

0

通過啓動myfile.rpt的進程,您告訴Windows使用與.rpt文件關聯的程序打開文件:在您的計算機上,這可能是CR設計者;在用戶的機器上,可能沒有與.rpt文件關聯的程序。我想你應該做什麼(儘管已經有一段時間了,因爲我已經使用CR與.NET),但是實例化一個CR查看器對象,它應該顯示的報告設置爲myfile.rpt

考慮一個related SO post在VS使用CR觀衆2010

UPDATE:

關於你的評論,你是一切寫代碼,而不是使用VS工具箱,我下載修修補補一點與CR for VS 2010成功POC爲您提供以下:

PoC Project in VS 2010 Solution Explorer

using System; 
using System.Windows.Forms; 
using CrystalDecisions.CrystalReports.Engine; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     // the CR viewer 
     private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1; 

     public Form1() 
     { 
      InitializeComponent(); 

      // 
      // crystalReportViewer1 
      // 
      this.crystalReportViewer1 = new CrystalDecisions.Windows.Forms.CrystalReportViewer(); 

      // FORNOW: just some "who cares" setup assignments for PoC - to be adjusted of course 
      this.crystalReportViewer1.ActiveViewIndex = -1; 
      this.crystalReportViewer1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; 
      this.crystalReportViewer1.Cursor = System.Windows.Forms.Cursors.Default; 
      this.crystalReportViewer1.Location = new System.Drawing.Point(0, 0); 
      this.crystalReportViewer1.Name = "crystalReportViewer1"; 
      this.crystalReportViewer1.Size = new System.Drawing.Size(150, 150); 
      this.crystalReportViewer1.TabIndex = 3; 

      // FORNOW: a DataSet1 as a dummy data source for PoC - really to be loaded with meaningful data of course 
      DataSet1 ds1 = new DataSet1(); 
      ds1.Tables["DataTable1"].Rows.Add(new object[] { 25 }); // whatever - just some data for PoC 

      // the CR document - myfile.rpt 
      ReportDocument reportDocument1 = new ReportDocument(); 
      reportDocument1.Load("myfile.rpt"); 
      // See the note regarding the next statement that follows the code. 
      reportDocument1.SetDataSource(ds1); // set to use our dummy data source for PoC 

      // the CR viewer set to use the CR document 
      this.crystalReportViewer1.ReportSource = reportDocument1; 

      // the CR viewer added to the form's controls 
      this.Controls.Add(this.crystalReportViewer1); 

      // etcetera 
     } 

     // etcetera 
    } 
} 

如果您的目標.NET 4.0中,你會遇到關於crdb_adoplus.dll一個FileNotFoundExceptionreportDocument1.SetDataSource(ds1);,除非你添加以下到您的app.config

<startup useLegacyV2RuntimeActivationPolicy="true"> 
    <supportedRuntime version="v4.0"/> 
</startup> 

更多信息請參見SAP Note 1525432 - Could not load file or assembly 'file:///crdb_adoplus.dll' or one of its dependencies

關於最後一點,雖然這可以沒有VS工具箱來完成,我會強烈建議使用它來投入了大量的設計時代碼在窗體的.designer.cs文件中的CR閱讀器和集中你的窗體的.cs文件中包含您需要編寫的更有意義的代碼來呈現報告。 TIMTOWDI,但。

快樂報道!

+1

我沒有使用工具箱將項目添加到我的項目中。我所做的一切都是基於代碼的。該鏈接只談使用工具箱。我如何從代碼中使用查看器? – user2721815

+0

這個想法是一樣的,工具箱只是一個拖放的方式來實例化一個查看器控件並將其添加到窗體的控件中。我會用一些代碼來更新我的答案,這些代碼顯示了我認爲你需要去的方向。 – J0e3gan

+0

@ user2721815,我按照承諾添加了PoC代碼示例的更新。 – J0e3gan