Spread for ASP.NET 打印预览实现方法

发布时间:2012/11/30 00:11 发布者:jian

返回博客中心

Spread for ASP.NET产品中提供了前台的Print方法,用户只需点击命令栏上的 Print 按钮便可打印Spread文档,不过Spread for ASP.NET并没有提供打印预览功能,本文就讲解如何实现Spread for ASP.NET的打印预览功能。

打印预览的主要是重写Spread的默认打印方法,并借助jQuery中的Ajax和Spread提供的SavePdf方法来实现,通过Ajax请求后台将Spread以PDF的形式导出到客户端,从而实现打印预览功能。

前台代码:

<script type="text/javascript" language="javascript" src="Script/jquery-1.4.1.min.js"></script>
    <script language="javascript" type="text/javascript">
        function myprint() {
            var image = document.getElementById("Image1");
            image.style.visibility = "visible";
            image.style.height = "100%";
            image.style.width = "100%";
            document.getElementById("Image1").style.visibility = "visible";
            document.getElementById("Image1").style.visibility = "visible";
            $.ajax({
                url: 'WebForm1.aspx?command=pdfexport',
                success: function (data) {
                    image.style.visibility = "hidden";
                    image.style.height = "0px";
                    image.style.width = "0px";
                    mywindow = window.open("WebForm1.aspx?command=showpdf", "_blank", "width=1024px,resizable=yes,location=no");
                }
            });
        }
    </script>

 

后台代码:

protected override void Render(HtmlTextWriter writer)
    {
        WebControl print = (WebControl)FpSpread1.FindControl("Print");
        if (print != null)
        {
            // 修改默认的打印行为
            print.Attributes.Add("onclick", "return myprint();");
        }
        base.Render(writer);
    }
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FpSpread1.OpenExcel(Server.MapPath(@"Resource\金牌用户电话回访记录表.xlsx"));
            FpSpread1.ActiveSheetView.GridLineColor = System.Drawing.Color.Black;
            FpSpread1.ActiveSheetView.AllowPage = false;
        }

        if (Request["command"] != null)
        {
            if (Request["command"].ToString() == "pdfexport")
            {
                // 设置打印参数
                PrintInfo pi = new PrintInfo();
                pi.Margin = new PrintMargin();
                pi.Margin.Top = 50;
                pi.Margin.Left = 100;
                pi.ShowRowHeader = PrintHeader.Hide;
                pi.ShowColumnHeader = PrintHeader.Hide;
                pi.ShowBorder = true;                    
                FpSpread1.ActiveSheetView.PrintInfo = pi;

                // 将Spread文件保存到Sess中
                Session.Add("pdfdata", null);
                System.IO.MemoryStream m_stream = new System.IO.MemoryStream();                    
                FpSpread1.SavePdf(FpSpread1.ActiveSheetView, 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=金牌用户电话回访记录表.pdf");
                    Response.BinaryWrite(m_stream.ToArray());
                    Response.End();
                }
            }
        }
    }

运行截图:

 

源码下载:VS2010 + Spread .NET 6.0 + jQuery

7171_Print.zip (148.89 kb)


关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网