本文目录一览:
ASP操作Excel的方法
代码如下:
%
'*******************************************************************
'使用说明
'Dim
a
'Set
a=new
CreateExcel
'a.SavePath=x
'保存路径
'a.SheetName=工作簿名称
'多个工作表
a.SheetName=array(工作簿名称一,工作簿名称二)
'a.SheetTitle=表名称
'可以为空
多个工作表
a.SheetName=array(表名称一,表名称二)
'a.Data
=d
'二维数组
'多个工作表
array(b,c)
b与c为二维数组
'Dim
rs
'Set
rs=server.CreateObject(Adodb.RecordSet)
'rs.open
Select
id,
classid,
className
from
[class]
,conn,
1,
1
'a.AddDBData
rs,
字段名一,字段名二,
工作簿名称,
表名称,
true
'true自动获取表字段名
'a.AddData
c,
true
,
工作簿名称,
表名称
'c二维数组
true
第一行是否为标题行
'a.AddtData
e,
Sheet1
'按模板生成
c=array(array(AA1,
内容),
array(AA2,
内容2))
'a.Create()
'a.UsedTime
生成时间,毫秒数
'a.SavePath
保存路径
'Set
a=nothing
'设置COM组件的操作权限。在命令行键入DCOMCNFG,则进入COM组件配置界面,选择MicrosoftExcel后点击属性按钮,将三个单选项一律选择自定义,编辑中将Everyone加入所有权限
'*******************************************************************
Class
CreateExcel
Private
CreateType_
Private
savePath_
Private
readPath_
Private
AuthorStr
Rem
设置作者
Private
VersionStr
Rem
设置版本
Private
SystemStr
Rem
设置系统名称
Private
SheetName_
Rem
设置表名
Private
SheetTitle_
Rem
设置标题
Private
ExcelData
Rem
设置表数据
Private
ExcelApp
Rem
Excel.Application
Private
ExcelBook
Private
ExcelSheets
Private
UsedTime_
Rem
使用的时间
Public
TitleFirstLine
Rem
首行是否标题
Private
Sub
Class_Initialize()
Server.ScriptTimeOut
=
99999
UsedTime_
=
Timer
SystemStr
=
Lc00_CreateExcelServer
AuthorStr
=
Surnfu[emailprotected]
31333716
VersionStr
=
1.0
if
not
IsObjInstalled(Excel.Application)
then
InErr(服务器未安装Excel.Application控件)
end
if
set
ExcelApp
=
createObject(Excel.Application)
ExcelApp.DisplayAlerts
=
false
ExcelApp.Application.Visible
=
false
CreateType_
=
1
readPath_
=
null
End
Sub
Private
Sub
Class_Terminate()
ExcelApp.Quit
If
Isobject(ExcelSheets)
Then
Set
ExcelSheets
=
Nothing
If
Isobject(ExcelBook)
Then
Set
ExcelBook
=
Nothing
If
Isobject(ExcelApp)
Then
Set
ExcelApp
=
Nothing
End
Sub
Public
Property
Let
ReadPath(ByVal
Val)
If
Instr(Val,
:)0
Then
readPath_
=
Trim(Val)
else
readPath_=Server.MapPath(Trim(Val))
end
if
End
Property
Public
Property
Let
SavePath(ByVal
Val)
If
Instr(Val,
:)0
Then
savePath_
=
Trim(Val)
else
savePath_=Server.MapPath(Trim(Val))
end
if
End
Property
Public
Property
Let
CreateType(ByVal
Val)
if
Val
1
and
Val
2
then
CreateType_
=
1
else
CreateType_
=
Val
end
if
End
Property
Public
Property
Let
Data(ByVal
Val)
if
not
isArray(Val)
then
InErr(表数据设置有误)
end
if
ExcelData
=
Val
End
Property
Public
Property
Get
SavePath()
SavePath
=
savePath_
End
Property
Public
Property
Get
UsedTime()
UsedTime
=
UsedTime_
End
Property
Public
Property
Let
SheetName(ByVal
Val)
if
not
isArray(Val)
then
if
Val
=
then
InErr(表名设置有误)
end
if
TitleFirstLine
=
true
else
ReDim
TitleFirstLine(Ubound(Val))
Dim
ik_
For
ik_
=
to
Ubound(Val)
TitleFirstLine(ik_)
=
true
Next
end
if
SheetName_
=
Val
End
Property
Public
Property
Let
SheetTitle(ByVal
Val)
if
not
isArray(Val)
then
if
Val
=
then
InErr(表标题设置有误)
end
if
end
if
SheetTitle_
=
Val
End
Property
Rem
检查数据
Private
Sub
CheckData()
if
savePath_
=
then
InErr(保存路径不能为空)
if
not
isArray(SheetName_)
then
asp excel错误:-2147217887
aspexcel错误-2147217887有2个原因
打开excel后,选择文件-选项-加载项-转到,然后在弹出的加载宏对话框里把所有的对钩去掉,然后确定即可。此时重新打开一遍excel就没有原来的问题了。方法是打开excel,选择工具-加载宏,然后把所有对钩去掉,确定。重新打开excel,问题解决
asp 调用 excel
using System;
using Excel;
namespace ExcelExample
{
/// summary
/// Summary description for Class1.
/// /summary
class ExcelClass
{
/// summary
/// The main entry point for the application.
/// /summary
[STAThread]
static void Main(string[] args)
{
Excel.Application excelApp = new Excel.ApplicationClass(); // Creates a new Excel Application
excelApp.Visible = false; // Makes Excel visible to the user.
// The following line adds a new workbook
Excel.Workbook newWorkbook = excelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
// The following code opens an existing workbook
string workbookPath = "c:/SomeWorkBook.xls"; // Add your own path here
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0,
false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true,
false, 0, true, false, false);
// The following gets the Worksheets collection
Excel.Sheets excelSheets = excelWorkbook.Worksheets;
// The following gets Sheet1 for editing
string currentSheet = "Sheet1";
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
// The following gets cell A1 for editing
Excel.Range excelCell = (Excel.Range)excelWorksheet.get_Range("A1", "A1");
// The following sets cell A1's value to "Hi There"
excelCell.Value2 = "Hi There";
}
}
如何用asp读取Excel文件
下面是读取一个EXCEL文件并将其写入数据库的代码实例,亲测通过:
注意:EXCEL里面的列名称要和数据库里的字段相同
%
i=0
Dim cn,oConn,connstr
'打开XLS.
Set cn = Server.CreateObject("ADODB.Connection")
cn.Provider = "Microsoft.Jet.OLEDB.4.0 "
cn.ConnectionString = "Data Source=" Server.MapPath("list.xls") ";" _
"Extended Properties=Excel 8.0;"
cn.Open
'打开MDB.
connstr="DBQ="+server.MapPath("TEST.mdb")+";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
set oConn=server.CreateObject("ADODB.CONNECTION")
oConn.open connstr
'读取数据.
set rsRead=server.CreateObject("ADODB.Recordset")
rsRead.Open "select * from [list$]",cn,1,1
do until rsRead.EOF
'写入数据库.
oConn.Execute("Insert into eer(mc)Values('" rsRead.Fields("mc") "')" )
rsRead.MoveNext
i=i+1
loop
response.write("p align=center成功导入"i"条数据/pbr")
response.Write("p align=centera href=javascript:window.close()关闭窗口/a/p")
%