ASP导出Excel数据的四种办法
时间:2007-08-26 18:32:00
本文介绍了四种asp导出excel数据的方法:1.使用OWC ,2.用Excel的Application组件,3.直接在IE中打开,4.导出以半角逗号隔开的csv
一、使用OWC
什么是OWC?
OWC是Office Web Compent的缩写,即Microsoft的Office Web组件,它为在Web中绘制图形提供了灵活的同时也是最基本的机制。在一个intranet环境中,如果可以假设客户机上存在特定的浏览器和一些功能强大的软件(如IE5和Office 2000),那么就有能力利用Office Web组件提供一个交互式图形开发环境。这种模式下,客户端工作站将在整个任务中分担很大的比重。
<%
Option Explicit
Class ExcelGen
Private objSpreadsheet
Private iColOffset
Private iRowOffset
Sub Class_Initialize()
Set objSpreadsheet = Server.CreateObject("OWC.Spreadsheet")
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
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")
objRS.Open "SELECT * FROM xxxx", "Provider=SQLOLEDB.1;Persist Security
Info=True;User ID=xxxx;Password=xxxx;Initial Catalog=xxxx;Data source=xxxx;"
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(Server.MapPath(ExcelPath)) then
"Response.Write "<html><body bgcolor="gainsboro" text="#000000">已保存为Excel文件.
<a href="" & server.URLEncode(ExcelPath) & "">下载</a>"
Else
Response.Write "在保存过程中有错误!"
End If
Set objExcel = Nothing
objRS.Close
Set objRS = Nothing
%>
标签:Excel,asp,导出
0
投稿
猜你喜欢
anaconda创建、查看、激活与删除虚拟环境指令总结
2023-04-09 13:01:23
python3 googletrans超时报错问题及翻译工具优化方案 附源码
2022-07-15 12:05:47
SQL Server数据在不同数据库中的应用
2008-12-24 15:34:00
Golang爬虫框架colly使用浅析
2024-02-06 23:28:06
详解基于python-django框架的支付宝支付案例
2023-01-07 12:30:22
Python企业编码生成系统总体系统设计概述
2021-03-31 09:12:19
Python多线程和队列操作实例
2023-02-06 07:23:54
python逐行读取文件内容的三种方法
2023-01-05 14:07:30
如何让对方在线查看我的程序代码?
2010-05-13 16:34:00
一个非常有代表性的javascript简易拖动类
2009-05-25 12:44:00
css学习笔记: 重置默认样式 css reset
2009-07-19 14:30:00
Tensorflow2.1实现文本中情感分类实现解析
2022-12-22 17:49:51
python操作kafka实践的示例代码
2021-04-19 10:57:34
Python人工智能深度学习CNN
2023-11-27 06:19:15
asp导出CSV格式数据
2008-12-24 21:25:00
说说CSS+Div布局中的结构与表现
2008-06-05 18:22:00
javascript在myeclipse中报错的解决方法
2024-04-22 22:41:55
深入解析Golang中JSON的编码与解码
2023-07-04 12:45:32
Oracle 的入门心得 强烈推荐
2009-05-24 19:55:00
使用sublime text3搭建Python编辑环境的实现
2023-01-20 21:01:56