2011-11-03 89 views
0

我試圖完成的事情: - 將Crystal Crystal報告直接轉到PDF(繞過查看器) - 它需要數據庫登錄。 - 需要一個參數。它在CR 11申請中顯示爲「@rpt_args」。 - 此Crystal報表爲其結果集調用Store過程。ASP.NET MVC運行水晶報告直接繞過PDF繞過水晶查看器

Partial solution

我的代碼:

 ReportClass rptH = new ReportClass();   
     //rptH.FileName = Server.MapPath("whkinvc.rpt");  
     rptH.FileName = "D:\\whkinvc.rpt";  
     rptH.Load();   
     rptH.SetDatabaseLogon("FAKEUSER", "FAKEPASSWORD", "FAKESERVER", "FAKEDB"); 
     rptH.SetParameterValue("@rpt_args", atm.WorkingModel.Header.TicketNumber);   
     Stream stream = rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);   
     return File(stream, "application/pdf"); 

目前,我得到一個數據庫登錄失敗。我是否正確設置了代碼以執行我正在查找的操作?數據庫登錄失敗的任何可能的原因?

感謝,

** **更新

我把它通過VS正與下面的代碼(數據庫登錄錯誤是一個錯字的結果):

 ReportClass rptH = new ReportClass(); 
     rptH.FileName = Server.MapPath(Url.Content("~/app_data/whkinvc.rpt")); 
     rptH.Load(); 

     rptH.SetDatabaseLogon(...); 

     ParameterValues xyz = new ParameterValues(); 
     ParameterDiscreteValue pdv = new ParameterDiscreteValue(); 
     pdv.Value = atm.WorkingModel.Header.TicketNumber; 

     xyz.Add(pdv); 
     rptH.DataDefinition.ParameterFields["@rpt_args"].ApplyCurrentValues(xyz); 
     Stream stream = rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);   
     return File(stream, "application/pdf"); 

我想要輸出到新的選項卡或窗口。目前它只是取代當前窗口。

+0

試圖做同樣的CR 10.5。以下在我的開發框中工作,但不在服務器上: 'ReportDocument doc = new ReportDocument(); – erict

+0

'ReportDocument doc = new ReportDocument(); doc.Load(Server.MapPath(「〜/ app_data/invoice.rpt」)); doc.SetDatabaseLogon(...); ParameterValues xyz = new ParameterValues(); xyz.Add(new ParameterDiscreteValue()。Value = id); doc.DataDefinition.ParameterFields [「Id」]。ApplyCurrentValues(xyz); doc.ExportToDisk(ExportFormatType.PortableDocFormat,Server.MapPath(「〜/ app_data/invoice.pdf」));' 我假設我已經安裝在開發中的CR,但不是在服務器上? – erict

+0

嗨,呃。我想你可能需要用「URL.Content」來包裝你的路徑才能找到正確的路徑。防爆。 Url.Content(「〜/ app_data/whkinvc.rpt」) – Elim99

回答

2

我得到它通過VS用下面的代碼工作(是一個錯字的結果的數據庫登錄錯誤):

ReportClass rptH = new ReportClass();   
rptH.FileName = Server.MapPath(Url.Content("~/app_data/whkinvc.rpt"));   
rptH.Load();   
rptH.SetDatabaseLogon(...);   
ParameterValues xyz = new ParameterValues();   
ParameterDiscreteValue pdv = new ParameterDiscreteValue();   
pdv.Value = atm.WorkingModel.Header.TicketNumber;   
xyz.Add(pdv);   
rptH.DataDefinition.ParameterFields["@rpt_args"].ApplyCurrentValues(xyz);   
Stream stream = rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);     
return File(stream, "application/pdf"); 
0

在過去,我必須爲報表中的每個表格(或存儲的proc或其他)以及每個子報表中的每個表格設置連接字符串。

例如:

(假設RD是的ReportDocument的一個實例,CN是ConnectionInfo的預先製備實例)

void SetupConnection(ReportDocument rd, ConnectionInfo cn) 
{ 

    foreach (Table t in rd.database.Tables) 
    { 
     TableLoginInfo li = t.LogOnInfo; 
     li.ConnectionInfo = cn; 
     t.ApplyLogOnInfo(li); 
     t.Location = t.Location; 
    } 
    if (!rd.IsSubReport) 
    { 
     foreach (ReportDocument sr in rd.SubReports) 
      SetupConnection(sr,cn); 
    } 
} 

我知道一些這方面沒有什麼意義,(如t.Location = t.Location)。這是我使用它之後的一段時間,但我相信這些是正常工作所必需的。