经常看到有些人问怎么把table的资料转到excel里面去,很多人在碰到这个问题都先考虑用execl.appliction,我用过感觉很不好,首先必需要掌握它的一些内部用法,其次它在客户端使用时涉及安全性,使用不太好,以下给大家介绍三众方法:
方法一:用单纯的文本格式实现
该方法相当简单,以下给出代码,各位就容易明白
<% set fso=server.createobject("scripting.filesystemobject")
strexcelfile=server.mappath("txttoexcel.xls")
if fso.fileexists(strexcelfile) then fso.deletefile strexcelfile
set xslfile = fso.createtextfile(strexcelfile , true)
xslfile.writeline("df" & vbtab & "345" & vblf & "fe" & vbtab & "mon" &vblf)
xslfile.close
set fso=nothing
response.write "ok"
%>
原理:
相信大家都知道,excel是可以打开文本文件的(什么?你不知道,先回去做两个月的文员再来编程,太不懂打破规则了),而对于里面的文本内容excel则会这样处理:如果遇到[制表符]tab键则跳下一列,如果遇到[换行符]则换下一行,所以根据这个规则我们整理一下就可以产生单一表格式excel文件了,
优点:
掌握简单,纯粹的文字整理而已(哈!原来不就是文字游戏而已,哼!提到游戏就精神了吧)
缺点:
效率较低,把一个较多内容的table转成excel档要则执行较长的循环(),而文档格式只能是规则的行列格式,对表格不能做更多的设置
方法二:用owc实现:
此方法也简单(owc!不又要学新东西了?放心,非常简单,只要你具备男国足的头脑就ok,什么?还不够,拖出去海扁九九八十二下,不好意思,打上瘾,多了一下),只是少为人知而已(在网上我没看到其他人用过),同样先给出代码:
<script language="javascript">
function exportexcel(atbldata){
if (typeof(export_object)!="object"){
document.body.insertadjacenthtml("afterbegin","<object style=display:none classid=clsid:0002e510-0000-0000-c000-000000000046 id=export_object></object>");
}
with (export_object){
datatype = "htmldata";
htmldata =atbldata.outerhtml;
try{
activesheet.export("c:\\owctoexcel.xls", 0);
alert(汇出完毕);
}
catch (e){
alert(汇出excel表失败,请确定已安装excel2000(或更高版本),并且没打开同名xls文件);
}
}
}
</script>
<table id="tbldata">
<tr><td>gdsssa</td><td>445</td></tr>
<tr><td>gdsssa</td><td>445</td></tr>
</table>
<input type="button" value="export" onclick="exportexcel(tbldata)">
原理:
大家应该看出这是前端脚本html实现,其实这是利用owc的功能来转出excel档,我们只需整理出一个table档(我还不会做这个table档呢!哐啷,晕了),基本上我们整理出的table在网页上是什么样子,在excel上就是什么样子,但要注意:<style>在excel里不起作用
优点:
掌握也很简单,而且可以在前台实现,并且不会设计权限问题,为前台产生excel档存到客户端而伤透脑筋的兄弟姐妹们,这个方法,走过路过,千万别错过了
缺点:
客户端必需安装excel2000或更高版本(不过这个一般都不成问题的了),而且样式表的内容不能输出。
方法三:利用xml来产生
这个嘛,先不给代码,首先讲讲道理,咳咳咳,注意啦,这可是非常这节课的重点和难点,而且每年必考,所以(所你个头啊,也不看看别人都睡着了,还不讲点有用的),啊!不好意思,拿错讲稿了。至于这个原理嘛,大家先自己随便编一个excel档,然后把它另存为*.htm文件,再打开,是不是根我们一般的网页内容非常相似(唉!什么还是老样子,用文本编辑器打开啊!讲漏一点都不行),那么我们是不是只需要整理出这些文字出来就可以了,哈哈!又是文字游戏(盯那么大的眼睛干嘛,一提到游戏就精神),关键是怎样整理出这份文本文字档案呢
(用第一个方法不就行了吗?哦!怎么我没想到,对,是太麻烦了)?我们分析一下这些文字,第一部分是<style>样式表的定义,第二部分是excel特有的定义内容,第三部分是则是最熟悉的table数据了,那么对于这三部分,前两部分基本是固定了,我整理一下基本只需要这些:
<html xmlns:s="uuid:bdc6e3f0-6da3-11d1-a2a3-00aa00c14882" xmlns:dt="uuid:c2f41010-65b3-11d1-a29f-00aa00c14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#rowsetschema" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/tr/rec-html40">
<head>
<meta http-equiv="content-type" content="text/html;"/>
<style>
.xl24{mso-style-parent:style0;mso-number-format:"\@";text-align:right;}
</style>
<xml>
<x:excelworkbook>
<x:excelworksheets>
<x:excelworksheet>
<x:name>sheet1</x:name>
<x:worksheetoptions>
<x:protectcontents>false</x:protectcontents>
<x:protectobjects>false</x:protectobjects>
<x:protectscenarios>false</x:protectscenarios>
</x:worksheetoptions>
</x:excelworksheet>
</x:excelworksheets>
</x:excelworkbook>
</xml>
</head>
(哇!压缩了这么多,难怪ms的软件都这么大),当然可能我也把一些有用的删掉了,各位发现有枉杀的帮我平反一下(不要以为学习这么容易,当然要做点事啦)
然后是第三部分,这一部分如果还是通过循环来读出然后再整理成table的html脚本,那就太不思进取了,众所周知,我们可以通过recordset来读取数据的指定内容,而且可以通过save来直接把那些内容生成一个xml文件,所以我们只需要根据这个xml文件写一个xsl文件来转换就…哈,取得西经了,那么这个方法是我认为最好的,因为我们可以任意设定table的样式,同时不必诸多循环,有效解决excel一些自做聪明的小动作(那些小动作?你在excel输入3423423423424这串数字看看,还有日期时间格式之类的),
思路就说完了,怎么实现就靠各人自己动手了。
这种方法的优点是你可以定制很好的格式的excel文件,只要你xsl会写,包括统计都可以做出来,所以懂xsl的人估计来到这里比较明白了,而且效率很高,不用再慢慢循环了
缺点嘛:就是技术要求高了一点,至于算有多高,各人不同了。
最后附上我的代码,希望大家不要停留于抄代码,能有所改进,当然有更好的方法别忘记通知一下我哦!
===========xmltoexcel.asp=============
<% set conn=server.createobject("adodb.connection")
conn.open "provider = microsoft.jet.oledb.4.0; data source ="& server.mappath("comp_apply.mdb")
strselect="select dept_no,dept_name,dept_check from dept_data"
strselect="select * from user_data" 改变该sql语句则可改变excel的内容
set rs=server.createobject("adodb.recordset")
rs.open strselect,conn,1,1
set fso=server.createobject("scripting.filesystemobject")
strxmlfile=server.mappath("xmltoexceltmp.xml")
strxslfile=server.mappath("xmltoexcel.xsl")
strexcelfile=server.mappath("xmltoexcel.xls")
if fso.fileexists(strxmlfile) then fso.deletefile strxmlfile
rs.save strxmlfile,1
rs.close
conn.close
set conn=nothing
set xmldoc=server.createobject("microsoft.xmldom")
set xsldoc=server.createobject("microsoft.xmldom")
xsldoc.load(strxslfile)
xmldoc.load(strxmlfile)
xmldoc.loadxml(xmldoc.transformnode(xsldoc))
if fso.fileexists(strexcelfile) then fso.deletefile strexcelfile
set xslfile = fso.createtextfile(strexcelfile , true)
xslfile.writeline(xmldoc.xml)
xslfile.close
fso.deletefile strxmlfile
set fso=nothing
response.redirect "xmltoexcel.xls"
%>
==============xmltoexcel.xsl=============
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"
xmlns:s=uuid:bdc6e3f0-6da3-11d1-a2a3-00aa00c14882
xmlns:dt=uuid:c2f41010-65b3-11d1-a29f-00aa00c14882
xmlns:rs=urn:schemas-microsoft-com:rowset
xmlns:z=#rowsetschema>
<xsl:output method="html" indent="yes" />
<xsl:template match="xml/rs:data">
<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">
<head>
<meta http-equiv="content-type" content="text/html;charset=gb2312" />
<style>
.xl24{mso-style-parent:style0;mso-number-format:"\@";text-align:right;}
</style>
<xml>
<x:excelworkbook>
<x:excelworksheets>
<x:excelworksheet>
<x:name>sheet1</x:name>
<x:worksheetoptions>
<x:protectcontents>false</x:protectcontents>
<x:protectobjects>false</x:protectobjects>
<x:protectscenarios>false</x:protectscenarios>
</x:worksheetoptions>
</x:excelworksheet>
</x:excelworksheets>
</x:excelworkbook>
</xml>
</head>
<body>
<table border="1" cellpadding="0" cellspacing="0">
<tr><xsl:for-each select="/xml/s:schema/s:elementtype/s:attributetype">
<th><xsl:value-of select="@name" /></th>
</xsl:for-each>
</tr>
<xsl:apply-templates select="z:row" />
</table>
</body>
</html>
</xsl:template>
<xsl:template match="z:row">
<xsl:variable name="position"><xsl:value-of select="position()" /></xsl:variable>
<tr><xsl:for-each select="/xml/s:schema/s:elementtype/s:attributetype"> <!– 读取定义好的数据列名 –>
<xsl:variable name="strcolumn"><xsl:value-of select="@name" /></xsl:variable>
<xsl:variable name="strvalue"><xsl:value-of select="/xml/rs:data/z:row[position()=$position]/@*[name() = $strcolumn]" /></xsl:variable>
<td class="xl24">
<xsl:value-of select="$strvalue" />
</td>
</xsl:for-each>
</tr>
</xsl:template>
</xsl:stylesheet>
补充一下:对于第三种方法因为recordset 用 save方法后有时对于那些null的字段在row中不会有它的字段名,所以我现在通过去elementtype那里读取字段名,以保证不造成到时的excel的纵列方位出错,但这个问题在.net里的dataset确很好解决,因为dataset中如果碰到这种null的数据它也会有<columnname />这样的空节点出现
