2012-11-08 91 views
0

我有兩個網頁,page1.aspx和page2.aspx。在頁面1中,有一個按鈕;在第2頁中,有一個本地報告(rdlc)。當我點擊按鈕時,它會調出page2,並將報告導出爲pdf和excel文件。如果報表是Crystal Report,則可以在page2的page_load中調用ExportToDisk(ExportFormatType,FileName)函數將報表導出到pdf/excel。但是現在我正在使用本地報告(rdlc),我想知道如何將它導出到pdf/excel。如何將本地報告導出爲pdf和excel文件?

回答

0

http://weblogs.asp.net/rajbk/archive/2006/03/02/How-to-render-client-report-definition-files-_28002E00_rdlc_2900_-directly-to-the-Response-stream-without-preview.aspx

/// <summary> 
/// References: 
/// </summary> 
private void RenderReport() { 
    LocalReport localReport = new LocalReport(); 
    localReport.ReportPath = Server.MapPath("~/Report.rdlc"); 

    //A method that returns a collection for our report 
    //Note: A report can have multiple data sources 
    List<Employee> employeeCollection = GetData(); 

    //Give the collection a name (EmployeeCollection) so that we can reference it in our report designer 
    ReportDataSource reportDataSource = new ReportDataSource("EmployeeCollection", employeeCollection); 
    localReport.DataSources.Add(reportDataSource); 

    string reportType = "PDF"; 
    string mimeType; 
    string encoding; 
    string fileNameExtension; 

    //The DeviceInfo settings should be changed based on the reportType 
    //http://msdn2.microsoft.com/en-us/library/ms155397.aspx 
    string deviceInfo = 
    "<DeviceInfo>" + 
    " <OutputFormat>PDF</OutputFormat>" + 
    " <PageWidth>8.5in</PageWidth>" + 
    " <PageHeight>11in</PageHeight>" + 
    " <MarginTop>0.5in</MarginTop>" + 
    " <MarginLeft>1in</MarginLeft>" + 
    " <MarginRight>1in</MarginRight>" + 
    " <MarginBottom>0.5in</MarginBottom>" + 
    "</DeviceInfo>"; 

    Warning[] warnings; 
    string[] streams; 
    byte[] renderedBytes; 

    //Render the report 
    renderedBytes = localReport.Render(
     reportType, 
     deviceInfo, 
     out mimeType, 
     out encoding, 
     out fileNameExtension, 
     out streams, 
     out warnings); 

    //Clear the response stream and write the bytes to the outputstream 
    //Set content-disposition to "attachment" so that user is prompted to take an action 
    //on the file (open or save) 
    Response.Clear(); 
    Response.ContentType = mimeType; 
    Response.AddHeader("content-disposition", "attachment; filename=foo." + fileNameExtension); 
    Response.BinaryWrite(renderedBytes); 
    Response.End(); 

} 
+0

http://weblogs.asp.net/rajbk/How-to-render-client-report-definition-files-_28002E00_rdlc_2900_-directly-to-the-Response-stream-without -preview *** not found *** –

+0

對不起,我試圖記住這是什麼契約工作,即使在...我沒有內存問題,但它將取決於數據。 –

相關問題