【问题标题】:How do I export a DataGridView to ReportViewer?如何将 DataGridView 导出到 ReportViewer?
【发布时间】:2011-06-13 19:42:10
【问题描述】:

我正在开发一个 C# 项目,在该项目中我从数据库生成报告,然后在 DataGridView 中显示,因为动态添加行和列更简单。但我现在需要将 DataGridView 内容动态导出到 ReportViewer 中以进行打印。我不想显示 ReportViewer。我只想创建它的实例并从我的 DatagridView 填充,然后立即显示打印对话框,以便在输出中获得高质量和组织良好的布局。目前,我通过将报告转换为 HTML 来打印报告,然后将其分配给 WebBrowser 控件并调用 ShowPrintDialog() 方法。但是通过这种方式,我无法控制一些打印问题,例如在所有页面中打印列标题,因为 WebBrowser 按顺序输出表格。

我需要在最短的时间内达到这个目标,因为这是一个生产项目。

非常感谢任何想法。

【问题讨论】:

    标签: datagridview reportviewer


    【解决方案1】:

    您应该使用 Microsoft Report Viewer http://www.microsoft.com/en-us/download/details.aspx?id=3841 将 dll 添加到您的项目中:Microsoft.ReportViewer.Common 和 Microsoft.ReportViewer.WinForms,因此您必须创建一个 *.rdlc 文件,您可以更改报表的设计。最后,为了保存它,您可以执行以下操作:

    public static void PrintArticles(ICollectionView Articles)
            {
    
                try
                {
                    var articlesRows = new DataTable("Articles");
                    articlesRows.Columns.Add("Id");
                    articlesRows.Columns.Add("Description");
    
                    var arts = Articles.Cast<Article>();
    
                    foreach (var art in arts)
                    {
                        articlesRows.Rows.Add(art.Id, art.Description);
                    }
    
                    ReportViewer reporter = new ReportViewer();
                    reporter.LocalReport.DisplayName = "Report1";
                    reporter.LocalReport.ReportPath = Path.Combine(Program.BasePath + "PrintArticles.rdlc");
                    reporter.LocalReport.DataSources.Clear();
                    reporter.LocalReport.DataSources.Add(new ReportDataSource("Project1", articlesRows));
    
                    byte[] report = reporter.LocalReport.Render("PDF");
    
                    reporter.LocalReport.ReleaseSandboxAppDomain();
    
                    string pdfpath = Path.Combine(Program.BasePath, "file.pdf");
    
                    if (File.Exists(pdfpath))
                        File.Delete(Path.Combine(Program.BasePath, "file.pdf"));
    
                    FileStream writer = new FileStream(pdfpath, FileMode.Create);
                    writer.Write(report, 0, report.Length);
                    writer.Close();
    
                    Process ar = Process.Start(pdfpath);
                }
                catch (Exception e)
                {
    
                }
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-15
      • 1970-01-01
      相关资源
      最近更新 更多