您当前的位置: 首页 » asp.net编程学习
» 使用HTML,CSS快速导出数据到Excel
使用HTML,CSS快速导出数据到Excel
根据原文做了修改
在应用中经常会遇到要从系统或数据库中导出数据平面文件,一般是导出到txt,csv或excel。txt和csv一般用在系统间的数据交换, 而excel一般有较好的显示效果,可以按照一定的模板导出,导出就不用再排版了,使用简单,如果是使用做报表一般都导出excel文件。
但是使用com组件导出到Excel数据很慢,有另一种生成excel文件的方式就是通过html和css快速导出数据同时并能设置样式,使用这种方式有两个优点:1是速度快,2是不需安装excel支持。
实现就是通过html可以直接转换成excel,像ASP.net中直接通过Gridview导出excel都没有显示出表格。 显示出表格代码如下:
<xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name>工作表标题</x:Name>
<x:WorksheetOptions>
<x:Print>
<x:ValidPrinterInfo />
</x:Print>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
</x:ExcelWorkbook>
</xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name>工作表标题</x:Name>
<x:WorksheetOptions>
<x:Print>
<x:ValidPrinterInfo />
</x:Print>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
</x:ExcelWorkbook>
</xml>
进一步封装后代码如下:
public static void CreateExcel(string strTable, string fileName)
{
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>Sheet1</x:Name>" + "<x:WorksheetOptions>" + "<x:Print>" + "<x:ValidPrinterInfo />" + "</x:Print>" + "</x:WorksheetOptions>" + "</x:ExcelWorksheet>" + "</x:ExcelWorksheets>" + "</x:ExcelWorkbook>" + "</xml>" + "<![endif]-->";
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
System.Web.HttpContext.Current.Response.ContentType = "ms-excel/application";
StringBuilder sbHtml = new StringBuilder();
sbHtml.AppendFormat(@"{0}</head>\{1}</body></html>", HEADER, strTable);
System.Web.HttpContext.Current.Response.Write(sbHtml.ToString());
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.End();
}
{
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>Sheet1</x:Name>" + "<x:WorksheetOptions>" + "<x:Print>" + "<x:ValidPrinterInfo />" + "</x:Print>" + "</x:WorksheetOptions>" + "</x:ExcelWorksheet>" + "</x:ExcelWorksheets>" + "</x:ExcelWorkbook>" + "</xml>" + "<![endif]-->";
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
System.Web.HttpContext.Current.Response.ContentType = "ms-excel/application";
StringBuilder sbHtml = new StringBuilder();
sbHtml.AppendFormat(@"{0}</head>\{1}</body></html>", HEADER, strTable);
System.Web.HttpContext.Current.Response.Write(sbHtml.ToString());
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.End();
}
上一篇:没有文章了
名字:
全部评论:
loading...
申明:本站部分文章来自网络,由于各种原因对文章的来源无从考究,如果您是“
使用HTML,CSS快速导出数据到Excel
”的原作者,若侵犯您的版权,请与我联系!联系方法:email:ahuinan@21cn.com QQ:106494262
文章档案
- 作者:ruinet
- 来源:ruinet's blog
- 日期:2011/10/7 22:54:00
- 点击:loading...
网友投票(您觉得这篇文章怎样?)
请稍侯......
请稍侯......