2013-07-30 59 views
2

我正在用數據集文件的TableAdapter方法從數據庫中獲取數據, 和Crysral報告對象來創建報告而不是CrystalReport文件本身。 (基本上我不希望使用物理CrystalReport文件)如何使用CrystalReport對象和TableAdapter將Crystal Report導出到Excel?

在這裏,我的代碼的樣本

DataTable dtRpt = new DataTable(); 
CrystalReportViewer crv = new CrystalReportViewer(); 
using (uspRptComplainReceiptTableAdapter _adpSales = new uspRptComplainReceiptTableAdapter()) 
{ 
    dtRpt = _adpSales.GetData(Convert.ToByte(bolObj.CompId), bolObj.ComplainId) as DataTable; 
    if (dtRpt != null && dtRpt.Rows.Count > 0) 
    { 
     crptComplainReceipt rpt1 = new crptComplainReceipt(); 
     rpt1.SetDataSource(dtRpt); 
     crv.ReportSource = rpt1; 
     crv.PrintReport(); 
    } 
    else 
     MessageBox.Show("Record not found.", "Report", MessageBoxButtons.OK, MessageBoxIcon.Information); 
} 

現在,我想我的導出使用此方法(CrystalReport對象)Excel文件報告。 請幫我這個問題...

回答

2

試試這個:

CrystalDecisions.CrystalReports.Engine.ReportClass rpt=new ReportClass(); 
rpt.ExportToDisk(ExportFormatType.Excel, "FilePath"); 

BTW不要忘記添加引用。

+0

對不起,但我不明白你的答案.. 什麼是c類?我在哪裏設置我的數據源? –

+0

對不起,cClass是一個錯誤的看到我的更新。 'rpt'不是物理晶體報告文件(就像你所說的那樣)。起初,你必須先製作一份報告的對象。那麼你會顯示你的數據等等。當你想導出的時候,你會調用'ExportToDisk()'方法,其中有兩個參數,第一個是你希望你的報告轉換成文件的格式,第二個是'filepath'只寫瞭如何將您的報告導出到Excel(因爲您的問題是關於這個問題的)。 –

+0

Yaaa ..它的工作.. 但是,它不需要聲明ReportClass的Onject。 只有下面的線就足夠了這個 'rpt.ExportToDisk(ExportFormatType.Excel,「FilePath」);' –

相關問題