← 返回所有博客文章

ActiveReports可以将报表导出为PDF,Excel,RTF/Word,HTML,以及图片格式,本文主要讲解如何在Web应用系统中将报表导出为以上几种格式的文件。

第一步,新建一个ASP.NET项目,然后在项目中鼠标右键选择【添加项】,在弹出的对话框中选择 ActiveReports 7 Page Report 项目模板,并给工程添加以下引用:

GrapeCity.ActiveReports.Export.Pdf.v7.dll

GrapeCity.ActiveReports.Export.Excel.v7.dll

GrapeCity.ActiveReports.Export.Word.v7.dll

GrapeCity.ActiveReports.Export.Html.v7.dll

第二步,添加完报表文件之后,切换到报表的设计界面,此时,在 Visual Studio 的菜单中会显示【Report】菜单项,选择【Convert to CPL Report】菜单项

image

然后,在报表设计界面添加你需要的控件,并设置相关属性。

通过以上操作我们就完成了报表的设计工作,接下来我们打开ASPX页面的设计界面,从 Visual Studio 工具箱中将WebViewer控件添加到页面中,调整WebViewer控件的大小,并设置WebViewer控件的ReportName属性为PageReport1.rdlx

在ASPX页面中添加4个Button控件,分别用于PDF,Excel,RTF/Word,HTML格式文件的到处操作,分别对应的后台代码如下:

PDF导出代码:

    protected void Button1_Click(object sender, EventArgs e)
    {
        GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("") + "\\PageReport1.rdlx"));
        GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);

        GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension _renderingExtension = new GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension();
        GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider _provider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();
        _reportRuntime.Render(_renderingExtension, _provider);

        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=MyExport.pdf");
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        _provider.GetPrimaryStream().OpenStream().CopyTo(ms);
        Response.BinaryWrite(ms.ToArray());
        Response.End();
    }

 

Html导出代码:

    protected void Button2_Click(object sender, EventArgs e)
    {
        GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("") + "\\PageReport1.rdlx"));
        GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);

        GrapeCity.ActiveReports.Export.Html.Page.HtmlRenderingExtension _renderingExtension = new GrapeCity.ActiveReports.Export.Html.Page.HtmlRenderingExtension(); GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider _provider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider(); GrapeCity.ActiveReports.Export.Html.Page.Settings s = new GrapeCity.ActiveReports.Export.Html.Page.Settings();
        s.Mode = GrapeCity.ActiveReports.Export.Html.Page.RenderMode.Paginated;
            
        _reportRuntime.Render(_renderingExtension, _provider, s);

        Response.ContentType = "message/rfc822";
        Response.AddHeader("content-disposition", "attachment;filename=MyExport.html");
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        _provider.GetPrimaryStream().OpenStream().CopyTo(ms);
        Response.BinaryWrite(ms.ToArray());
        Response.End();
    }

 

Excel导出代码:

    protected void Button3_Click(object sender, EventArgs e)
    {
        GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("") + "\\PageReport1.rdlx"));
        GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);

        GrapeCity.ActiveReports.Export.Excel.Page.ExcelTransformationDevice _renderingExtension = new GrapeCity.ActiveReports.Export.Excel.Page.ExcelTransformationDevice();
        GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider _provider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();
        GrapeCity.ActiveReports.Export.Excel.Page.Settings s = new GrapeCity.ActiveReports.Export.Excel.Page.Settings();            

        _reportRuntime.Render(_renderingExtension, _provider, s);

        Response.ContentType = "application/vnd.ms-excel";
        Response.AddHeader("content-disposition", "attachment;filename=MyExport.xls");
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        _provider.GetPrimaryStream().OpenStream().CopyTo(ms);
        Response.BinaryWrite(ms.ToArray());
        Response.End(); 
    }

 

Word导出代码:

    protected void Button4_Click(object sender, EventArgs e)
    {
        GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("") + "\\PageReport1.rdlx"));
        GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);

        GrapeCity.ActiveReports.Export.Word.Page.WordRenderingExtension _renderingExtension = new GrapeCity.ActiveReports.Export.Word.Page.WordRenderingExtension();
        GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider _provider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();
        GrapeCity.ActiveReports.Export.Word.Page.Settings s = new GrapeCity.ActiveReports.Export.Word.Page.Settings();
        s.UseMhtOutput = true;

        _reportRuntime.Render(_renderingExtension, _provider, s);

        Response.ContentType = "application/msword";
        Response.AddHeader("content-disposition", "attachment;filename=MyExport.doc");
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        _provider.GetPrimaryStream().OpenStream().CopyTo(ms);
        Response.BinaryWrite(ms.ToArray());
        Response.End(); 
    }

 

运行截图:

image

 

源码下载:VS2010 + ActiveReports 7.0

ExportReport_Web.zip (1.89 mb)