把数据导出到的Excel文件

 1         public static void ExportExcel()
 2         {
 3             StringBuilder sb = new StringBuilder();
 4             sb.Append("<table style='width:1000px;'>");
 5             sb.Append("<tr style='background: green'><td align='center' valign='middle' colspan='4'>这是表格标题</td></tr>");
 6             sb.Append("<tr style='border:1px solid yellow'><th style='width:200px'>t1</th><th style='width:500px'>t2</th><th colspan='2' style='width:300px'>t3t4</th></tr>");
 7             for (int i = 1; i <= 9; i++)
 8             {
 9                 sb.Append("<tr>");
10                 sb.Append("<td style='border:1px solid #abd;width:200px;'>" + i * 1 + "</td>");
11                 sb.Append("<td style='border:1px solid #abd;width:500px;'>" + i * 2 + "</td>");
12                 sb.Append("<td colspan='2' style='border:1px solid #abd;width:300px;'>" + i * 4 + "</td>");
13                 sb.Append("</tr>");
14             }
15             sb.Append("</table>");
16             sb.Append("<table style='width:1000px;'>");
17             sb.Append("<tr style='background: green'><td align='center' valign='middle' colspan='4'>这是表格标题</td></tr>");
18             sb.Append("<tr style='border:1px solid #F3F3F4'><th>t1</th><th>t2</th><th colspan='2'>t3t4</th></tr>");
19             for (int i = 1; i <= 9; i++)
20             {
21                 sb.Append("<tr>");
22                 for (int j = 1; j <= 4; j++)
23                 {
24                     sb.Append("<td style='border:1px solid #abd;'>" + i * j + "</td>");
25                 }
26                 sb.Append("</tr>");
27             }
28             sb.Append("</table>");
29             File.WriteAllBytes("abc.xls", Encoding.Default.GetBytes(sb.ToString()));
30             Process.Start("abc.xls");
31         }

 

posted @ 2012-08-05 11:40  自然去留  阅读(181)  评论(0)    收藏  举报