按钮点击从RDLC生成PDF报告

时间:2016-06-10 05:50:43

标签: c# sql pdf rdlc reportviewer

我需要点击按钮上的简单代码,将我当前的rdlc报告保存为pdf格式,并通过savedialog询问用户将文件保存在所需位置。我已经到处搜索过,无法找到任何解决方案。

我的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();

帮助将受到高度赞赏。

2 个答案:

答案 0 :(得分:0)

您可以使用名为ItextSharp

的库

您可以使用以下链接中给出的方法创建pdf。您可以通过Nuget Package Manager下载itextsharp。

http://www.mikesdotnetting.com/article/86/itextsharp-introducing-tables

答案 1 :(得分: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