繁体   English   中英

在winform的ReportViewer的LocalReport中导出为CSV

[英]Export to CSV in LocalReport of ReportViewer of winform

我想在LocalReport中将ReportViewer中的数据导出为CSV格式。 我在MSDN论坛上看过这篇文章,说本地报告不支持CSV渲染。但这可以在服务器报告中完成,但我不能使用服务器报告。

有没有办法在LocalReport中向ReportViewer添加CSV导出扩展?

选项1 - 自定义导出到Excel工具栏按钮

处理报表查看器的ReportExport事件,如果选择的扩展名为excel,请将导出导出到csv。

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;
    }
}

选项2 - 向ReportViewer工具栏添加自定义按钮

将自定义按钮添加到报表查看器的工具栏中,并指定处理程序以单击它的事件并在此处导出到csv:

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    
}

注意:

无论您想将数据导出到csv,都可以使用已传递的数据源数据进行报告。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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