zoukankan      html  css  js  c++  java
  • 从GridView导出到Excel 加行

    private void toexcel(string title)
        {
            try
            {
                Encoding encContent = Encoding.GetEncoding("utf-8");
                int count = GridView1.Columns.Count;
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.Buffer = true;
                Response.Charset = encContent.WebName;
                Response.ContentEncoding = encContent;
                Response.ContentType = "application/vnd.ms-excel";
                Response.Write(string.Format("<meta http-equiv=Content-Type content=text/html;charset={0}>", encContent.WebName));
                string rename = HttpUtility.UrlEncode(title, Encoding.UTF8);
                Response.AppendHeader("content-disposition", string.Format("attachment;filename={0}.xls", rename));
                StringBuilder titleName = new StringBuilder();
                string colspanCount = string.Format("<table><tr><td colspan=\"{0}\"", count);
                titleName.Append(colspanCount);
                titleName.Append("style=\"font-family:宋体;font-size:12.0pt;font-weight:700;");
                titleName.Append("text-align:center;vertical-align:middle;\">");
                titleName.Append(string.Format("{0}</td></tr></table>", title));
                EnableViewState = false;
                StringWriter sw = new StringWriter();
                HtmlTextWriter htw = new HtmlTextWriter(sw);
                this.GridView1.RenderControl(htw);
                Response.Write(AddExcelHead(title));
                Response.Write(titleName.ToString());
                Response.Write(sw.ToString());
                Response.Write(AddExcelbottom());
                sw.Close();
                Response.End();
            }
            catch
            {

            }
        }
       
        protected void btn_toexcel_Click(object sender, EventArgs e)
        {
            string orderid = "";
            if (Request.Params["id"] != null)
            {
                orderid = Request.Params["id"].ToString();
            }
            toexcel("订单" + orderid + "详细");
        }
        public static string AddExcelHead(string title)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
            sb.Append(" <head>");
            sb.Append(" <!--[if gte mso 9]><xml>");
            sb.Append("<x:ExcelWorkbook>");
            sb.Append("<x:ExcelWorksheets>");
            sb.Append("<x:ExcelWorksheet>");
            sb.Append(string.Format("<x:Name>{0}</x:Name>", title));
            sb.Append("<x:WorksheetOptions>");
            sb.Append("<x:Print>");
            sb.Append("<x:ValidPrinterInfo />");
            sb.Append(" </x:Print>");
            sb.Append("</x:WorksheetOptions>");
            sb.Append("</x:ExcelWorksheet>");
            sb.Append("</x:ExcelWorksheets>");
            sb.Append("</x:ExcelWorkbook>");
            sb.Append("</xml>");
            sb.Append("<![endif]-->");
            sb.Append(" </head>");
            sb.Append("<body>");
            return sb.ToString();
        }
        public static string AddExcelbottom()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("</body>");
            sb.Append("</html>");
            return sb.ToString();
        }
        public override void VerifyRenderingInServerForm(Control control)
        {

        }

  • 相关阅读:
    leetCode 24. Swap Nodes in Pairs (双数交换节点) 解题思路和方法
    【jQuery 区别】.click()和$(document).on("click","指定的元素",function(){});的区别
    【前台 submit的重复提交 错误】submit的重复提交
    【spring 注解 错误】使用controller 作为后台给前台ajax交互数据出错
    【Filter 不登陆无法访问】web项目中写一个过滤器实现用户不登陆,直接给链接,无法进入页面的功能
    【Filter 页面重定向循环】写一个过滤器造成的页面重定向循环的问题
    【前台页面 BUG】回车按钮后,页面自动跳转
    【hibernate 执行方法未插入数据库】hibernate的save方法成功执行,但是未插入到数据库
    【maven 报错】maven项目执行maven install时报错Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)
    【hibernate criteria】hibernate中criteria的完整用法 转
  • 原文地址:https://www.cnblogs.com/94cool/p/1795942.html
Copyright © 2011-2022 走看看