用asp语言,读取数据库内容生成excel并保存
时间: 2024-05-06 09:20:27 浏览: 134
c#把读取数据库的内容保存成excel(完整代码,加入及用)
5星 · 资源好评率100%
以下是使用ASP语言读取数据库内容生成Excel并保存的示例代码:
```
<%
'连接数据库
Dim conn
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\data\mydatabase.mdb"
'查询数据
Dim rs
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open "SELECT * FROM mytable", conn
'创建Excel对象
Dim excel
Set excel = Server.CreateObject("Excel.Application")
excel.Visible = False
'添加工作簿
Dim workbook
Set workbook = excel.Workbooks.Add()
'添加工作表
Dim worksheet
Set worksheet = workbook.Worksheets.Add()
worksheet.Name = "My Sheet"
'写入数据
Dim row, column
row = 1
column = 1
Do While Not rs.EOF
worksheet.Cells(row, column).Value = rs("column1")
worksheet.Cells(row, column + 1).Value = rs("column2")
row = row + 1
rs.MoveNext
Loop
'保存Excel文件
workbook.SaveAs "C:\data\myexcel.xls"
'关闭Excel对象
workbook.Close False
excel.Quit
'释放对象
Set worksheet = Nothing
Set workbook = Nothing
Set excel = Nothing
Set rs = Nothing
Set conn = Nothing
'Redirect到Excel文件
Response.Redirect "C:\data\myexcel.xls"
%>
```
请注意,此代码需要正确的数据库连接字符串和正确的文件路径才能正常工作。此外,如果您的服务器上没有安装Microsoft Excel,则此代码也将无法正常工作。
阅读全文