码迷,mamicode.com
首页 > Web开发 > 详细

asp.netDataTable导出excel方法-我的总结

时间:2015-12-29 14:35:37      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

先来写一段代码,这段代码也是我在网上找的,但是他那个原先有点问题,我对他那个进行了修改,这个是暂时的,现在在上班,我把代码弄好,晚上再写总结~

 1  public int StreamExport(System.Data.DataTable dt, List<string> ColNames, string fileName)
 2         {
 3 
 4             if (string.IsNullOrEmpty(fileName)) return 0;
 5 
 6             StringBuilder content = new StringBuilder();
 7             StringBuilder strtitle = new StringBuilder();
 8             content.Append("<html xmlns:o=‘urn:schemas-microsoft-com:office:office‘ xmlns:x=‘urn:schemas-microsoft-com:office:excel‘ xmlns=‘http://www.w3.org/TR/REC-html40‘>");
 9             content.Append("<head><title></title><meta http-equiv=‘Content-Type‘ content=\"text/html; charset=gb2312\">");
10             //注意:[if gte mso 9]到[endif]之间的代码,用于显示Excel的网格线,若不想显示Excel的网格线,可以去掉此代码
11             content.Append("<!--[if gte mso 9]>");
12             content.Append("<xml>");
13             content.Append(" <x:ExcelWorkbook>");
14             content.Append("  <x:ExcelWorksheets>");
15             content.Append("   <x:ExcelWorksheet>");
16             content.Append("    <x:Name>Sheet1</x:Name>");
17             content.Append("    <x:WorksheetOptions>");
18             content.Append("      <x:Print>");
19             content.Append("       <x:ValidPrinterInfo />");
20             content.Append("      </x:Print>");
21             content.Append("    </x:WorksheetOptions>");
22             content.Append("   </x:ExcelWorksheet>");
23             content.Append("  </x:ExcelWorksheets>");
24             content.Append("</x:ExcelWorkbook>");
25             content.Append("</xml>");
26             content.Append("<![endif]-->");
27             content.Append("</head><body><table style=‘border-collapse:collapse;table-layout:fixed;‘><tr>");
28             for (int i = 0; i < ColNames.Count; i++)
29             {
30 
31                 content.Append("<td><b>" + ColNames[i] + "</b></td>");
32             }
33             content.Append("</tr>\n");
34 
35             for (int j = 0; j < dt.Rows.Count; j++)
36             {
37                 content.Append("<tr>");
38                 for (int k = 0; k < dt.Columns.Count; k++)
39                 {
40                     object obj = dt.Rows[j][k];
41                     Type type = obj.GetType();
42                     if (type.Name == "Int32" || type.Name == "Single" || type.Name == "Double" || type.Name == "Decimal")
43                     {
44                         double d = obj == DBNull.Value ? 0.0d : Convert.ToDouble(obj);
45                         if (type.Name == "Int32" || (d - Math.Truncate(d) == 0))
46                             content.AppendFormat("<td style=‘vnd.ms-excel.numberformat:#,##0‘>{0}</td>", obj);
47                         else
48                             content.AppendFormat("<td style=‘vnd.ms-excel.numberformat:#,##0.00‘>{0}</td>", obj);
49                     }
50                     else
51                         content.AppendFormat("<td style=‘vnd.ms-excel.numberformat:@‘>{0}</td>", obj);
52                 }
53                 content.Append("</tr>\n");
54             }
55             content.Append("</table></body></html>");
56             content.Replace("&nbsp;", "");
57             Response.Clear();
58             Response.Buffer = true;
59             Response.ContentType = "application/vnd.ms-excel";  //"application/ms-excel";
60             Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
61             Response.Charset = "gb2312";
62             fileName = System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);
63             Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
64             Response.Write(content.ToString());
65             Response.Output.Flush();
66             //pages.Response.End();  //注意,若使用此代码结束响应可能会出现“由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值。”的异常。
67             HttpContext.Current.ApplicationInstance.CompleteRequest(); //用此行代码代替上一行代码,则不会出现上面所说的异常。
68             return 1;
69         }

 

asp.netDataTable导出excel方法-我的总结

标签:

原文地址:http://www.cnblogs.com/junshijie/p/5085384.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!