Click here to Skip to main content
15,500,921 members
Articles / Programming Languages / C#
Tip/Trick
Posted 21 Mar 2015

Stats

53.7K views
2.3K downloads
17 bookmarked

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

Rate me:
Please Sign up or sign in to vote.
4.69/5 (8 votes)
21 Mar 2015CPOL
In this tip, I want show you how to export RDLC Local report directly to Word, Excel or PDF from code.

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:

C#
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:

C#
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:

Image 1

C#
private void export_btn_Click(object sender, EventArgs e)
{
    excel_btn.Visible = word_btn.Visible = pdf_btn.Visible = !excel_btn.Visible;
}
C#
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:

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

Execution:

Image 2Image 3

Export to PDF - Word - Excel:

Image 4 Image 5 Image 6

END !

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
SMPS
Morocco Morocco
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionCould not reproduce your results Pin
djmarcus29-Mar-15 12:39
Memberdjmarcus29-Mar-15 12:39 
QuestionNot able to view the Images Pin
meeram3924-Mar-15 16:23
professionalmeeram3924-Mar-15 16:23 
QuestionExcellent Article + Question for you Pin
DumpsterJuice23-Mar-15 7:41
MemberDumpsterJuice23-Mar-15 7:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.