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

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

发布于 2012/11/30 00:00

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)

关于葡萄城

葡萄城是专业的软件开发技术和低代码平台提供商,以“赋能开发者”为使命,致力于通过表格控件、低代码和BI等各类软件开发工具和服务,一站式满足开发者需求,帮助企业提升开发效率并创新开发模式。葡萄城开发技术始于1980年,40余年来始终聚焦软件开发技术,有深厚的技术积累和丰富的产品线。是业界能够同时赋能软件开发和低代码开发的企业。凭借过硬的产品能力、活跃的用户社区和丰富的伙伴生态,与超过3000家合作伙伴紧密合作,产品广泛应用于信息和软件服务、制造、交通运输、建筑、金融、能源、教育、公共管理等支柱产业。

推荐相关案例
推荐相关资源
关注微信
葡萄城社区二维码

关注“葡萄城社区”

加微信获取技术资讯

加微信获取技术资讯

想了解更多信息,请联系我们, 随时掌握技术资源和产品动态