LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

asp中如何在客户端把查询的结果集在本机上保存为excel

admin
2015年10月27日 12:48 本文热度 5498

http://www.haolizi.net/example/view_4000.html

保存的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<% 
Class   ExcelGen 
Private   objSpreadsheet 
Private   iColOffset 
  
Private   iRowOffset 
Sub   Class_Initialize() 
Set   objSpreadsheet   =   Server.CreateObject( "OWC.Spreadsheet "
''Set   objSpreadsheet   =   Server.CreateObject( "Excel.Application ") 
iRowOffset   =   2 
iColOffset   =   2 
End   Sub 
  
Sub   Class_Terminate() 
Set   objSpreadsheet   =   Nothing   ''Clean   up 
End   Sub 
  
Public   Property   Let   ColumnOffset(iColOff) 
If   iColOff   >   0   then 
iColOffset   =   iColOff 
Else 
iColOffset   =   2 
End   If 
End   Property 
  
Public   Property   Let   RowOffset(iRowOff) 
If   iRowOff>   0   then 
iRowOffset   =   iRowOff 
Else 
iRowOffset   =   2 
End   If 
End   Property   
  
Sub   GenerateWorksheet(objRS) 
''Populates   the   Excel   worksheet   based   on   a   Recordset ''s   contents 
''Start   by   displaying   the   titles 
If   objRS.EOF   then   Exit   Sub 
Dim   objField,   iCol,   iRow 
iCol   =   iColOffset 
iRow   =   iRowOffset 
For   Each   objField   in   objRS.Fields 
objSpreadsheet.Cells(iRow,   iCol).Value   =   objField.Name 
objSpreadsheet.Columns(iCol).AutoFitColumns 
''设置Excel表里的字体 
objSpreadsheet.Cells(iRow,   iCol).Font.Bold   =   True 
objSpreadsheet.Cells(iRow,   iCol).Font.Italic   =   False 
objSpreadsheet.Cells(iRow,   iCol).Font.Size   =   10 
objSpreadsheet.Cells(iRow,   iCol).Halignment   =   2   ''居中 
iCol   =   iCol     1 
Next   ''objField 
''Display   all   of   the   data 
Do   While   Not   objRS.EOF 
iRow   =   iRow     1 
iCol   =   iColOffset 
''For   Each   objField   in   objRS.Fields 
''If   IsNull(objField.Value)   then 
''objSpreadsheet.Cells(iRow,   iCol).Value   =   " " 
''Else 
''objSpreadsheet.Cells(iRow,   iCol).Value   =   objField.Value 
''objSpreadsheet.Columns(iCol).AutoFitColumns 
''objSpreadsheet.Cells(iRow,   iCol).Font.Bold   =   False 
''objSpreadsheet.Cells(iRow,   iCol).Font.Italic   =   False 
''objSpreadsheet.Cells(iRow,   iCol).Font.Size   =   10 
''End   If 
''iCol   =   iCol     1 
''Next   ''objField 
  
  
For   i=0   to   objrs.fields.count-1 
If   IsNull(objrs.fields(i).value)   then 
objSpreadsheet.Cells(iRow,   iCol).Value   =   " " 
Elseif   i=3   then 
objSpreadsheet.Cells(iRow,   iCol).Value   =   cstr(objrs.fields(i).value& " '' "
objSpreadsheet.Columns(iCol).AutoFitColumns 
objSpreadsheet.Cells(iRow,   iCol).Font.Bold   =   False 
objSpreadsheet.Cells(iRow,   iCol).Font.Italic   =   False 
objSpreadsheet.Cells(iRow,   iCol).Font.Size   =   10 
else 
objSpreadsheet.Cells(iRow,   iCol).Value   =   objrs.fields(i).value 
objSpreadsheet.Columns(iCol).AutoFitColumns 
objSpreadsheet.Cells(iRow,   iCol).Font.Bold   =   False 
objSpreadsheet.Cells(iRow,   iCol).Font.Italic   =   False 
objSpreadsheet.Cells(iRow,   iCol).Font.Size   =   10 
End   If 
iCol   =   iCol     1 
Next   ''objField 
  
objRS.MoveNext 
Loop 
End   Sub 
  
Function   SaveWorksheet(strFileName) 
  
''Save   the   worksheet   to   a   specified   filename 
On   Error   Resume   Next 
Call   objSpreadsheet.ActiveSheet.Export(strFileName,   0) 
SaveWorksheet   =   (Err.Number   =   0) 
End   Function 
  
End   Class 
  
Dim   objRS 
Set   objRS   =   Server.CreateObject( "ADODB.Recordset "
      Set   con=Server.Createobject( "ADODB.Connection ")     
con.open   "provider=microsoft.jet.oledb.4.0;data   source= "&   server.MapPath( ". "& "/database/project.mdb "
objRS.Open   session( "sql "),   con,1,1 
Dim   SaveName 
SaveName   =   Request.Cookies( "savename ")( "name "
Dim   objExcel 
Dim   ExcelPath 
ExcelPath   =   "Excel\ "   &   SaveName   &   ".xls " 
Set   objExcel   =   New   ExcelGen 
objExcel.RowOffset   =   1 
objExcel.ColumnOffset   =   1 
objExcel.GenerateWorksheet(objRS) 
  
If   objExcel.SaveWorksheet( "c:/test.xls ")   then   %> 
<script   language= "javascript "
window.alert( "数据已经保存在C盘下test.xls文件里,请核实. "); 
history.back(); 
</script> 
<% 
Else 
Response.Write( " <script   language=javascript> window.alert(数据保存失败。); </script> "
End   If 
Set   objExcel   =   Nothing 
objRS.Close 
Set   objRS   =   Nothing 
''session( "sql ")= " " 
%> 
  
================ 
session( "Sql ")保存的是查询的sql语句   
运行完后test.xls就保存在了服务器上了。但是我想保存在客户端上(在客户端上运行完后保存在了服务器上   服务器是运行iis这台机子)
客户端: 
查询页面:   <input   type=button   value= "导出 "   onClick= "javascript:export_onclick(); "
  function   export_onclick() 
window.location.href   =   "rp_export.asp?reports_sql= " sql;//这里的sql可以用你的session( "Sql "
rp_export.asp: 
<%@   Language=VBScript%> 
<html> 
<head> 
<meta   http-equiv= "Content-Type "   content= "text/html;   charset=gb2312 "
<title> 无标题文档 </title> 
</head> 
  
<body> 
<% 
Response.Clear 
Response.ContentType   =   "text/xls " 
Response.AddHeader   "content-disposition ",   "attachment;   filename=export.xls " 
  
''点导出按钮后事件 
  
set   conn=server.createobject( "adodb.connection "
conn.open   "sql   server驱动 " 
SQL=session( "Sql "
''Set   rs=Server.CreateObject( "Adodb.RecordSet ") 
Set   rs=conn.execute(SQL) 
total=rs.fields.count 
while   not   rs.eof 
i=0 
while   i <cint(total) 
      Data=Data&rs(i)&chr(9) 
i=i 1     
wend 
Response.Write   Data&chr(13) 
Data= " " 
rs.moveNext 
wend 
rs.close 
conn.close   
Response.Flush 
Response.End 
  
%> 
</body> 
</html>

该文章在 2015/10/27 12:48:06 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved