• Asp.net(c#)导出有表格线的Excel


    表格用文件流的方式输出为excel。实例代码如下:


            public static void DaochuTalbe(string TableInnerHtml, string filename)
            {
                StringWriter sw 
    = new
     StringWriter();
                sw.WriteLine(TableInnerHtml);
                sw.Close();
                System.Web.HttpContext.Current.Response.AddHeader(
    "Content-Disposition""attachment;filename=" + filename + ".xls"
    );
                System.Web.HttpContext.Current.Response.ContentType 
    = "application/ms-excel"
    ;
                System.Web.HttpContext.Current.Response.ContentEncoding 
    =
     System.Text.Encoding.UTF7;
                System.Web.HttpContext.Current.Response.Write(sw);
                System.Web.HttpContext.Current.Response.End();
            }
    这种方法的从本质上说并非标准的excel格式,不过把html格式的文件另存为excel的格式,然后用excel打开罢了。
    打开后会发现导出的excel无表格线,白白的一片,太难看了。
    通过分析excel的格式代码,我尝试在程序里增加带表格线的excel头部代码,结果顺利导出有表格线的excel.
    增加的代码如下:

          /// <summary>
            
    /// Excel头部
            
    /// </summary>

            
    /// <returns></returns>
            public static string AddExcelHead()
            {
                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(
    "<x:Name></x:Name>"
    );
                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();

            }

            /// <summary>
            
    /// Excel尾部
            
    /// </summary>

            
    /// <returns></returns>
            public static string AddExcelbottom()
            {
                StringBuilder sb 
    = new
     StringBuilder();
                sb.Append(
    "</body>"
    );
                sb.Append(
    "</html>"
    );
                
    return
     sb.ToString();
            }
  • 相关阅读:
    CSS选择器
    CSS框模型
    AJAX
    HTML 表单
    二叉树
    词嵌入、word2vec
    双向、深层循环神经网络
    20201012----每天都在进步吗?
    20201012--环境搭建
    20201011--记录一下
  • 原文地址:https://www.cnblogs.com/ymyglhb/p/1460124.html
Copyright © 2020-2023  润新知