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)