RDLC - Export Directly to Word, Excel or PDF from Code - CodeProject

:

Introduction

Sometimes, we have a requirement to directly export data in WORD, EXCEL or PDF format. In this tip, we will create a sample report and export it directly to Word, Excel or PDF format without using report viewer toobar.

Using the Code

After you add a new DataSet and create a table inside this DataSet that contains all your columns name and datatype, then I create DataTable method that returns a new DataTable with all my data like this one:

private DataTable Data()
        {
            var dataTable = new DataTable("Data");

            dataTable.Columns.Add("Id", typeof(int));
            dataTable.Columns.Add("Name", typeof(string));
            dataTable.Columns.Add("Gender", typeof(string));
            dataTable.Columns.Add("Age", typeof(int));

            dataTable.Rows.Add(1000, "Ahmed", "Male", 22);
            dataTable.Rows.Add(1001, "Mohammed", "Male", 25);
            dataTable.Rows.Add(1002, "Hassan", "Male", 41);
            dataTable.Rows.Add(1003, "Abdullah", "Male", 19);
            dataTable.Rows.Add(1004, "Maryam", "Female", 21);

            return dataTable;
        }

After that, I create a void method that initials my report:

void InitialReport()
        {
            try
            {
                var ds = Data();

                // this is my Local DataSet :
                var myDataSet = new ReportingDataSet();
                
                // merge data with "Data()" method and MyDataSet Table :
                myDataSet.Tables["Data"].Merge(ds);

                ReportDataSource rdsR = new ReportDataSource("Data", myDataSet.Tables["Data"]);

                this.ReportViewer.LocalReport.DataSources.Clear();
                this.ReportViewer.LocalReport.DataSources.Add(rdsR);
                this.ReportViewer.LocalReport.Refresh();
                
                //set zoom mode to PageWidth :
                this.ReportViewer.ZoomMode = ZoomMode.PageWidth;
                this.ReportViewer.RefreshReport();
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message);
            }
        }

Now, I create a delegate method and void method to Export report to Word, Excel or PDF format, this method gets all RederingExtension from Report sheet, but I need just (Excel, Word, PDF rendering extension). Before that, I add tags value to the 3 buttons and the 3 buttons are Sets Visible to False by default and I use another button Named Export to show or hide the 3 buttons:

private void export_btn_Click(object sender, EventArgs e)
{
    excel_btn.Visible = word_btn.Visible = pdf_btn.Visible = !excel_btn.Visible;
}
private delegate void ExportTo(object sender);

private void ExportToFile(object sender)
{
    try
    {
        var x = ReportViewer.LocalReport.ListRenderingExtensions();
        RenderingExtension render_ = null;

        var obj = (sender as Button);

        switch (obj.Tag.ToString())
        {
            case "word":
                render_ = x[5];
                break;
            case "excel":
                render_ = x[1];
                break;
            case "pdf":
                render_ = x[3];
                break;
        }
        if (render_ != null)
        {
            var DialogResult = ReportViewer.ExportDialog(render_);
            if (DialogResult == DialogResult.OK)
                MessageBox.Show("Done!");
        }
    }
    catch (Exception x)
    {
        MessageBox.Show(x.Message);
    }
}

Then, I create an Event method for my 3 export buttons:

private void Exporting(object sender, EventArgs e)
{
    ReportViewer.Invoke(new ExportTo(ExportToFile), new object[] { sender });
}

Execution:

Export to PDF - Word - Excel:

END !