2016-06-10 232 views
0

我需要簡單的代碼點擊按鈕,將我目前的RDLC報告保存爲PDF格式,並會通過savedialog要求用戶將文件保存在所需的位置。我已經到處搜索,找不到任何解決方案。從按鈕點擊生成RDLC的PDF報告

我ShowReport按鈕的代碼是在這裏:

SqlConnection S_Conn = new SqlConnection(strConnString); 
     S_Conn.Open(); 
     string query_1 = ""; 
     query_1 = "SELECT Record_Id, Select_Ward, Mr_No, Patient_Name, Date_Of_Admission, Date_Of_Dsch_Death, Disease from EO_System_RecordRoomData WHERE Date_Of_Admission = '" + txtbx_DateForReport.Text.Trim() + "'"; 
     SqlCommand Command_1 = new SqlCommand(query_1, S_Conn); 
     SqlDataAdapter Data_Adapter = new SqlDataAdapter(Command_1); 
     DataSet1 Data_Set = new DataSet1(); 
     Data_Adapter.Fill(Data_Set); 
     reportViewer1.LocalReport.DataSources.Clear(); 
     reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", Data_Set.Tables[1])); 
     this.reportViewer1.RefreshReport(); 

幫助將得到高度讚賞。

+1

[在後臺從RDLC報告中創建PDF]的可能副本(http://stackoverflow.com/questions/2684221/creating-a-pdf-from-a-rdlc-report-in-the-the-background) –

+0

它不顯示文件將被保存的位置。我想要顯示saveasdialog爲了保存用戶想要的文件。 –

回答

0

試試這個:

SqlConnection S_Conn = new SqlConnection(strConnString); 
S_Conn.Open(); 
string query_1 = ""; 
query_1 = "SELECT Record_Id, Select_Ward, Mr_No, Patient_Name, Date_Of_Admission, Date_Of_Dsch_Death, Disease " 
     + "from EO_System_RecordRoomData WHERE Date_Of_Admission = '" + txtbx_DateForReport.Text.Trim() + "'"; 
SqlCommand Command_1 = new SqlCommand(query_1, S_Conn); 
SqlDataAdapter Data_Adapter = new SqlDataAdapter(Command_1); 
DataSet1 Data_Set = new DataSet1(); 
Data_Adapter.Fill(Data_Set); 
reportViewer1.LocalReport.DataSources.Clear(); 
reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", Data_Set.Tables[1])); 
reportViewer1.RefreshReport(); 

SaveFileDialog saveFileDialogPDF = new SaveFileDialog(); 
saveFileDialogPDF.Filter = "PDF|*.pdf"; 
saveFileDialogPDF.Title = "Save report to PDF"; 
saveFileDialogPDF.ShowDialog(); 

if (saveFileDialogPDF.FileName != "") 
{ 
    Warning[] warnings; 
    string[] streamids; 
    string mimeType; 
    string encoding; 
    string filenameExtension; 
    byte[] bytes = reportViewer.LocalReport.Render(
       "PDF", null, out mimeType, out encoding, out filenameExtension, 
       out streamids, out warnings); 
    using (FileStream fs = new FileStream(saveFileDialogPDF.FileName, FileMode.Create)) 
    { 
     fs.Write(bytes, 0, bytes.Length); 
    } 
} 

來源:

https://msdn.microsoft.com/en-us/library/sfezx97z(v=vs.110).aspx

Creating a PDF from a RDLC Report in the Background