zoukankan      html  css  js  c++  java
  • C#导出EXCEL没有网格线的解决方法

    今天在做项目时,通过流导出数据到Excel却不显示网格线,真是郁闷。上网查了好久才得一良方(注意<XML>标签中的代码):
    DataTable thisTable = DBHelper.GetDataTable("select * from table");
    string sheetName = "sheetName";
    string fileName = "fileName";

    if (thisTable != null)
    {
        StringWriter sw 
    = new StringWriter();
        sw.WriteLine(
    "<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
        sw.WriteLine(
    "<head>");
        sw.WriteLine(
    "<!--[if gte mso 9]>");
        sw.WriteLine(
    "<xml>");
        sw.WriteLine(
    " <x:ExcelWorkbook>");
        sw.WriteLine(
    "  <x:ExcelWorksheets>");
        sw.WriteLine(
    "   <x:ExcelWorksheet>");
        sw.WriteLine(
    "    <x:Name>" + sheetName + "</x:Name>");
        sw.WriteLine(
    "    <x:WorksheetOptions>");
        sw.WriteLine(
    "      <x:Print>");
        sw.WriteLine(
    "       <x:ValidPrinterInfo />");
        sw.WriteLine(
    "      </x:Print>");
        sw.WriteLine(
    "    </x:WorksheetOptions>");
        sw.WriteLine(
    "   </x:ExcelWorksheet>");
        sw.WriteLine(
    "  </x:ExcelWorksheets>");
        sw.WriteLine(
    "</x:ExcelWorkbook>");
        sw.WriteLine(
    "</xml>");
        sw.WriteLine(
    "<![endif]-->");
        sw.WriteLine(
    "</head>");
        sw.WriteLine(
    "<body>");
        sw.WriteLine(
    "<table>");
        sw.WriteLine(
    " <tr>");
        sw.WriteLine(
    "  <td><strong>列名0</strong></td>");
        sw.WriteLine(
    "  <td>列名1</td>");
        sw.WriteLine(
    "  <td>列名2</td>");
        sw.WriteLine(
    "  <td>列名3</td>");
        sw.WriteLine(
    "  <td>列名4</td>");
        sw.WriteLine(
    "  <td>列名5</td>");
        sw.WriteLine(
    " </tr>");
        
    foreach (DataRow dr in thisTable.Rows)
        {
            sw.WriteLine(
    " <tr>");
            sw.WriteLine(
    "  <td>" + dr["C0"+ "</td>");
            sw.WriteLine(
    "  <td>" + dr["C1"+ "</td>");
            sw.WriteLine(
    "  <td>" + dr["C2"+ "</td>");
            sw.WriteLine(
    "  <td>" + dr["C3"+ "</td>");
            sw.WriteLine(
    "  <td>" + dr["C4"+ "</td>");
            sw.WriteLine(
    "  <td>" + dr["C5"+ "</td>");
            sw.WriteLine(
    " </tr>");
        }
        sw.WriteLine(
    "</table>");
        sw.WriteLine(
    "</body>");
        sw.WriteLine(
    "</html>");
        sw.Close();
        Response.Clear();
        Response.Buffer 
    = true;
        Response.Charset 
    = "UTF-8";
        
    this.EnableViewState = false;
        Response.AddHeader(
    "Content-Disposition""attachment; filename=" + fileName +".xls");
        Response.ContentType 
    = "application/ms-excel";
        Response.ContentEncoding 
    = System.Text.Encoding.GetEncoding("GB2312");
        Response.Write(sw);
        Response.End();
    }
  • 相关阅读:
    ASP.NET 缓存 @ OutputCache
    转:Windows下JDK1.6.0+Tomcat6.0的安装配置
    bit、byte、位、字节、汉字、字符之间的区别
    JDK1.6 tomcat 6.0环境变量配置
    DataList和Repeater里的自定义button控件的使用
    Button控件的onclick、oncommand、commandname、CommandArgument的区别
    GridView里的Button控件用法
    ASP.NET2.0 文本编辑器FCKeditor用法
    应用系统架构设计补全篇
    java.util.vector中的vector的详细用法
  • 原文地址:https://www.cnblogs.com/linyechengwei/p/1597835.html
Copyright © 2011-2022 走看看