Excel Datei öffnen und Auslesen

  • Hallo Forumuser,

    ich habe folgenden Code erstellt!!!


    [autoit]

    #cs ----------------------------------------------------------------------------

    [/autoit][autoit][/autoit][autoit]

    AutoIt Version: 3.3.0.0
    Author: Thomas Böhmer

    [/autoit][autoit][/autoit][autoit]

    Script Function:
    Template AutoIt script.

    [/autoit][autoit][/autoit][autoit]

    #ce ----------------------------------------------------------------------------

    [/autoit][autoit][/autoit][autoit]

    ; Script Start - Add your code below here

    [/autoit][autoit][/autoit][autoit]

    $oExcel = ObjCreate("Excel.Application") ; Create an Excel Object
    $AN = "Abcde"

    [/autoit][autoit][/autoit][autoit]

    With $oExcel

    [/autoit][autoit][/autoit][autoit]

    .Visible = 1 ; Let Excel show itself

    [/autoit][autoit][/autoit][autoit]

    .WorkBooks.Add ; Add a new workbook
    .ActiveWorkBook.ActiveSheet.Cells(1,3).Value="Abcde" ; Fill a cell
    .ActiveWorkBook.ActiveSheet.Cells(5,2).Value="Acde"
    .ActiveWorkBook.ActiveSheet.Cells(5,3).Value="Abcde"
    .ActiveWorkBook.ActiveSheet.Cells(5,6).Value="Abcd"
    .ActiveWorkBook.ActiveSheet.Cells(20,6).Value="Abcde"
    .ActiveWorkBook.ActiveSheet.Cells(1,1).Value="1" ; Fill a cell
    .ActiveWorkBook.ActiveSheet.Cells(5,1).Value="5"
    .ActiveWorkBook.ActiveSheet.Cells(5,1).Value="5"
    .ActiveWorkBook.ActiveSheet.Cells(5,1).Value="5"
    .ActiveWorkBook.ActiveSheet.Cells(20,1).Value="20"

    [/autoit][autoit][/autoit][autoit][/autoit][autoit]

    For $cell in .ActiveSheet.Range("A1:J500")
    If $cell.Value = $AN Then
    $cell.Select
    $cell = .ActiveCell.Address
    $spl = StringSplit($cell, "$")
    $jbnr = .ActiveWorkBook.ActiveSheet.Cells($spl[3],1).Value
    FileCopy("C:\test\" & $jbnr & "*.job", "D:\mydir\", 9)
    Endif
    Next
    sleep(4000) ;See the results for 4 seconds
    .ActiveWorkBook.Saved = 1 ; Simulate a save of the Workbook
    .Quit ; Quit Excel
    EndWith

    [/autoit]

    Jetzt muss ich das ganze so umschreiben, das er keine neue Excel Datei erstellt sondern eine vorhandene öffnet und ausließt!

    Kann mir vielleicht jemand helfen???

    MfG
    Thomas

  • Hallo zemkedesign,

    habe ich schon probiert allerdings hat dann das auslesen nicht mehr funktioniert!!!

    MfG
    Thomas

    • Offizieller Beitrag

    Was genau willst Du denn auslesen?

    Eine XLS-Datei öffnen geht so:

    [autoit]


    $oExcel = ObjCreate('Excel.Application')
    If IsObj($oExcel) Then
    With $oExcel
    .Visible = 1
    WinWait('Microsoft Excel')
    .Workbooks.Open(@ScriptDir & '\test.xls')
    For $cell in .ActiveSheet.Range('A1:M1')
    MsgBox(0,0,$cell.Value)
    Next
    .Quit
    EndWith
    EndIf
    $oExcel = 0

    [/autoit]
  • hallo zusammen,

    also ich persönlich nehme immer die "ExcelCOM_UDF.au3" !!!

    scriptbeispiel wäre dann ....

    Spoiler anzeigen
    [autoit]


    #include <ExcelCOM_UDF.au3> ; Include the collection

    [/autoit] [autoit][/autoit] [autoit]

    DIM $sFilePath, $fVisible, $fReadOnly, $sPassword, $sWritePassword, $fSave, $fAlerts
    $sFilePath_01 = "C:\name.xls"
    ;---- workbook öffnen ----
    $oExcel_01 = _ExcelBookOpen($sFilePath_01, $fVisible = 1, $fReadOnly = False, $sPassword = "", $sWritePassword = "")
    $datum = _ExcelReadCell($oExcel_01, 4,1) ;liest zelle aus
    _ExcelBookClose($oExcel_01,0,0) ; schließt workbook

    [/autoit] [autoit][/autoit] [autoit]

    ;---------- zweites workbook öffnen ------------
    $sFilePath_02 = "C:\datei2.xls"
    $oExcel_02 = _ExcelBookOpen($sFilePath_02, $fVisible = 1)
    _ExcelSheetActivate($oExcel_02, "tab1") ; tabellenblatt "tab1" aktiv setzen
    _ExcelWriteCell($oExcel_02,$a01,32,3) ; schreib in zelle
    _ExcelBookClose($oExcel_02,1,0); schließt datei 2

    [/autoit] [autoit][/autoit] [autoit]

    ; ---- weitere beispiele ----

    [/autoit] [autoit][/autoit] [autoit]

    $oExcel_12 = _ExcelBookOpen($sFilePath_02, $fVisible = 1)

    [/autoit] [autoit][/autoit] [autoit]

    $var = _ExcelReadCell($oExcel_12, "F2") ;einzelne zelle lesen
    $var = _ExcelReadCell($oExcel_12, "C8:C24") ; zellenbereich lesen
    ;------------
    $sFilePath_13 = "C:\datei13.xls"
    $oExcel_13 = _ExcelBookOpen($sFilePath_02, $fVisible = 1)

    [/autoit] [autoit][/autoit] [autoit]

    _ExcelWriteCell($oExcel_13, $var, "L9:L25") ; zellenbereich schreiben
    ; oder z.b.
    _ExcelWriteCell($oExcel_13, $var, "L35:L51") ; zellenbereich schreiben

    [/autoit] [autoit][/autoit] [autoit]

    _ExcelBookClose($oExcel_12,1,0)
    _ExcelBookClose($oExcel_13,1,0)

    [/autoit] [autoit][/autoit] [autoit]

    ; immer daran denken, das file wieder zu schließen

    [/autoit] [autoit][/autoit] [autoit][/autoit]


    und so weiter ....
    die dateien müssen aber vorhanden sein !!!

    ;)

    gruß gmmg

    Einmal editiert, zuletzt von gmmg (17. Februar 2009 um 13:43)