C#导出EXCEL没有网格线的解决方法介绍

做项目时,通过流导出数据到Excel却不显示网格线,真是郁闷。上网查了好久才得一良方(注意<XML>标签中的代码):

DataTablethisTable=DBHelper.GetDataTable("select*fromtable");
stringsheetName="sheetName";
stringfileName="fileName";

if(thisTable!=null)
{
StringWritersw=newStringWriter();
sw.WriteLine("<htmlxmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
sw.WriteLine("<head>");
sw.WriteLine("<!--[ifgtemso9]>");
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(DataRowdrinthisTable.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();
}

<!--EndFragment-->
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值