简体   繁体   中英

Export to CSV in LocalReport of ReportViewer of winform

I wanted to export data from ReportViewer to CSV format in LocalReport. I have gone through this article on MSDN Forum which says that the Local report does not support CSV rendering.but this can be done in server Report but I can not use Server Report.

Is there any way to add a CSV export extension to ReportViewer in LocalReport?

Option 1 - Customize Export To Excel Toolbar Button

Handle ReportExport event of report viewer and if selected extension is excel, do your export to csv instead.

private void reportViewer1_ReportExport(object sender, 
    Microsoft.Reporting.WinForms.ReportExportEventArgs e)
{
    if (e.Extension.Name == "EXCELOPENXML")
    {
        //Export your data to csv here    
        e.Cancel = true;
    }
}

Option 2 - Add Custom Button To ReportViewer Toolbar

Add your custom button to toolbar of report viewer and assign a handler to click event of it and do your export to csv here:

private void ReportForm_Load(object sender, EventArgs e)
{
    //Load your data from wherever your data is
    //For example using Entity Framework:
    var db = new TestDBEntities(); 
    var data = db.Categories.ToList();

    //Set data to report
    this.CategoryBindingSource.DataSource = data;

    this.reportViewer1.RefreshReport();

    //Add your button to Toolbar of ReportViewr
    ToolStrip toolStrip = (ToolStrip)reportViewer1.Controls.Find("toolStrip1", true)[0];
    toolStrip.Items.Add(new ToolStripButton("Export To CSV", 
    null /*image*/, ExportToCSV_Click));
}

void ExportToCSV_Click(object sender, EventArgs e)
{
    //Export your data to csv here    
}

Note:

Wherever you want to export data to csv , you can use data of datasource that you have passed to report.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM