ActiveReports Flash Viewer 提供了灵活的定制接口,我们可以向其中添加自定义按钮用于实现自定义功能。导出报表到客户端也是比较常见的使用场景,本篇文章将阐述如何实现以上功能。

下面分布阐述:

1.定制 FlashViewer 添加导出按钮,详细设置方法请参考,代码如下:

            //设置显示语言版本为中文
            this.WebViewer1.FlashViewerOptions.ResourceLocale = "zh_CN";
 
            //创建自定义工具条按钮
            ToolButton btnPDF = Tool.CreateButton("PDF");
            btnPDF.ToolTip = "导出到 PDF";
            btnPDF.Caption = "导出到 PDF";
 
            //设置点击按钮执行的服务
            btnPDF.ClickNavigateTo = "ARExport.ashx?exporttype=PDF";
 
            //添加按钮到 FlashViewer 中
            this.WebViewer1.FlashViewerToolBar.Tools.Add(btnPDF);
 
            ToolButton btnWord = Tool.CreateButton("Word");
            btnWord.ToolTip = "导出到 Word";
            btnWord.Caption = "导出到 Word";
            btnWord.ClickNavigateTo = "ARExport.ashx?exporttype=Word";
            this.WebViewer1.FlashViewerToolBar.Tools.Add(btnWord);
 
            ToolButton btnExcel = Tool.CreateButton("Excel");
            btnExcel.ToolTip = "导出到 Excel";
            btnExcel.Caption = "导出到 Excel";
            btnExcel.ClickNavigateTo = "ARExport.ashx?exporttype=Excel";
            this.WebViewer1.FlashViewerToolBar.Tools.Add(btnExcel);

 

2.通过 HttpContext.Request 判断导出类型:

            var key = context.Request["exporttype"];
            switch (key)
            {
                case "PDF":
                    ExportPdf(context, pr);
                    break;
                case "Word":
                    ExportWord(context, pr);
                    break;
                case "Excel":
                    ExportExcel(context, pr);
                    break;
            }

 

3.导出 Word 文档到客户端:

        private void ExportWord(HttpContext context, PageReport pr)
        {
            context.Response.ContentType = "application/msword";
            context.Response.Clear();
            context.Response.HeaderEncoding = System.Text.Encoding.Default;
            context.Response.AddHeader("content-disposition", "attachment;test.doc");
 
            var word = new RtfExport();
            var memStream = new System.IO.MemoryStream();
            word.Export(pr.Document, memStream);
            context.Response.BinaryWrite(memStream.ToArray());
            context.Response.End();
        }

 

4.导出 PDF 文档到客户端:

        private void ExportPdf(HttpContext context, PageReport pr)
        {
            context.Response.ContentType = "application/pdf";
            context.Response.Clear();
            context.Response.HeaderEncoding = System.Text.Encoding.Default;
            context.Response.AddHeader("content-disposition", "attachment;test.pdf");
 
 
            var pdf = new PdfExport();
            var memStream = new System.IO.MemoryStream();
            pdf.Export(pr.Document, memStream);
            context.Response.BinaryWrite(memStream.ToArray());
            context.Response.Flush();
            context.Response.End();
        }

 

5.导出 Excel 文件到客户端:

        private void ExportPdf(HttpContext context, PageReport pr)
        {
            context.Response.ContentType = "application/pdf";
            context.Response.Clear();
            context.Response.HeaderEncoding = System.Text.Encoding.Default;
            context.Response.AddHeader("content-disposition", "attachment;test.pdf");
 
 
            var pdf = new PdfExport();
            var memStream = new System.IO.MemoryStream();
            pdf.Export(pr.Document, memStream);
            context.Response.BinaryWrite(memStream.ToArray());
            context.Response.Flush();
            context.Response.End();
        }

 

最终效果图:

image

 

Demo 下载:

VS2010 + C# + AR9.0 + .NET4.0

 

更多ActiveReports报表功能请参考:/developer/activereports