使用HTML,CSS快速导出数据到Excel

 

在head中加入css定义


<style type="text/css">
.spercent
{
background-color:#ffff99;
mso-number-format:0.00%;
}
</style>
在css中加入:mso-number-format定义数据格式,格式可以在excel中查看自定义格式,具体可以参考一下:
mso-number-format:"0" NO Decimals
mso-number-format:"0/.000" 3 Decimals
mso-number-format:"/#/,/#/#0/.000" Comma with 3 dec
mso-number-format:"mm//dd//yy" Date7
mso-number-format:"mmmm/ d/,/ yyyy" Date9
mso-number-format:"m//d//yy/ h/:mm/ AM//PM" D -T AMPM
mso-number-format:"Short Date" 01/03/1998
mso-number-format:"Medium Date" 01-mar-98
mso-number-format:"d/-mmm/-yyyy" 01-mar-1998
mso-number-format:"Short Time" 5:16
mso-number-format:"Medium Time" 5:16 am
mso-number-format:"Long Time" 5:16:21:00
mso-number-format:"Percent" Percent - two decimals
mso-number-format:"0%" Percent - no decimals
mso-number-format:"0/.E+00" Scientific Notation
mso-number-format:"/@" Text
mso-number-format:"/#/ ???//???" Fractions - up to 3 digits (312/943)

导出的excel可以直接通过excel打开,效果如下:


完整代码:


ALL Code
class Program
    {
        protected const string HEADER = "<html xmlns:x=/"urn:schemas-microsoft-com:office:excel/">" +
                                          "<meta http-equiv=Content-Type content=/"text/html; charset=/"gb2312/">" +
                                          "<head>" +
                                          "<!--[if gte mso 9]><xml>" +
                                           "<x:ExcelWorkbook>" +
                                               "<x:ExcelWorksheets>" +
                                                   "<x:ExcelWorksheet>" +
                                                       "<x:Name>工作表标题</x:Name>" +
                                                       "<x:WorksheetOptions>" +
                                                           "<x:Print>" +
      &n bsp;                                                        "<x:ValidPrinterInfo />" +
                                                           "</x:Print>" +
                                                       "</x:WorksheetOptions>" +
                                                   "</x:ExcelWorksheet>" +
                                               "</x:ExcelWorksheets>" +
                                           "</x:ExcelWorkbook>" +
                                       "</xml>" +
                                       "<![endif]-->" ;

        const string STYLE="<style type=/"text/css/">" +
                                       ".spercent" +
                                       " {" +
                                       "   background-color:#ffff99;" +
                                       "   mso-number-format:0.00%;" +
                                       " }" +
                                        ".sId" http://msnpiki.msnfanatic.com/index.php/Main_Page-->
0;">+
                                       " {" +
                                       "   background-color:#ff6633;" +
                                       "   mso-number-format:0;" +
                                       " }" +
                                        ".sName" +
                                       " {" +
                                       "     color:red;" +
                                       " }" +
                                        ".sValue" +
                                       " {" +
                                       "   color:blue;" +
                                       "   mso-number-format:0;" +
                                       " }" +
                                       "</style>";


        static void Main(string[] args)
        {
            using (StreamWriter writer = new StreamWriter(@"C:/1.xls", true, System.Text.Encoding.GetEncoding("gb2312"
style="color: #000000;">), 512))
            {
                writer.WriteLine(HEADER);
                writer.WriteLine(STYLE);
                writer.WriteLine("</head><body><table border=/"1/" style=/"font-size:9pt/"><tr>");
                writer.WriteLine ("<th>ID</th>");
                writer.WriteLine ("<th>Name</th>");
                writer.WriteLine ("<th>Value</th>");
                writer.WriteLine ("<th>Percent</th>");

                for (int row = 1; row < 50; row++)
                {
                    writer.WriteLine("<tr>");
                    writer.WriteLine("<td class=/"sId/">{0}</td", row);
                    writer.WriteLine("<td class=/"sName/">{0}</td", Guid.NewGuid ().ToString ());
                    writer.WriteLine("<td class=/"sValue/">{0}</td", new Random().Next());
                    writer.WriteLine("<td class=/"spercent/">{0}</td", new Random().NextDouble());
                    writer.WriteLine("</tr>");
                }

                writer.WriteLine("</table></body>");
            }
          
        }
    }

http://www.diybl.com/course/4_webprogram/asp.net/netjs/20091019/179435.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值