|
发表于 2008-7-5 20:39:10
|
显示全部楼层
$FilePath=@TempDir & "\temp.xls"
$oExcel=_ExcelBookOpen($FilePath)
$sReadCell = _ExcelReadCell($oExcel, "C4")
MsgBox(0, "Cell C4", $sReadCell)
_ExcelBookClose($oExcel)
Func _ExcelReadCell($oExcel, $sRangeOrRow, $iColumn = 1)
If NOT IsObj($oExcel) Then Return SetError(1, 0, 0)
If NOT StringRegExp($sRangeOrRow, "[A-Z,a-z]", 0) Then
If $sRangeOrRow < 1 Then Return SetError(2, 0, 0)
If $iColumn < 1 Then Return SetError(2, 1, 0)
Return $oExcel.Activesheet.Cells($sRangeOrRow, $iColumn).Value
Else
Return $oExcel.Activesheet.Range($sRangeOrRow).Value
EndIf
EndFunc ;==>_ExcelReadCell
Func _ExcelBookOpen($sFilePath, $fVisible = 1, $fReadOnly = False, $sPassword = "", $sWritePassword = "")
Local $oExcel = ObjCreate("Excel.Application")
If NOT IsObj($oExcel) Then Return SetError(1, 0, 0)
If NOT FileExists($sFilePath) Then Return SetError(2, 0, 0)
If $fVisible > 1 Then $fVisible = 1
If $fVisible < 0 Then $fVisible = 0
If $fReadOnly > 1 Then $fReadOnly = 1
If $fReadOnly < 0 Then $fReadOnly = 0
With $oExcel
.Visible = $fVisible
If $sPassword <> "" And $sWritePassword <> "" Then .WorkBooks.Open($sFilePath, Default, $fReadOnly, Default, $sPassword, $sWritePassword)
If $sPassword = "" And $sWritePassword <> "" Then .WorkBooks.Open($sFilePath, Default, $fReadOnly, Default, Default, $sWritePassword)
If $sPassword <> "" And $sWritePassword = "" Then .WorkBooks.Open($sFilePath, Default, $fReadOnly, Default, $sPassword, Default)
If $sPassword = "" And $sWritePassword = "" Then .WorkBooks.Open($sFilePath, Default, $fReadOnly)
.ActiveWorkbook.Sheets(1).Select()
EndWith
Return $oExcel
EndFunc ;==>_ExcelBookOpen
Func _ExcelBookClose($oExcel, $fSave = 1, $fAlerts = 0)
If NOT IsObj($oExcel) Then Return SetError(1, 0, 0)
If $fSave > 1 Then $fSave = 1
If $fSave < 0 Then $fSave = 0
If $fAlerts > 1 Then $fAlerts = 1
If $fAlerts < 0 Then $fAlerts = 0
$oExcel.Application.DisplayAlerts = $fAlerts
$oExcel.Application.ScreenUpdating = $fAlerts
If $fSave Then
$oExcel.ActiveWorkBook.Save
EndIf
$oExcel.Application.DisplayAlerts = True
$oExcel.Application.ScreenUpdating = True
$oExcel.Quit
Return 1
EndFunc ;==>_ExcelBookClose |
|