今天在做项目时,通过流导出数据到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();}

最新文章

  1. bzoj3388(神奇的解法)
  2. Node.js 的module 系统
  3. z-index详解
  4. Javascript的调试利器:Firebug使用详解
  5. hdu2053
  6. [原]POJ-1631-Bridging signals-( 水LIS-O(nlogn) -DP)
  7. google对js延迟加载方案的建议
  8. android startActivityForResult(Intent intent, int requestCode) 整理与总结! .
  9. 发现一个Doxygen风格的QT帮助
  10. 关于SD卡
  11. PE文件结构整理
  12. HttpCookie
  13. 三家DirectUI的商业公司
  14. linux下文件和目录的颜色表示
  15. Hadoop源码学习之HDFS(一)
  16. DRUID连接池配置详情
  17. 5.4Python数据处理篇之Sympy系列(四)---微积分
  18. ready
  19. 二、Python-运算符、控制及循环语句
  20. 用&lt;pre&gt;预格式化的文本

热门文章

  1. centos crontab(定时任务) 使用
  2. HDU2282 Chocolate KM算法
  3. windows mysql 操作实践
  4. (10.11)Java第一小步
  5. 【转载】基于VoiceOver的移动web站无障碍访问实战
  6. 在 Arch 下编译 OpenWRT cmcurl 问题与解决方案
  7. UVa10791 - Minimum Sum LCM
  8. zookeeper 笔记-ACL
  9. 423. Reconstruct Original Digits from English (leetcode)
  10. Windows搭建wnmp