65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 Report Viewer 2010 将 .NET MSChart 导出到 Excel/PDF

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.67/5 (3投票s)

2011年10月3日

CPOL

3分钟阅读

viewsIcon

55317

downloadIcon

2074

本文将演示如何使用报表查看器控件作为媒介,将图像、Web 窗体导出为 PDF/Excel。这将帮助我们避免使用第三方控件来实现最终目标。

目录

  1. 概述
  2. 案例研究/问题陈述
  3. 解决方案陈述
  4. 必备组件
  5. 如何开始?
  6. 一些曲折(技巧与窍门)
  7. 结论

概述

在 Microsoft Office 工具中生成报表的需求一直是一个挑战和需求。Microsoft ASP.NET 工具包中没有明确的解决方案,可以将 Excel、PDF 或 Word 组件用作控件。为了满足我们的需求,我们可以选择使用 Microsoft 报表服务,如 Crystal Reports 或报表查看器控件,通过这些工具创建报表,并轻松获得所有内置的导出或打印功能。

案例研究/问题陈述

但是,如果我们已经花费了大量的成本和精力创建了不具备这些导出功能的报表怎么办?在我们这里就发生了这种情况,我们已经使用 GridView、Repeater 和普通的 HTML 控件在原始的 ASP.NET 网页中创建了报表。这些报表网页包含 Microsoft .NET 图表组件、图像和表格中的数据等。现在,客户要求我们提供一个功能,可以将这些 HTML 报表导出为 Excel、Word 和 PDF。我们尝试了网上所有可用的选项来将 .NET 图表控件渲染到 Excel,但都徒劳无功。最后,我们考虑使用 Report Viewer Control Redistributable 2010,但将所有网页/代码组件重新开发并迁移到 Report Viewer 需要投入精力。我们时间紧迫,因此考虑了一种使用 Report Viewer 的 `EnableExternalImage` 属性的方法。

解决方案陈述

我们可以实现什么?

  • 我们可以将 Microsoft .NET 图表保存为图像,并将其导出到 Report Viewer 的 RDLC 文件中。
  • 我们可以按原样将网页导出到 Report Viewer。
  • 某些 AXD 文件格式无法在 Word、Excel 和 PDF 中无缝导出。对于这种情况,我们可以利用 Report Viewer 的导出功能。

必备组件

如何开始?

  1. 创建 Web 解决方案。添加一个 aspx 网页。拖放 Report Viewer Web 控件。
  2. 添加属性 `EnableExternalImage=true`。

  3. 添加属性 `localpath=`。
  4. 添加新项,RDLC 文件。
  5. 从工具箱将 Image 控件拖放到 RDLC 文件中。
  6. 转到“视图”菜单。选择“报表数据”选项,参见图 2。

  7. 添加一个报表参数。参见图 2。

  8. 在解决方案中添加一个示例图像,并在其函数表达式中引用报表参数。同时选择外部图像。参见图。注意:这是我们创建了一个绑定,将 aspx 源图像(路径)桥接到 Report Viewer 的 Image 控件。使用报表参数,我们从 aspx 传递文件路径详细信息,图像将被嵌入到 Report Viewer 的 Image 控件容器中。

    为给定的 Image 控件选择图像源“外部”。

    在函数表达式中分配报表参数路径。

  9. 转到代码隐藏。现在是时候在 `Page_Load` 下编写几行代码来设置报表参数并调用 Report Viewer 渲染引擎了。编写以下代码:
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Microsoft.Reporting.WebForms.ReportParameter[] ReportParameters = 
    			new Microsoft.Reporting.WebForms.ReportParameter[1];
                ReportParameters[0] = 
    		new Microsoft.Reporting.WebForms.ReportParameter
    		("ReportParameter1", file:///+
    			Server.MapPath("MemoryStreamImage.JPG"));
                ReportViewer1.LocalReport.SetParameters(ReportParameters);
        }
    }
  10. ***需要注意的最重要一点是;确保在母版页中添加 `Script Manager`,因为 Report Viewer 控件默认在 `异步 Ajax 模式` 下工作。否则,您将收到以下运行时错误。

  11. 按 Ctrl+F5 查看输出。

***一些曲折(技巧与窍门)

  1. 如果您有 Microsoft Chart 组件,并希望将其导出为 Excel、PDF 和 Word,请使用下面的代码。将 **Microsoft .NET Chart** 对象保存在内存或某个服务器路径中。将此路径作为输入参数值传递给 **Report Viewer** 控件的报表参数。

    public void ExportChart(Chart chart)
    { 
        MemoryStream image = new MemoryStream();
        chart.SaveImage(image, ChartImageFormat.Png);
    
        Response.ContentType = "image/png";
        Response.AppendHeader("Content-Disposition",
                    String.Format
    		("attachment; filename={0}.png", Page.Title.Replace(" ", "_")));
        Response.OutputStream.Write(image.ToArray(), 0, (int)image.Length); }
  2. 上述实现方法会在 Report Viewer 中渲染内容,然后最终用户需要选择导出 PDF、Word 或 Excel 的选项。我们可以调整上述代码并包含以下代码片段,以直接打开 PDF/Word 或 Excel。请查看下面的示例代码以完成此操作。
      ReportViewer1.LocalReport.DataSources.Clear();
               ReportViewer1.LocalReport.ReportPath = "report1.rdlc";
    
               List<microsoft.reporting.webforms.reportparameter> _ParamList = 
    		new List<microsoft.reporting.webforms.reportparameter>();
               ReportViewer1.LocalReport.EnableExternalImages = true;
                objReportParameter.Add(new Microsoft.Reporting.WebForms.ReportParameter
    				("ImagePath", "/Images/Sample.JPG"));
    
               ReportViewer1.LocalReport.SetParameters(objReportParameter);
               ReportViewer1.DataBind();
                Microsoft.Reporting.WebForms.Warning[] warnings;
                string[] streamids;
                string mimeType;
                string encoding;
                string extension;
    
     string deviceInfo = "<deviceinfo>" +
                                        "  <outputformat>PDF</outputformat>" +
                                        "  <pagewidth>8.5in</pagewidth>" +
                                        "  <pageheight>11in</pageheight>" +
                                        "  <margintop>0.5in</margintop>" +
                                        "  <marginleft>0.25in</marginleft>" +
                                        "  <marginright>0.25in</marginright>" +
                                        "  <marginbottom>1in</marginbottom>" +
                                        "</deviceinfo>";
    byte[]writeBinaryBytes = new byte[0];
    writeBinaryBytes = ReportViewer1.LocalReport.Render
    	("PDF", deviceInfo, out mimeType, out encoding, out extension, 
    	out streamids, out warnings);
    
                HttpContext.Current.Response.Buffer = true;
                HttpContext.Current.Response.AddHeader
    		("content-disposition", "attachment; filename=" + "San.pdf");
                HttpContext.Current.Response.ContentType = "application/octet-stream";
                HttpContext.Current.Response.BinaryWrite(writeBinaryBytes);
                HttpContext.Current.Response.Flush(); 

结论

希望上述方法/解决方案能在您遇到类似问题陈述时为您提供一些指导。

© . All rights reserved.