← 返回所有博客文章

ActiveReports 产品除了本身非常容易使用之外,他还提供了用户自定义的能力,比如我们今天要实现的在ASP.NET系统中“为区域报表导出为PDF添加进度显示功能”,我们将在报表导出过程中显示一个导出进度提示,下面是实际运行的截图:

 

要实现该功能,我们可以借助jQuery中的Ajax技术。我们将在按钮的单击事件中将报表导出为PDF文件,同时我们会将隐藏的进度提示图片变为可见状态,下面是按钮单击事件的处理代码:

<script language="javascript" type="text/javascript">
        function btnExport_onclick() {
            document.getElementById("Image1").style.visibility = "visible";
            document.getElementById("btnExport").style.visibility = "hidden";
            $.ajax({
                url: 'WebForm1.aspx?command=pdfexport',
                success: function (data) {
                    window.location = "WebForm1.aspx?command=showpdf";
                }
            });
        }
    </script>

在后台代码中我们将导出的 PDF 文档以文件流的显示存放到Session中,Window.Location通过一个带参数的URL地址来显示一个页面,下面是服务端的代码实现:

protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["command"] != null)
        {
            if (Request["command"].ToString() == "pdfexport")
            {
                Session.Add("pdfdata", null);
                System.IO.MemoryStream m_stream = new System.IO.MemoryStream();
                NewActiveReport1 rpt = new NewActiveReport1();
                rpt.Run();
                GrapeCity.ActiveReports.Export.Pdf.Section.PdfExport pdfExport1 = new GrapeCity.ActiveReports.Export.Pdf.Section.PdfExport();
                if (pdfExport1 == null)
                {
                    pdfExport1 = new GrapeCity.ActiveReports.Export.Pdf.Section.PdfExport();
                }
                pdfExport1.Export(rpt.Document, m_stream);
                m_stream.Position = 0;
                Session["pdfdata"] = m_stream;
            }
            if (Request["command"].ToString() == "showpdf")
            {
                if (Session["pdfdata"] != null)
                {
                    System.IO.MemoryStream m_stream = new System.IO.MemoryStream();
                    m_stream = (System.IO.MemoryStream)Session["pdfdata"];
                    Response.ContentType = "application/pdf";
                    Response.AddHeader("content-disposition", "inline;filename=MyExport.pdf");
                    Response.BinaryWrite(m_stream.ToArray());
                    Response.End();
                }
            }
        }
    }

源码下载:VS2010 + ActiveReports 7

PDFLoadIndicator_C#.zip (616.32 kb)