[已解决]麻烦高手给个如何读取当前正在打开的excel文件的方法
本帖最后由 silvay22 于 2013-4-25 19:50 编辑如题:读取当前excel中4行3列的值 不知道为何你要读取EXCEL,读ini不行吗?
我晕!
读ini我也会,问题就是要用到excel才这样提问的。 可能要用到建立对象来解决。 $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, "", 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 原帖由 xwjsyyx 于 2008-7-5 20:39 发表 http://www.autoitx.com/images/common/back.gif
$FilePath=@TempDir & "\temp.xls"
$oExcel=_ExcelBookOpen($FilePath)
$sReadCell = _ExcelReadCell($oExcel, "C4")
MsgBox(0, "Cell C4", $sReadCell)
_ExcelBookClose($oExcel)
Func _ExcelReadCell($oE ...
强。 直接把库文件给贴出来了 高人强啊~~~~ 同问:face (37): 有没有直接读取DBF的UDF呢? 有没有直接读取DBF的UDF呢?
menfan 发表于 2009-7-24 10:48 http://www.autoitx.com/images/common/back.gif
http://www.autoitx.com/forum.php?mod=viewthread&tid=2943&page=1
比较难啊。。。au3支持不好
页:
[1]