http://tomieric.wordpress.com/2010/06/24/export_to_excel/
简单导出到excel二则
1.利用IE和html的ContentType。
最简单且可导出6W以内记录。
06 |
html="<table><tr><td>105</td><td>105223</td><td>中国</td><td>北京</td><td>广州</td><td>4.5</td><td>0</td><td>茂名</td><td>树仔</td><td>内园</td><td>电白 </td></tr><tr><td>105</td><td>105223</td><td>中国</td><td>北京</td><td>广州</td><td>4.5</td><td>0</td><td>茂名</td><td>树仔</td><td>内园</td><td>电白 </td></tr><tr><td>105</td><td>105223</td><td>中国</td><td>北京</td><td>广州</td><td>4.5</td><td>0</td><td>茂名</td><td>树仔</td><td>内园</td><td>电白 </td></tr><tr><td>105</td><td>105223</td><td>中国</td><td>北京</td><td>广州</td><td>4.5</td><td>0</td><td>茂名</td><td>树仔</td><td>内园</td><td>电白 </td></tr></table>"
|
09 |
<form action="export.asp" method="post">
|
10 |
<input type="hidden" name="export" value="<%=html%>">
|
11 |
<input type="button" name="submit" value="导出当前页面表数据">
|
06 |
Response.ContentType="application/vnd.ms-excel"
|
07 |
Response.AddHeader "content-disposition","attachment;filename=Test.xls"
|
08 |
Response.Buffer = True
|
09 |
Response.ExpiresAbsolute = Now() - 1 |
11 |
Response.CacheControl = "no-cache"
|
12 |
Response.AddHeader "Pragma", "No-Cache"
|
15 |
response.Write "<table>"
|
17 |
response.Write "<tr><td>105</td><td>105223</td><td>中国</td><td>北京</td><td>广州</td><td>4.5</td><td>0</td><td>茂名</td><td>树仔</td><td>内园</td><td>电白 </td></tr>"
|
19 |
if i=65000 then response.end |
21 |
response.Write "</table>"
|
2.利用模版生成excel文件
主目录下有download文件夹,download文件夹有temp.xls模版文件和temp文件夹(临时存放文件)。
需设置服务器端对temp文件夹设置可写权限。
先建立所需模版,利用查询出来的结果集再插入excel表中
4 |
<meta http-equiv=Content-Type content=”text/html; charset=GB2312″> |
6 |
Response.Charset= “GB2312″ |
7 |
Response.ContentType=”application/vnd.ms-excel” |
8 |
response.write request(“export”) |
002 |
‘模版字段 zd1,zd2,zd3….zdn(估计查询结果字段最多数量) |
004 |
‘固定格式,下面的中文标题转换就不需要做了,中文标题作为excel表temp表的字段 |
006 |
Sql = “select * From books” |
007 |
Set Rs = Db.CreateRS()
|
008 |
Rs.Open Sql, Db.Conn, 1, 1 |
009 |
Dim i,TabTitle,zdstr ‘i,表头,输出字段
|
010 |
if rs.eof and rs.bof then
|
011 |
response.Write “没有数据…”
|
014 |
for i=0 to rs.fields.count-1
|
015 |
ReDim Preserve fileds_Array(i)
|
016 |
TabTitle=TabTitle&”‘”&ChangeToCn(rs.fields(i).name)&”‘,” ‘交换成中文字段,输出标题
|
017 |
zdstr=zdstr&”zd”&cstr(i)&”,”
|
018 |
fileds_Array(i)=rs.fields(i).name
|
022 |
zdstr=left(zdstr,len(zdstr)-1) ‘去掉末尾“,”
|
023 |
TabTitle=left(TabTitle,len(TabTitle)-1) ‘去掉末尾“,”
|
024 |
‘———————————-删除之前生成的文件—————- |
025 |
dim d1,d2,fso,path,temp
|
026 |
set fso = createobject(“scripting.filesystemobject”)
|
027 |
path = server.mappath(“download/temp/”)
|
028 |
Set temp = fso.getfolder(path)
|
029 |
for each file in temp.Files
|
030 |
d1=FormatDateTime(ShowDateCreated(server.mappath(“download/temp/”&file.name )),2)
|
031 |
d2=FormatDateTime(now(),2)
|
033 |
fso.deleteFile(server.mappath(“download/temp/”&file.name )) ‘删除前一天生成的文件
|
038 |
‘——————打开excel——————————— |
040 |
Dim Xls_conn,Xls_Connstr,Xls_rs,Xls_Sql,iflag
|
041 |
dim NewFileName,NewFileName1,FileName
|
043 |
TempSource= Server.MapPath(“download/temp.xls”)
|
044 |
NewFileName1=”download/temp/” & Generator(10) & “.xls”
|
045 |
TempEnd=Server.MapPath( NewFileName1)
|
046 |
Call CopyFiles(TempSource,TempEnd) ‘复制文件到临时文件夹
|
048 |
FileName=”<divdownload”"><span>文件1 <a href=”"download/”&NewFileName1 & “”"><img src=”"images/btndown.gif”" /></a></span></div>”
|
050 |
Set Xls_conn=Server.CreateObject(“ADODB.Connection”)
|
051 |
if Xls_conn.state then set Xls_conn=nothing ‘如果连接已经打开.则先关闭
|
052 |
Xls_ConnStr=”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” &Server.MapPath(NewFileName1)& “;Extended Properties=Excel 8.0″ ‘连接字符串
|
053 |
Xls_conn.Open Xls_ConnStr
|
054 |
Set Xls_rs = Server.CreateObject(“ADODB.Recordset”)
|
056 |
Xls_Conn.execute(” insert into [temp$] (“&zdstr&”) values (“&TabTitle&”)”)
|
057 |
‘—————————————————————– |
061 |
if i=60000 or i=120000 or i=18000 or i=240000 or i=30000 or i=36000 or i=420000 or i=480000 then ’分文件存放,excel表最多存放65,000条记录
|
062 |
‘——————–’复制文件到临时文件夹———————————
|
065 |
TempSource= Server.MapPath(“download/temp.xls”)
|
066 |
NewFileName1=”download/temp/” & Generator(6) & “.xls”
|
067 |
TempEnd=Server.MapPath( NewFileName1)
|
068 |
Call CopyFiles(TempSource,TempEnd) ‘复制文件到临时文件夹
|
072 |
Set Xls_conn=Server.CreateObject(“ADODB.Connection”)
|
073 |
if Xls_conn.state then set Xls_conn=nothing ‘如果连接已经打开.则先关闭
|
074 |
Xls_Connstr=”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” &Server.MapPath(NewFileName1)& “;Extended Properties=Excel 8.0″
|
075 |
Xls_conn.Open Xls_Connstr
|
076 |
Xls_Conn.execute(” insert into [temp$] (“&zdstr&”) values (“&TabTitle&”)”)
|
078 |
FileName=FileName&”<divdownload”"><span>文件”&i&” <a href=”"download/”&NewFileName1 & “”"><img src=”"images/sjh/btndown.gif”" /></a></span></div>”
|
082 |
‘可用select val1,val2 into temp
|
083 |
Xls_sql=”insert into [temp$] (“&zdstr&”) values (“
|
084 |
For i=0 To ubound(fileds_Array)
|
085 |
if CheckFieldsInt(rs.fields(i).name)=1 then
|
086 |
Xls_sql=Xls_sql&”‘”&rs(fileds_Array(i))&”‘,”
|
088 |
Xls_sql=Xls_sql&rs(fileds_Array(i))&”,”
|
092 |
Xls_sql=left(Xls_sql,len(Xls_sql)-1)&”)”
|
093 |
Xls_Conn.execute(Xls_sql)
|
101 |
<%=NewFileName1%></p> |
105 |
Set Xls_conn=Server.CreateObject(“ADODB.Connection”)
|
106 |
if Xls_conn.state then set Xls_conn=nothing ‘如果连接已经打开.则先关闭 |
107 |
Xls_ConnStr=”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” &Server.MapPath(NewFileName1)& “;Extended Properties=Excel 8.0″ ‘连接字符串 |
108 |
Xls_conn.Open Xls_ConnStr |
109 |
Set Xls_rs = Server.CreateObject(“ADODB.Recordset”)
|
111 |
Xls_Conn.execute(” insert into [temp$] (“&zdstr&”) values (“&TabTitle&”)”) |
112 |
‘—————————————————————– |
116 |
if i=60000 or i=120000 or i=18000 or i=240000 or i=30000 or i=36000 or i=420000 or i=480000 then ’分文件存放,excel表最多存放65,000条记录 |
117 |
‘——————–’复制文件到临时文件夹——————————— |
120 |
TempSource= Server.MapPath(“download/temp.xls”) |
121 |
NewFileName1=”download/temp/” & Generator(6) & “.xls” |
122 |
TempEnd=Server.MapPath( NewFileName1) |
123 |
Call CopyFiles(TempSource,TempEnd) ‘复制文件到临时文件夹
|
127 |
Set Xls_conn=Server.CreateObject(“ADODB.Connection”)
|
128 |
if Xls_conn.state then set Xls_conn=nothing ‘如果连接已经打开.则先关闭 |
129 |
Xls_Connstr=”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” &Server.MapPath(NewFileName1)& “;Extended Properties=Excel 8.0″ |
130 |
Xls_conn.Open Xls_Connstr |
131 |
Xls_Conn.execute(” insert into [temp$] (“&zdstr&”) values (“&TabTitle&”)”) |
133 |
FileName=FileName&”<divdownload”"><span>文件”&i&” <a href=”"download/”&NewFileName1 & “”"><img src=”"images/sjh/btndown.gif”" /></a></span></div>”
|
136 |
‘可用select val1,val2 into temp |
137 |
Xls_sql=”insert into [temp$] (“&zdstr&”) values (“ |
138 |
For i=0 To ubound(fileds_Array)
|
139 |
if CheckFieldsInt(rs.fields(i).name)=1 then |
140 |
Xls_sql=Xls_sql&”‘”&rs(fileds_Array(i))&”‘,” |
142 |
Xls_sql=Xls_sql&rs(fileds_Array(i))&”,” |
146 |
Xls_sql=left(Xls_sql,len(Xls_sql)-1)&”)” |
147 |
Xls_Conn.execute(Xls_sql) |
155 |
<%=NewFileName1%></p> |
http://tomieric.wordpress.com/2010/06/24/export_to_excel/
简单导出到excel二则
1.利用IE和html的ContentType。
最简单且可导出6W以内记录。
06 |
html="<table><tr><td>105</td><td>105223</td><td>中国</td><td>北京</td><td>广州</td><td>4.5</td><td>0</td><td>茂名</td><td>树仔</td><td>内园</td><td>电白 </td></tr><tr><td>105</td><td>105223</td><td>中国</td><td>北京</td><td>广州</td><td>4.5</td><td>0</td><td>茂名</td><td>树仔</td><td>内园</td><td>电白 </td></tr><tr><td>105</td><td>105223</td><td>中国</td><td>北京</td><td>广州</td><td>4.5</td><td>0</td><td>茂名</td><td>树仔</td><td>内园</td><td>电白 </td></tr><tr><td>105</td><td>105223</td><td>中国</td><td>北京</td><td>广州</td><td>4.5</td><td>0</td><td>茂名</td><td>树仔</td><td>内园</td><td>电白 </td></tr></table>"
|
09 |
<form action="export.asp" method="post">
|
10 |
<input type="hidden" name="export" value="<%=html%>">
|
11 |
<input type="button" name="submit" value="导出当前页面表数据">
|
06 |
Response.ContentType="application/vnd.ms-excel"
|
07 |
Response.AddHeader "content-disposition","attachment;filename=Test.xls"
|
08 |
Response.Buffer = True
|
09 |
Response.ExpiresAbsolute = Now() - 1 |
11 |
Response.CacheControl = "no-cache"
|
12 |
Response.AddHeader "Pragma", "No-Cache"
|
15 |
response.Write "<table>"
|
17 |
response.Write "<tr><td>105</td><td>105223</td><td>中国</td><td>北京</td><td>广州</td><td>4.5</td><td>0</td><td>茂名</td><td>树仔</td><td>内园</td><td>电白 </td></tr>"
|
19 |
if i=65000 then response.end |
21 |
response.Write "</table>"
|
2.利用模版生成excel文件
主目录下有download文件夹,download文件夹有temp.xls模版文件和temp文件夹(临时存放文件)。
需设置服务器端对temp文件夹设置可写权限。
先建立所需模版,利用查询出来的结果集再插入excel表中
4 |
<meta http-equiv=Content-Type content=”text/html; charset=GB2312″> |
6 |
Response.Charset= “GB2312″ |
7 |
Response.ContentType=”application/vnd.ms-excel” |
8 |
response.write request(“export”) |
002 |
‘模版字段 zd1,zd2,zd3….zdn(估计查询结果字段最多数量) |
004 |
‘固定格式,下面的中文标题转换就不需要做了,中文标题作为excel表temp表的字段 |
006 |
Sql = “select * From books” |
007 |
Set Rs = Db.CreateRS()
|
008 |
Rs.Open Sql, Db.Conn, 1, 1 |
009 |
Dim i,TabTitle,zdstr ‘i,表头,输出字段
|
010 |
if rs.eof and rs.bof then
|
011 |
response.Write “没有数据…”
|
014 |
for i=0 to rs.fields.count-1
|
015 |
ReDim Preserve fileds_Array(i)
|
016 |
TabTitle=TabTitle&”‘”&ChangeToCn(rs.fields(i).name)&”‘,” ‘交换成中文字段,输出标题
|
017 |
zdstr=zdstr&”zd”&cstr(i)&”,”
|
018 |
fileds_Array(i)=rs.fields(i).name
|
022 |
zdstr=left(zdstr,len(zdstr)-1) ‘去掉末尾“,”
|
023 |
TabTitle=left(TabTitle,len(TabTitle)-1) ‘去掉末尾“,”
|
024 |
‘———————————-删除之前生成的文件—————- |
025 |
dim d1,d2,fso,path,temp
|
026 |
set fso = createobject(“scripting.filesystemobject”)
|
027 |
path = server.mappath(“download/temp/”)
|
028 |
Set temp = fso.getfolder(path)
|
029 |
for each file in temp.Files
|
030 |
d1=FormatDateTime(ShowDateCreated(server.mappath(“download/temp/”&file.name )),2)
|
031 |
d2=FormatDateTime(now(),2)
|
033 |
fso.deleteFile(server.mappath(“download/temp/”&file.name )) ‘删除前一天生成的文件
|
038 |
‘——————打开excel——————————— |
040 |
Dim Xls_conn,Xls_Connstr,Xls_rs,Xls_Sql,iflag
|
041 |
dim NewFileName,NewFileName1,FileName
|
043 |
TempSource= Server.MapPath(“download/temp.xls”)
|
044 |
NewFileName1=”download/temp/” & Generator(10) & “.xls”
|
045 |
TempEnd=Server.MapPath( NewFileName1)
|
046 |
Call CopyFiles(TempSource,TempEnd) ‘复制文件到临时文件夹
|
048 |
FileName=”<divdownload”"><span>文件1 <a href=”"download/”&NewFileName1 & “”"><img src=”"images/btndown.gif”" /></a></span></div>”
|
050 |
Set Xls_conn=Server.CreateObject(“ADODB.Connection”)
|
051 |
if Xls_conn.state then set Xls_conn=nothing ‘如果连接已经打开.则先关闭
|
052 |
Xls_ConnStr=”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” &Server.MapPath(NewFileName1)& “;Extended Properties=Excel 8.0″ ‘连接字符串
|
053 |
Xls_conn.Open Xls_ConnStr
|
054 |
Set Xls_rs = Server.CreateObject(“ADODB.Recordset”)
|
056 |
Xls_Conn.execute(” insert into [temp$] (“&zdstr&”) values (“&TabTitle&”)”)
|
057 |
‘—————————————————————– |
061 |
if i=60000 or i=120000 or i=18000 or i=240000 or i=30000 or i=36000 or i=420000 or i=480000 then ’分文件存放,excel表最多存放65,000条记录
|
062 |
‘——————–’复制文件到临时文件夹———————————
|
065 |
TempSource= Server.MapPath(“download/temp.xls”)
|
066 |
NewFileName1=”download/temp/” & Generator(6) & “.xls”
|
067 |
TempEnd=Server.MapPath( NewFileName1)
|
068 |
Call CopyFiles(TempSource,TempEnd) ‘复制文件到临时文件夹
|
072 |
Set Xls_conn=Server.CreateObject(“ADODB.Connection”)
|
073 |
if Xls_conn.state then set Xls_conn=nothing ‘如果连接已经打开.则先关闭
|
074 |
Xls_Connstr=”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” &Server.MapPath(NewFileName1)& “;Extended Properties=Excel 8.0″
|
075 |
Xls_conn.Open Xls_Connstr
|
076 |
Xls_Conn.execute(” insert into [temp$] (“&zdstr&”) values (“&TabTitle&”)”)
|
078 |
FileName=FileName&”<divdownload”"><span>文件”&i&” <a href=”"download/”&NewFileName1 & “”"><img src=”"images/sjh/btndown.gif”" /></a></span></div>”
|
082 |
‘可用select val1,val2 into temp
|
083 |
Xls_sql=”insert into [temp$] (“&zdstr&”) values (“
|
084 |
For i=0 To ubound(fileds_Array)
|
085 |
if CheckFieldsInt(rs.fields(i).name)=1 then
|
086 |
Xls_sql=Xls_sql&”‘”&rs(fileds_Array(i))&”‘,”
|
088 |
Xls_sql=Xls_sql&rs(fileds_Array(i))&”,”
|
092 |
Xls_sql=left(Xls_sql,len(Xls_sql)-1)&”)”
|
093 |
Xls_Conn.execute(Xls_sql)
|
101 |
<%=NewFileName1%></p> |
105 |
Set Xls_conn=Server.CreateObject(“ADODB.Connection”)
|
106 |
if Xls_conn.state then set Xls_conn=nothing ‘如果连接已经打开.则先关闭 |
107 |
Xls_ConnStr=”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” &Server.MapPath(NewFileName1)& “;Extended Properties=Excel 8.0″ ‘连接字符串 |
108 |
Xls_conn.Open Xls_ConnStr |
109 |
Set Xls_rs = Server.CreateObject(“ADODB.Recordset”)
|
111 |
Xls_Conn.execute(” insert into [temp$] (“&zdstr&”) values (“&TabTitle&”)”) |
112 |
‘—————————————————————– |
116 |
if i=60000 or i=120000 or i=18000 or i=240000 or i=30000 or i=36000 or i=420000 or i=480000 then ’分文件存放,excel表最多存放65,000条记录 |
117 |
‘——————–’复制文件到临时文件夹——————————— |
120 |
TempSource= Server.MapPath(“download/temp.xls”) |
121 |
NewFileName1=”download/temp/” & Generator(6) & “.xls” |
122 |
TempEnd=Server.MapPath( NewFileName1) |
123 |
Call CopyFiles(TempSource,TempEnd) ‘复制文件到临时文件夹
|
127 |
Set Xls_conn=Server.CreateObject(“ADODB.Connection”)
|
128 |
if Xls_conn.state then set Xls_conn=nothing ‘如果连接已经打开.则先关闭 |
129 |
Xls_Connstr=”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” &Server.MapPath(NewFileName1)& “;Extended Properties=Excel 8.0″ |
130 |
Xls_conn.Open Xls_Connstr |
131 |
Xls_Conn.execute(” insert into [temp$] (“&zdstr&”) values (“&TabTitle&”)”) |
133 |
FileName=FileName&”<divdownload”"><span>文件”&i&” <a href=”"download/”&NewFileName1 & “”"><img src=”"images/sjh/btndown.gif”" /></a></span></div>”
|
136 |
‘可用select val1,val2 into temp |
137 |
Xls_sql=”insert into [temp$] (“&zdstr&”) values (“ |
138 |
For i=0 To ubound(fileds_Array)
|
139 |
if CheckFieldsInt(rs.fields(i).name)=1 then |
140 |
Xls_sql=Xls_sql&”‘”&rs(fileds_Array(i))&”‘,” |
142 |
Xls_sql=Xls_sql&rs(fileds_Array(i))&”,” |
146 |
Xls_sql=left(Xls_sql,len(Xls_sql)-1)&”)” |
147 |
Xls_Conn.execute(Xls_sql) |
155 |
<%=NewFileName1%></p> |
相关文章:
-
2021-08-27
-
2022-12-23
-
2021-10-24
-
2022-12-23
-
2022-12-23
-
2021-11-05
-
2021-07-31
-
2022-01-13