ASP导出到Excel
ASP导出到Excel
http://tomieric.wordpress.com/2010/06/24/export_to_excel/
简单导出到excel二则
1.利用IE和html的ContentType。
最简单且可导出6W以内记录。
01
|
'-------------------------------------------
|
02
|
'showData.asp
|
03
|
'-------------------------------------------
|
04
|
<%
|
05
|
dim html
|
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>"
|
07
|
response.write html
|
08
|
%>
|
09
|
<form action= "export.asp" method= "post" >
|
10
|
<input type= "hidden" name= "export" value= "<%=html%>" >
|
11
|
<input type= "button" name= "submit" value= "导出当前页面表数据" >
|
12
|
</form>
|
01
|
'-------------------------------
|
02
|
'export.asp
|
03
|
'导出最大65,000条记录
|
04
|
'-------------------------------
|
05
|
<%
|
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
|
10
|
Response.Expires = 0
|
11
|
Response.CacheControl = "no-cache"
|
12
|
Response.AddHeader "Pragma" , "No-Cache"
|
13
|
dim i
|
14
|
i=1
|
15
|
response.Write "<table>"
|
16
|
while i<=75000
|
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>"
|
18
|
i=i+1
|
19
|
if i=65000 then response.end
|
20
|
wend
|
21
|
response.Write "</table>"
|
22
|
%>
|
2.利用模版生成excel文件
主目录下有download文件夹,download文件夹有temp.xls模版文件和temp文件夹(临时存放文件)。
需设置服务器端对temp文件夹设置可写权限。
先建立所需模版,利用查询出来的结果集再插入excel表中
1
|
‘————————————–
|
2
|
‘export.asp
|
3
|
‘————————————–
|
4
|
<meta http-equiv=Content-Type content=”text/html; charset=GB2312″>
|
5
|
<%
|
6
|
Response.Charset= “GB2312″
|
7
|
Response.ContentType=”application/vnd.ms-excel”
|
8
|
response.write request(“export”)
|
9
|
%>
|
001
|
‘——————————-
|
002
|
‘模版字段 zd1,zd2,zd3….zdn(估计查询结果字段最多数量)
|
003
|
‘生成文件需删除每个文件第一行
|
004
|
‘固定格式,下面的中文标题转换就不需要做了,中文标题作为excel表temp表的字段
|
005
|
‘——————————-
|
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 “没有数据…”
|
012
|
response. End ()
|
013
|
else
|
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
|
019
|
next
|
020
|
end if
|
021
|
|
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)
|
032
|
if (d1<d2) then
|
033
|
fso.deleteFile(server.mappath(“download/temp/”&file.name )) ‘删除前一天生成的文件
|
034
|
end if
|
035
|
next
|
036
|
|
037
|
|
038
|
‘——————打开excel———————————
|
039
|
|
040
|
Dim Xls_conn,Xls_Connstr,Xls_rs,Xls_Sql,iflag
|
041
|
dim NewFileName,NewFileName1,FileName
|
042
|
|
043
|
TempSource= Server.MapPath(“download/temp.xls”)
|
044
|
NewFileName1=”download/temp/” & Generator(10) & “.xls”
|
045
|
TempEnd=Server.MapPath( NewFileName1)
|
046
|
Call CopyFiles(TempSource,TempEnd) ‘复制文件到临时文件夹
|
047
|
|
048
|
FileName=”<divdownload” "><span>文件1 <a href=”" download/”&NewFileName1 & “” "><img src=”" images/btndown.gif”" /></a></span></div>”
|
049
|
|
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”)
|
055
|
|
056
|
Xls_Conn.execute(” insert into [temp$] (“&zdstr&”) values (“&TabTitle&”)”)
|
057
|
‘—————————————————————–
|
058
|
iflag=0
|
059
|
while not rs.eof
|
060
|
|
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
|
‘——————–’复制文件到临时文件夹———————————
|
063
|
|
064
|
‘生成多文件
|
065
|
TempSource= Server.MapPath(“download/temp.xls”)
|
066
|
NewFileName1=”download/temp/” & Generator(6) & “.xls”
|
067
|
TempEnd=Server.MapPath( NewFileName1)
|
068
|
Call CopyFiles(TempSource,TempEnd) ‘复制文件到临时文件夹
|
069
|
|
070
|
’———————————
|
071
|
|
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&”)”)
|
077
|
|
078
|
FileName=FileName&”<divdownload” "><span>文件”&i&” <a href=”" download/”&NewFileName1 & “” "><img src=”" images/sjh/btndown.gif”" /></a></span></div>”
|
079
|
|
080
|
|
081
|
end if
|
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))&”‘,”
|
087
|
else
|
088
|
Xls_sql=Xls_sql&rs(fileds_Array(i))&”,”
|
089
|
end if
|
090
|
next
|
091
|
|
092
|
Xls_sql=left(Xls_sql,len(Xls_sql)-1)&”)”
|
093
|
Xls_Conn.execute(Xls_sql)
|
094
|
|
095
|
iflag=iflag+1
|
096
|
rs.movenext
|
097
|
wend
|
098
|
%>
|
099
|
<div id=”content”>
|
100
|
<span>下载地址:</span>
|
101
|
<%=NewFileName1%></p>
|
102
|
</div>
|
103
|
|
104
|
|
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”)
|
110
|
|
111
|
Xls_Conn.execute(” insert into [temp$] (“&zdstr&”) values (“&TabTitle&”)”)
|
112
|
‘—————————————————————–
|
113
|
iflag=0
|
114
|
while not rs.eof
|
115
|
|
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
|
‘——————–’复制文件到临时文件夹———————————
|
118
|
|
119
|
‘生成多文件
|
120
|
TempSource= Server.MapPath(“download/temp.xls”)
|
121
|
NewFileName1=”download/temp/” & Generator(6) & “.xls”
|
122
|
TempEnd=Server.MapPath( NewFileName1)
|
123
|
Call CopyFiles(TempSource,TempEnd) ‘复制文件到临时文件夹
|
124
|
|
125
|
‘———————————
|
126
|
|
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&”)”)
|
132
|
|
133
|
FileName=FileName&”<divdownload” "><span>文件”&i&” <a href=”" download/”&NewFileName1 & “” "><img src=”" images/sjh/btndown.gif”" /></a></span></div>”
|
134
|
|
135
|
end if
|
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))&”‘,”
|
141
|
else
|
142
|
Xls_sql=Xls_sql&rs(fileds_Array(i))&”,”
|
143
|
end if
|
144
|
next
|
145
|
|
146
|
Xls_sql=left(Xls_sql,len(Xls_sql)-1)&”)”
|
147
|
Xls_Conn.execute(Xls_sql)
|
148
|
|
149
|
iflag=iflag+1
|
150
|
rs.movenext
|
151
|
wend
|
152
|
%>
|
153
|
<div id=”content”>
|
154
|
<span>下载地址:</span>
|
155
|
<%=NewFileName1%></p>
|
156
|
</div>
|
转载于:https://www.cnblogs.com/heartstill/archive/2011/03/09/1978502.html
ASP导出到Excel相关推荐
- Asp导出到Excel之二
response.contentType = "application/vnd.ms-excel" response.addheader "Content-Dis ...
- Asp将查询结果导出到excel里
是Office的OWC版本问题.2000,2002XP,2003各不相同,加入Office的版本检查即可,完整代码如下: <script language="javascript&qu ...
- asp.net导出到Excel的最佳选择
在实际中可能经常要遇到将数据导出到excel中. 在asp.net项目中导出到excel可以有以下三中方法: 方法 优点 缺点 1.使用active加javascript 不用刷新页面在客户端运行 2 ...
- asp导出EXCEL数字格式及自动适应宽度的问题
asp导出EXCEL数字格式及自动适应宽度的问题 下面直接进行代码演示: <% If Response.IsClientConnected Then Response.Flush Else Re ...
- ASP导出数据到excel
最近做一个用asp做的网站后台的数据导出功能时,google,baidu了很多,但是多数都是转载,代码大概都一样,但是就是报错,最后有人说通过设置MIME类型和建立模板,最后整理,测试问题终于解决.关 ...
- ASP.NET 2.0中将 GridView 导出到 Excel 文件中
下面代码实现将 GridView 导出到 Excel文件中. 值得注意的是VerifyRenderingInServerForm重载方法: MSDN上的 VerifyRenderingInServer ...
- ASP.NET 2.0中将 GridView 导出到 Excel 文件中 (转)
下面代码实现将 GridView 导出到 Excel文件中. 值得注意的是VerifyRenderingInServerForm重载方法: MSDN上的 VerifyRenderingInServer ...
- asp html转word文档,【转】利用ASP导出文档到WORD或Excel
ASP导出WORD或Excel文档(比较另类,而且超级强的.) Response.Buffer = True Response.ContentType = "文档类型" Res ...
- ASP.NET MVC Excel 导入导出 ASP.NET Web API Excel 下载 C# Excel 导入导出
注意:服务器需要安装office软件 1.Excel导入 提示:未在本地计算机上注册"Microsoft.Jet.OLEDB.4.0"提供程序. ...
- ASP导出Excel数据的四种方法
一.使用OWC 什么是OWC? OWC是Office Web Compent的缩写,即Microsoft的Office Web组件,它为在Web中绘制图形提供了灵活的同时也是最基本的机制.在一个int ...
最新文章
- 网络编程学习笔记(服务器端进程终止)
- The Complete List of MIME Types
- 50、Power Query-Text.Contains的学习
- 【转】8086内部结构及原理
- pymysql的使用(笔记)
- flask 视图函数的使用
- 学习笔记--Dubbo
- InfoPath Resource Sites
- 直接从硬盘安装XP操作系统教程
- Maven - Maven3实战学习笔记(3)使用maven构建Web应用
- ubuntu14.4安装GTK\opencv和pythoy opencv
- 基于STM32的CNC插补智能微型激光雕刻机设计-毕业设计
- 字节跳动将双月OKR调整为季度;马斯克批OpenAI违背初心:被微软控制,只顾赚钱;苹果上新348元省电保护膜|极客头条
- app抓包工具_抓包助手app下载安装_抓包助手软件最新版免费下载
- 《改变心理学的40项研究》第一章 生物学与人类行为
- 爬虫案例—京东数据爬取、数据处理及数据可视化(效果+代码)
- 类名.class 类名.this 详解
- 激光测距仪行业报告-产能、产量、销量、销售额、价格及未来趋势
- Git如何处理blob上的SHA-1冲突?
- 如何选择专业的海关数据公司合作
热门文章
- Angularjs在初始化未完毕时出现闪烁的解决办法
- js四舍五入保留两位小数(可完全保留两位)
- 插入排序InsertSort
- 每天一道剑指offer-栈的压入,弹出序列
- php正则字母,PHP匹配连续的数字或字母的正则表达式
- 计算机集群用什么网络,什么是计算机系统集群?
- python求小于n的所有素数_关于求N以内素数的python实现以及优化方法
- 当前串口号不可以或被占用_原来团建是可以不占用私人时间当然,还可以拿加班费|不一样的团建...
- Javascript:面向对象举例——矩形类及其实例化
- Echarts数据动态加载的实现样例