Posted on 2008-10-21 21:41
S.l.e!ep.¢% 閱讀(3434)
評論(0) 編輯 收藏 引用 所屬分類:
VBScript
Function
?GetCellValue(excelSheet,?row,?column,?path)
????????
on
?
error
?
resume
?
next
????????
Set
?Wshshell?
=
?
CreateObject
(
"
Wscript.shell
"
)
????????
Set
?ExcelApp?
=
?
CreateObject
(
"
excel.Application
"
)
????????ExcelApp.Visible?
=
?
True
????????
Set
?newBook?
=
?ExcelApp.Workbooks.Open(path)
????????
If
??Err?
=
?
0
?
Then
????????????????
Set
?excelSheet?
=
?ExcelApp.ActiveSheet
????????????????GetCellValue?
=
?excelSheet.Cells(row,?column)
????????????????ExcelApp.Quit
????????????????Wshshell.Popup?GetCellValue,
2
,
"
獲取的Excel單元格的值為:
"
,
0
+
64
?????????
Else
????????????????Wshshell.Popup?
"
請確認文件是否已經創建
"
,
3
,
"
文件不存在
"
,
0
+
64
????????????????ExcelApp.Quit
?????????
End
?
If
End?Function
Call
?GetCellValue(
"
excel
"
,
9
,
9
,
"
E:/excel.xls
"
)

寫入:
Function
?WExcel(row,col,value,path)
????????
Set
?Wshshell?
=
?
Createobject
(
"
Wscript.shell
"
)
????????Err?
=
?
0
????????
on
?
error
?
resume
?
next
????
Dim
?fso,f
????
Set
?fso?
=
?
CreateObject
(
"
Scripting.FileSystemObject
"
)
????
Set
?f?
=
?fso.GetFile(path)
???????????
Set
?ExcelApp?
=
?
CreateObject
(
"
Excel.Application
"
)
????????ExcelApp.Visible?
=
?
true
????????
If
??Err?
=
?
0
?
Then
????????????????
Set
?newBook?
=
?ExcelApp.Workbooks.Open(path)
????????????????newBook.Worksheets(
1
).Activate
????????????????newBook.Worksheets(
1
).Cells(row,col).value
=
value
????????????????newBook.Worksheets(
1
).Name
=
"
excel
"
????????????????newBook.Save
????????????????ExcelApp.Application.quit
????????????????
set
?newBook?
=
?
nothing
????????????????
Set
?ExcelApp?
=
?
nothing
?????????
Elseif
?Err?
=
?
53
?
Then
????????????????
Set
?newBook?
=
?ExcelApp.Workbooks.Add
????????????????newBook.Worksheets(
1
).Activate
????????????????newBook.Worksheets(
1
).Cells(row,col).value
=
value?????????????????
????????????????newBook.Worksheets(
1
).Name
=
"
excel
"
????????????????newBook.SaveAs?path
????????????????ExcelApp.Application.quit
????????????????
set
?newBook?
=
?
nothing
????????????????
Set
?ExcelApp?
=
?
nothing
?????????
Else
????????????????Wshshell.Popup?
"
發生未知錯誤
"
,?
5
,?
"
無法繼續
"
,
0
+
32
????????
?????????
End
?
If
End?Function
Call
?WExcel(
9
,
9
,
"
liuyi
"
,
"
E:/excel.xls
"
)