Exel Daten (.xls / .xlsx) in .txt Daten umwandeln

    • Offizieller Beitrag

    Probier mal

    Spoiler anzeigen
    [autoit]

    ;EDIT 8.2.2010
    ;Bugs entfernt, Funktionen hinzugefügt
    ;Info auf http://wiki.services.openoffice.org/wiki/Spreadsheet_common
    ;
    ;Example & Functions

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

    #include <Array.au3>
    AutoItSetOption("WinTitleMatchMode", 2)
    $oMyError = ObjEvent("AutoIt.Error", "_OOErrFunc") ; Implementiert einen eigenen Error-Handler

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

    Dim $OpenPar[3];,$cellkoord[2]

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

    Global $setOOoProp, $oSheet, $odoc, $oDesk, $osm, $oCell, $errormodul
    Global $cellkoord[2]

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

    $summe = 0
    $file = @ScriptDir & "\testcalc1"

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

    ;*****************************************************
    ;*************************DEMO START******************
    ;*****************************************************

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

    MsgBox(0, "Demo OpenOffice CALC-Functions", "Starting OpenOffice...", 2)
    _OOInit() ;verbindung zu openoffice

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

    MsgBox(0, "Demo OpenOffice CALC-Functions", "Create a new Book", 2)
    _OOAddNewBook() ;testdatei erstellen

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

    winwait("OpenOffice")
    ;WinSetState("OpenOffice","",@SW_MAXIMIZE)
    WinActivate("OpenOffice", "")

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

    if WinActive("OpenOffice", "")=0 then ;sometimes the OO-window is not activated automatically ...
    msgbox(262144, "Demo OpenOffice CALC-Functions", _
    "AutoIt can´t activate the OpenOffice-Window!" & @CRLF & _
    "Please activate the OpenOffice-Window and press OK")
    endif
    If WinActive("OpenOffice", "")=0 then exit

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

    MsgBox(0, "OpenOffice Calc Demo", "Write some Text and Data into the sheet...", 4)

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

    _OOSheetSetCell(0, 0, "Tabellentest") ;man kann die Zellen entweder mit Reihe und Spalte ansprechen...
    _OOSheetSetCellColor(0, 0, 0x0000FF, 0x00FF00) ;Vordergrundfarbe und Hintergrundfarbe in 0xRRGGBB
    _OOSheetSetCell("A2", 0, "===================") ;...oder mit direkter Adresse
    _OOSheetSetCellColor("A2", 0, 0x000000, 0xFF0000)
    For $col = 1 To 5 ;testdaten auf 1. Tabelle anlegen
    For $row = 2 To 4
    _OOSheetSetCell($col, $row, Int(Random(10, 1000)), "value") ;diesmal werdem zahlen geschrieben
    Next
    Next

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

    MsgBox(262144, "OpenOffice Calc Demo", "Transfer some Data from an AutoIt-Array to the sheet...please close the following window to continue the demo", 4)
    Dim $array[3][2] = [[11, 12],[13, 14],["TEST", 15]] ;ein zweidimensionales array erstellen
    _ArrayDisplay($array,"Testarray (pls close by hand)")
    _OOSheetArrayToRange(0, "B7", $array) ;schreibt das Array an die Position der Zelle

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

    MsgBox(262144, "OpenOffice Calc Demo", "Transfer some Data to an AutoIt-Array...please close the following windows to continue the demo", 4)
    $array = _OOSheetRangetoArray(0, "c3", "h5", "")
    _ArrayDisplay($array, "Array with data C3:H5 (pls close by hand)")
    $array = _OOSheetRangetoArray(0, "A1", "A1", "ALL")
    _ArrayDisplay($array, "Array with all data (pls close by hand)")

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

    MsgBox(262144, "OpenOffice Calc Demo", "Add a new sheet but the Focus is still on the actual sheet...", 5)

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

    _OOBookAddNewSheet("NeueTabelle") ;neues Tabellenblatt erstellen
    _OOSheetActivate("NeueTabelle", 0) ;die folgenden Daten werden ins neue Tabellenblatt geschrieben, der Fokus ist aber immer noch bei Tabelle1
    _OOSheetActivate(3, 0) ;macht genau dasselbe, entweder die Tabellennummer oder den Namen angeben

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

    MsgBox(262144, "OpenOffice Calc Demo", "Copy some content from the actual sheet to the new sheet...", 5)

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

    _OOSheetMoveOrCopyRange(0, "B3:c6", "NeueTabelle", "C2", 1) ;Bereiche werden entweder kopiert oder verschoben
    _OOSheetSetCell(0, 0, "Gesamtsumme Daten aus " & _OONameOfActiveSheet())

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

    MsgBox(262144, "OpenOffice Calc Demo", "Activate the new sheet...", 4)

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

    _OOSheetActivate(3, 1) ;auf diese Tabelle wechseln

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

    MsgBox(262144, "OpenOffice Calc Demo", "Searching for some numbers, color these cells, and add them with a formula...", 6)

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

    For $col = 2 To 3 ;Tabellenbereich nach zahlen durchsuchen...
    For $row = 1 To 3
    If _OOSheetGetCellType($col, $row) = "VALUE" Then ;..und wenn gefunden
    $summe = $summe + _OOSheetGetCellValue($col, $row) ;..zusammenzählen
    _OOSheetSetCellColor($col, $row, 0x0000FF, 0x00FF00) ;Vordergrundfarbe und Hintergrundfarbe in 0xRRGGBB
    EndIf
    Next
    Next

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

    _OOSheetSetCell(2, 6, "Summe aller Zahlen:", "string") ;string in Tabelle schreiben
    _OOSheetSetCell(3, 6, $summe, "value") ;wert in Tabelle schreiben
    _OOSheetSetCell(2, 8, "SUM(D2:D4)--->", "string") ;string
    _OOSheetSetCell(3, 8, "=SUM(D2:D4)", "formula") ;oder Formel schreiben

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

    MsgBox(262144, "OpenOffice Calc Demo", "set width of first column to optimum and second column to 10mm...", 6)
    _OOSheetSetcolProperties(0, 5000, True, True, True) ;erste spalte verbreitern optimum
    _OOSheetSetRowColor(0, 0xFF00F0, 0xF0FFF0)
    _OOSheetSetcolProperties(1, 1000, False, True, False) ;zweite spalte verbreitern
    _OOSheetSetcolProperties(2, 5000, True, True, False) ;dritte spalte verbreitern optimum

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

    MsgBox(262144, "OpenOffice Calc Demo", "Delete 8th row ...", 4)
    _OOSheetdeleteRow(7, 1)
    MsgBox(262144, "OpenOffice Calc Demo", "Insert 3 rows ...", 4)
    _OOSheetinsertRow(4, 3)

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

    MsgBox(262144, "OpenOffice Calc Demo Store", "Save the Book in:" & @CRLF & ".ods OOffice-Format" & @CRLF & ".xls MS Excel 97" & @CRLF & ".pdf calc_pdf_Export" & @CRLF & ".txt Text - txt - csv (StarCalc)" & @CRLF & ".html HTML (StarCalc)", 5)

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

    ;speichern in verschiedenen Formaten
    _OOStoreBook($file & ".ods") ;speichern im OOcalc-format
    _OOStoreBook($file & ".xls", "MS Excel 97")
    _OOStoreBook($file & ".pdf", "calc_pdf_Export")
    _OOStoreBook($file & ".txt", "Text - txt - csv (StarCalc)")
    _OOStoreBook($file & ".html", "HTML (StarCalc)")
    _OOCloseBook()

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

    MsgBox(262144, "OpenOffice Calc Demo", "Show the stored files...", 5)
    ShellExecute($file & ".pdf")
    MsgBox(262144, "OpenOffice Calc Demo PDF", "Press OK")
    ShellExecute($file & ".txt")
    MsgBox(262144, "OpenOffice Calc Demo TXT", "Press OK")
    ShellExecute($file & ".html")
    MsgBox(262144, "OpenOffice Calc Demo HTML", "Press OK")

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

    Exit
    ;*****************************************************
    ;********************DEMO END*************************
    ;*****************************************************

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

    ;Functions

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

    Func _OOSetProp($cName, $uValue) ;Eigenschaften in struct übergeben
    $errormodul = "_OOSetProp"
    $osm = ObjCreate("com.sun.star.ServiceManager")
    $oPropertyValue = $osm.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
    $oPropertyValue.Name = $cName
    $oPropertyValue.Value = $uValue
    $setOOoProp = $oPropertyValue
    Return $setOOoProp
    EndFunc ;==>_OOSetProp

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

    Func _OOInit($pass = "", $readonly = False, $hidden = False) ;verbindung zu OO herstellen
    $errormodul = "_OOINIT"
    $osm = ObjCreate("com.sun.star.ServiceManager")
    $oDesk = $osm.createInstance("com.sun.star.frame.Desktop")
    $OpenPar[0] = _OOSetProp("ReadOnly", $readonly)
    $OpenPar[1] = _OOsetProp("Password", $pass) ;setzt das passwort des dokuments
    $OpenPar[2] = _OOsetProp("Hidden", $hidden)
    EndFunc ;==>_OOInit

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

    Func _OOAddNewBook() ; neue Tabellendatei erstellen
    $errormodul = "_OOAddNewBook"
    $odoc = $oDesk.loadComponentFromURL("private:factory/scalc", "_blank", 0, $OpenPar)
    $oSheet = $odoc.CurrentController.ActiveSheet
    EndFunc ;==>_OOAddNewBook

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

    Func _OOOpenFile($fname) ; bestehende *.Datei öffnen, OO importiert mit Filter wenn möglich
    $errormodul = "_OOOpenFile"
    $fname = StringReplace($fname, ":", "|")
    $fname = StringReplace($fname, " ", "%20")
    $fname = "file:///" & StringReplace($fname, "\", "/")
    $odoc = $oDesk.loadComponentFromURL($fname, "_blank", 0, $OpenPar)
    EndFunc ;==>_OOOpenFile

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

    Func _OOOpenBook($fname) ; bestehende Tabellendatei öffnen
    $errormodul = "_OOOpenBook"
    $fname = StringReplace($fname, ":", "|")
    $fname = StringReplace($fname, " ", "%20")
    $fname = "file:///" & StringReplace($fname, "\", "/")
    $odoc = $oDesk.loadComponentFromURL($fname, "_blank", 0, $OpenPar)
    $oSheet = $odoc.CurrentController.ActiveSheet ;auskommentieren, um alle importierbaren Formate zu öffnen
    EndFunc ;==>_OOOpenBook

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

    Func _OOStoreBook($fname, $filter = "StarOffice XML (Calc)") ;speichert Datei , angewandt wird der Dateifilter
    ;Alle EXPORT-Filter von dieser Page sind anwendbar, es gelten die internen Namen!
    ;http://wiki.services.openoffice.org/wiki/Framework…FilterList_SO_7
    $errormodul = "_OOStoreBook"
    $fname = StringReplace($fname, ":", "|")
    $fname = StringReplace($fname, " ", "%20")
    $fname = "file:///" & StringReplace($fname, "\", "/")
    ;MsgBox(0, "File saved", $filter)
    $oSave = _ArrayCreate(_OOsetProp("FilterName", $filter))
    $odoc.storetourl($fname, $oSave)
    EndFunc ;==>_OOStoreBook

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

    Func _OOSheetActivate($sheetnameornumber, $activeflag = 1) ;Tabellenname oder nummer zum schreiben/lesen aktivieren
    ;mit $activeflag=1 kommt die Tabelle in den Vordergrund
    ;mit $activeflag=0 wird die Tabelle verdeckt aktiviert z.B.um im Hintergrund Daten zu ändern
    $errormodul = "_OOSheetactivate"
    If IsString($sheetnameornumber) Then
    $activesheet = $odoc.sheets.getbyname($sheetnameornumber)
    Else
    $activesheet = $odoc.sheets.getbyindex($sheetnameornumber) ;index starts with 0
    EndIf

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

    If $activeflag = 1 Then
    $oSheet = $odoc.CurrentController.setActiveSheet($activesheet)
    EndIf
    $oSheet = $activesheet
    EndFunc ;==>_OOSheetActivate

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

    Func _OOBookAddNewSheet($sheetname) ;erstellt neues Tabellenblatt mit dem Namen $sheetname
    $errormodul = "_OOAddNewsheet"
    $eSheets = $odoc.getSheets.createEnumeration
    $flag = 0
    While $eSheets.hasMoreElements ;abfrage, ob sheet schon existiert
    $oElement = $eSheets.nextElement()
    If $oElement = $sheetname Then $flag = 1 ;wenn ja, merken
    WEnd
    If $flag = 0 Then
    $Inst = $odoc.createInstance("com.sun.star.sheet.Spreadsheet")
    $odoc.Sheets.insertByName($sheetname, $Inst)
    EndIf
    EndFunc ;==>_OOBookAddNewSheet

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

    Func _OOSheetGetCellValue($col, $row) ;Inhalt der Zelle bsp; "A3" oder F22
    $errormodul = "_OOSheetGetCellValue"
    If IsString($col) Then
    _OOAdress2Koord($col)
    $col = $cellkoord[0]
    $row = $cellkoord[1]
    EndIf
    $oCell = $oSheet.getCellByPosition($col, $row).value
    Return $oCell
    EndFunc ;==>_OOSheetGetCellValue

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

    Func _OONumberOfSheets() ; Anzahl der Tabellenblätter
    $errormodul = "_OONumberofsheets"
    Return $odoc.getsheets.getcount
    EndFunc ;==>_OONumberOfSheets

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

    Func _OOAdress2Koord($cellname) ;wandelt "C1" in $cellkoord[0]=2 und $cellkoord[1]=0
    $errormodul = "_OOAdress2Koord"
    Local $textchar[3]
    Local $numchar[3]
    $cellname = StringUpper($cellname)
    $numchar = StringRegExp($cellname, '\d+', 1) ;y-Koordinate der Zelle, findet Zahlen im Zellennamen;
    ; msgbox (0,$cellname,$textchar[0]&" "&$numchar[0])
    $cellkoord[1] = $numchar[0] - 1
    $textchar = StringRegExp($cellname, '[[:alpha:]]{0,2}', 1) ;findet A oder AA im Zellennamen
    $x = (Asc(StringMid($textchar[0], 1, 1)) - 65) ;ascii erster Buchstabe
    If StringLen($textchar[0]) = 2 Then
    $x = (($x + 1) * 26) + (Asc(StringMid($textchar[0], 2, 1)) - 65)
    EndIf
    $cellkoord[0] = $x
    Return $cellkoord
    EndFunc ;==>_OOAdress2Koord

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

    Func _OOKoordToAddress($row,$col) ;wandelt 2,3 in "C4"
    $x = Int($col / 26)
    $y = Mod($col, 26)

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

    If $x = 0 Then
    $char = Chr(64 + $y) ;A B C
    Else
    $char = Chr(64 + $x) & Chr(65 + $y) ; AA AB AC...
    EndIf
    Return $char & String($row + 1)
    EndFunc ;==>_OOKoordToAddress

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

    Func _OONameOfActiveSheet()
    $errormodul = "_OONameofAvtiveSheet"
    Return $oSheet.name
    EndFunc ;==>_OONameOfActiveSheet

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

    Func _OOSheetSetRowProperties($row, $height, $optheight = Default, $visible = Default, $newpage = Default) ;Eigenschaften der Zeile
    ; $optheight, $visible $newpage all OO_true or OO_False
    $errormodul = "_OOSheetSetRowProperties"
    $orow = $oSheet.getRows().getByIndex($row)
    If IsNumber($height) Then $orow.Height = $height ;column height (in 100ths of mm)
    $orow.OptimalWidth = Number($optheight)
    $orow.IsVisible = Number($visible)
    $orow.IsStartOfNewPage = Number($newpage)
    EndFunc ;==>_OOSheetSetRowProperties

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

    Func _OOSheetSetColProperties($col, $width, $optwidth = Default, $visible = Default, $newpage = Default) ;Eigenschaften Spalte
    ; $opt, $visible $newpage all OO_true or OO_False
    $errormodul = "_OOSheetSetColProperties"
    If IsString($col) Then
    $ocol = $oSheet.getColumns().getByName($col)
    Else
    $ocol = $oSheet.getColumns().getByIndex($col)
    EndIf
    $ocol.Width = $width ;column width (in 100ths of mm)
    $ocol.OptimalWidth = Number($optwidth)
    $ocol.IsVisible = Number($visible)
    $ocol.IsStartOfNewPage = Number($newpage)
    EndFunc ;==>_OOSheetSetColProperties

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

    Func _OOSheetdeleteRow($startrow, $numberofrows = 1) ;Zeile löschen
    $errormodul = "_OOSheetDeleteRow"
    $oSheet.getrows.removeByIndex($startrow, $numberofrows)
    EndFunc ;==>_OOSheetdeleteRow

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

    Func _OOSheetinsertRow($startrow, $numberofrows = 1) ;Zeile einfügen
    $errormodul = "_OOSheetInsertRow"
    $oSheet.getrows.insertbyindex($startrow, $numberofrows)
    EndFunc ;==>_OOSheetinsertRow

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

    Func _OOSheetdeleteCol($startcol, $numberofcols = 1) ;Spalte löschen
    $errormodul = "_OOSheetDeleteCol"
    $oSheet.getcolumns.removeByIndex($startcol, $numberofcols)
    EndFunc ;==>_OOSheetdeleteCol

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

    Func _OOSheetinsertCol($startcol, $numberofcols = 1) ;spalte einfügen
    $errormodul = "_OOSheetInsertCol"
    $oSheet.getcolumns.insertbyindex($startcol, $numberofcols)
    EndFunc ;==>_OOSheetinsertCol

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

    Func _OOSheetGetCellType($row, $col) ;Rückgabe: Zellentyp: value, string, formula, empty
    $errormodul = "_OOSheetGetCellType"
    If IsString($row) Then
    $cell = _OOAdress2Koord($row)
    $row = $cell[0]
    $col = $cell[1]
    EndIf
    $oCell = $oSheet.getcellbyposition($row, $col)
    Select
    Case $oCell.type = 1
    $celltype = "VALUE"
    Case $oCell.type = 2
    $celltype = "STRING"
    Case $oCell.type = 3
    $celltype = "FORMULA"
    Case $oCell.type = 0
    $celltype = "EMPTY"
    Case Else
    $celltype = "UNKNOWN"
    EndSelect
    Return $celltype
    EndFunc ;==>_OOSheetGetCellType

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

    Func _OOSheetMoveOrCopyRange($fromsheetnameornumber, $fromrange, $tosheetnameornumber, $tocell, $flag) ;$fromrange="B2:c4" $tocell="F4" flag: 0=move, 1=copy
    $errormodul = "_OOSheetMoveOrCopyRange"
    If IsString($fromsheetnameornumber) Then
    $fromsheet = $odoc.sheets.getbyname($fromsheetnameornumber)
    Else
    $fromsheet = $odoc.sheets.getbyindex($fromsheetnameornumber) ;index starts with 0
    EndIf
    If IsString($tosheetnameornumber) Then
    $tosheet = $odoc.sheets.getbyname($tosheetnameornumber)
    Else
    $tosheet = $odoc.sheets.getbyindex($tosheetnameornumber) ;index starts with 0
    EndIf
    $oRangeOrg = $fromsheet.getCellRangeByName($fromrange).RangeAddress ; copy range
    $oCellCpy = $tosheet.getCellrangeByname($tocell).CellAddress ; insert position
    If $flag = 0 Then ;move
    $oSheet.MoveRange($oCellCpy, $oRangeOrg)
    Else
    $oSheet.CopyRange($oCellCpy, $oRangeOrg)
    EndIf
    EndFunc ;==>_OOSheetMoveOrCopyRange

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

    Func _OOSheetSetCell($xkoord, $ykoord, $data, $ref = "string") ;$ref= angabe, ob "string" "value" "formula"
    $errormodul = "_OOSheetSetCell"
    If IsString($xkoord) Then
    $cell = _OOAdress2Koord($xkoord)
    $xkoord = $cell[0]
    $ykoord = $cell[1]

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

    EndIf
    $ref = StringUpper($ref)
    Select
    Case $ref = "STRING"
    $oCell = $oSheet.getCellByPosition($xkoord, $ykoord)
    $oCell.setString($data)

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

    Case $ref = "VALUE"
    $oCell = $oSheet.getCellByPosition($xkoord, $ykoord)
    $oCell.setvalue($data)

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

    Case $ref = "FORMULA"
    $oCell = $oSheet.getCellByPosition($xkoord, $ykoord)
    $oCell.setformula($data)

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

    Case Else
    $oCell = $oSheet.getCellByPosition($xkoord, $ykoord)
    $oCell.setstring($data)
    EndSelect
    EndFunc ;==>_OOSheetSetCell

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

    Func _OOCloseBook() ;Datei beenden
    $errormodul = "_OOCloseBook"
    $odoc.close(True)
    EndFunc ;==>_OOCloseBook

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

    Func _OOSheetSetCellColor($col, $row, $front = Default, $back = Default) ;RRGGBB
    $errormodul = "_OOSheetSetCell"
    If IsString($col) Then
    $cell = _OOAdress2Koord($col)
    $col = $cell[0]
    $row = $cell[1]
    EndIf
    $oCell = $oSheet.getCellByPosition($col, $row)
    $oCell.CharColor = $front
    $oCell.CellBackColor = $back
    EndFunc ;==>_OOSheetSetCellColor

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

    Func _OOSheetSetRowColor($row, $front = Default, $back = Default) ;RRGGBB
    $errormodul = "_OOSheetSetRowColor"
    $orow = $oSheet.getRows().getByIndex($row)
    $orow.CharColor = $front
    $orow.CellBackColor = $back
    EndFunc ;==>_OOSheetSetRowColor

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

    Func _OOSheetSetColColor($col, $front = Default, $back = Default) ;RRGGBB
    $errormodul = "_OOSheetSetColColor"
    $ocol = $oSheet.getColumns().getByIndex($col)
    $ocol.CharColor = $front
    $ocol.CellBackColor = $back
    EndFunc ;==>_OOSheetSetColColor

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

    Func _OOSheetRangeToArray($sheetnameornumber, $startcell, $endcell, $all = "ALL") ;gibt ein Array der Daten aus dem Bereich des Tabellenblatts
    $errormodul = "_OOSheetRangeToArray"
    If IsString($sheetnameornumber) Then
    $oSheet = $odoc.sheets.getbyname($sheetnameornumber)
    Else
    $oSheet = $odoc.sheets.getbyindex($sheetnameornumber) ;index starts with 0
    EndIf

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

    If StringUpper($all) = "ALL" Then ;used data of the whole sheet
    $oCursor = $oSheet.createCursor()
    $oCursor.GotoStartOfUsedArea(0) ;von der ersten ausgefüllten Zelle
    $start = $oCursor.getrangeaddress()
    $Start_row = $start.startRow
    $Start_col = $start.startColumn
    $oCursor.GotoEndOfUsedArea(1);bis zur letzten ausgefüllten Zelle
    $end = $oCursor.getrangeaddress()
    $end_row = $end.endRow
    $end_col = $end.endColumn
    Else
    If IsString($startcell) Then
    $cell = _OOAdress2Koord($startcell)
    $Start_row = $cell[1]
    $Start_col = $cell[0]
    EndIf
    If IsString($endcell) Then
    $cell = _OOAdress2Koord($endcell)
    $end_row = $cell[1]
    $end_col = $cell[0]
    EndIf
    EndIf

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

    $ooarray = $oSheet.getCellRangeByPosition($Start_col, $Start_row, $end_col, $end_row).getDataArray() ;verschachteltes array, in [0] ist die erste zeile, in [2] die 2. usw

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

    Dim $array[UBound($ooarray)][UBound($ooarray[0])]
    For $rows = 0 To UBound($ooarray) - 1
    $row = $ooarray[$rows]
    For $cols = 0 To UBound($row) - 1
    $array[$rows][$cols] = $row[$cols]
    Next
    Next
    Return $array
    ;ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $GetUsedRangeAddress = ' & $GetUsedRangeAddress & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
    ;~ ; return $Range
    EndFunc ;==>_OOSheetRangeToArray

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

    Func _OOSheetArrayToRange($sheetnameornumber, $cellname, $array) ;Array ins Tabellenblatt am Position cellname ("B4")
    $errormodul = "_OOSheetArrayToRange"
    If IsString($sheetnameornumber) Then
    $oSheet = $odoc.sheets.getbyname($sheetnameornumber) ;"Tabelle1"
    Else
    $oSheet = $odoc.sheets.getbyindex($sheetnameornumber) ;index starts with 0
    EndIf
    If IsString($cellname) Then
    $cell = _OOAdress2Koord($cellname)
    $row = $cell[1]
    $col = $cell[0]
    Else
    return seterror(1,0,0)
    EndIf
    $rangestring = $cellname & ":" & _OOKoordToAddress($row + UBound($array, 1) - 1, $col + UBound($array, 2)) ;calculate the cellname of the last arrayitem in the sheet
    ;build ooArray
    $dimensions = UBound($array, 0)
    If $dimensions > 2 Then Return SetError(1, 0, 0) ;nur maximal 2-dimensionale arrays
    If $dimensions = 1 Then ;eine zeile im Array wird zu einer spalte im sheet!!!
    Dim $ooarray[1]
    $ooarray[0] = $array
    Else
    Dim $ooarray[UBound($array, 1)] ;anzahl der Zeilen
    Dim $arows[UBound($array, 2)] ;länge der zeilen
    For $row = 0 To UBound($array, 1) - 1 ;alle Zeilen abarbeiten
    For $col = 0 To UBound($array, 2) - 1 ;alle Spalteneinträge in dieser Zeile
    $arows[$col] = $array[$row][$col] ;die Spalteneinträge als Reihe ins array
    Next
    $ooarray[$row] = $arows ;das array der gesamten Zeile in das ooarray an index $row
    Next
    EndIf
    $oRange = $oSheet.getCellRangeByName($rangestring)
    $oData = $oRange.setDataArray($ooarray)
    EndFunc ;==>_OOSheetArrayToRange

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

    ; Das ist unser eigener Error-Handler
    Func _OOErrFunc() ;COM-Error-Handler
    $HexNumber = Hex($oMyError.number, 8)
    MsgBox(0, "COM-Error OpenOffice Script", "Ein COM-Fehler wurde abgefangen!" & @CRLF & _
    "Fehlernummer: " & $HexNumber & @CRLF & _
    "WinDescription: " & $oMyError.windescription & @CRLF & _
    "Error in Modul " & $errormodul)
    SetError(1)
    EndFunc ;==>_OOErrFunc

    [/autoit]
  • Zuerst ne Blöde Frage von mir. Ist es Richtig wenn ich daraus eine au3 Datei erstelle und diese dann in den Include Ordner kopiere. Den Teil mit den Beispielen habe ich rausgelöscht. Die datei habe ich OpenOffice genannt und so als include genommen. Hier mein jetziger Script:

    Spoiler anzeigen
    [autoit]

    #include <OpenOffice.au3>
    #include <File.au3>
    #include <Array.au3>
    Local $folder = "C:\Users\dacohelpdesk\Desktop\test\Input"
    Local $FileList = _FileListToArray($folder, '*.xls', 1)
    For $i = 1 To UBound($FileList) - 1
    $oOO = _OOOpenBook($folder & '\' & $FileList[$i], 0)
    If StringRight($FileList[$i],3) = "xls" Then
    _OOStoreBook($oOO, "C:\Users\dacohelpdesk\Desktop\test\Output" & '\' & StringTrimRight($FileList[$i], 4), "txt") , 0, 1, "ReadOnly") ; für xls

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

    Else
    _OOStoreBook($oOO, "C:\Users\dacohelpdesk\Desktop\test\Output" & '\' & StringTrimRight($FileList[$i], 5), "txt") , 0, 1, "ReadOnly") ; für xlsx

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

    EndIf
    _OOBookClose($oOO, 1, 0)
    Next

    [/autoit]

    Lg Ru642

    Einmal editiert, zuletzt von ru642 (11. Januar 2013 um 13:44)

  • Habs mir auch nur zum testen gezogen. Es wäre wirklich genial wenn du es dir zu testzwecken ziehen würdes fänd ich extrem cool. Wenn du das vielleicht auch schnell testen kannst.
    Dann hätt ich wenigstens jemand der sich n bissl auskennt :thumbup:
    Lg Ru642

    Edit: Du musst natürlich nicht :D . Nun möchte ich aber noch schnell das mit dem Filemove anschauen. Wenn ich das so einfüge geht das schon aber nicht so wie ich möchte. Es sollte eine Abfrage geben ob die Excel Datei zum txt file geändert und abgespeichert wurde und erst dann soll es das ganze verschieben in Archiv.

    Spoiler anzeigen
    [autoit]

    #include <Excel.au3>
    #include <File.au3>
    #include <Array.au3>
    Local $folder = "C:\Users\dacohelpdesk\Desktop\test\Input"
    Local $FileList = _FileListToArray($folder, '*.xls', 1)
    For $i = 1 To UBound($FileList) - 1
    $oExcel = _ExcelBookOpen($folder & '\' & $FileList[$i], 0)
    If StringRight($FileList[$i],3) = "xls" Then
    _ExcelBookSaveAs($oExcel, "C:\Users\dacohelpdesk\Desktop\test\Output" & '\' & StringTrimRight($FileList[$i], 4), "txt")
    FileMove ($folder & '\' & "*.xls", "C:\Users\dacohelpdesk\Desktop\test\Archiv")
    Else
    _ExcelBookSaveAs($oExcel, "C:\Users\dacohelpdesk\Desktop\test\Output" & '\' & StringTrimRight($FileList[$i], 5), "txt") ;, 0, 1, "ReadOnly") ; für xlsx
    FileMove ($folder & '\' & "*.xlsx", "C:\Users\dacohelpdesk\Desktop\test\Archiv")
    EndIf
    _ExcelBookClose($oExcel, 1, 0)
    Next

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

    4 Mal editiert, zuletzt von ru642 (11. Januar 2013 um 16:59)

  • Meinst du vlt. so? :D

    Spoiler anzeigen
    [autoit]

    #include <Excel.au3>
    #include <File.au3>
    #include <Array.au3>
    Local $folder = "C:\Users\dacohelpdesk\Desktop\test\Input"
    Local $FileList = _FileListToArray($folder, '*.xls', 1)
    For $i = 1 To UBound($FileList) - 1
    $oExcel = _ExcelBookOpen($folder & '\' & $FileList[$i], 0)
    If StringRight($FileList[$i],3) = "xls" Then
    _ExcelBookSaveAs($oExcel, "C:\Users\dacohelpdesk\Desktop\test\Output" & '\' & StringTrimRight($FileList[$i], 4), "txt")
    If FileExists("C:\Users\dacohelpdesk\Desktop\test\Output" & '\' & $FileList[$i]) == 1 Then FileMove ($folder & '\' & "*.xls", "C:\Users\dacohelpdesk\Desktop\test\Archiv")
    Else
    _ExcelBookSaveAs($oExcel, "C:\Users\dacohelpdesk\Desktop\test\Output" & '\' & StringTrimRight($FileList[$i], 5), "txt") ;, 0, 1, "ReadOnly") ; für xlsx
    If FileExists("C:\Users\dacohelpdesk\Desktop\test\Output" & '\' & $FileList[$i]) == 1 Then FileMove ($folder & '\' & "*.xlsx", "C:\Users\dacohelpdesk\Desktop\test\Archiv")
    EndIf
    _ExcelBookClose($oExcel, 1, 0)
    Next

    [/autoit]
  • Es werden mit deinem Script keine Daten verschoben ins Archiv. Könnte daran liegen das $FileList auch schon ein Pfad hat.

    LG Ru642

    Spoiler anzeigen
    [autoit]

    #include <Excel.au3>
    #include <File.au3>
    #include <Array.au3>
    Local $folder = "C:\Users\dacohelpdesk\Desktop\test\Input"
    Local $FileList = _FileListToArray($folder, '*.xls', 1)
    For $i = 1 To UBound($FileList) - 1
    $oExcel = _ExcelBookOpen($folder & '\' & $FileList[$i], 0)
    If StringRight($FileList[$i],3) = "xls" Then
    _ExcelBookSaveAs($oExcel, "C:\Users\dacohelpdesk\Desktop\test\Output" & '\' & StringTrimRight($FileList[$i], 4), "txt")
    If FileExists("C:\Users\dacohelpdesk\Desktop\test\Output" & '\' & $FileList[$i]) == 1 Then FileMove ($folder & '\' & "*.xls", "C:\Users\dacohelpdesk\Desktop\test\Archiv") ; Stimmt das so? Bei $Filelist wird ja auch ein Pfad mitgegeben.
    Else
    _ExcelBookSaveAs($oExcel, "C:\Users\dacohelpdesk\Desktop\test\Output" & '\' & StringTrimRight($FileList[$i], 5), "txt") ;, 0, 1, "ReadOnly") ; für xlsx
    If FileExists("C:\Users\dacohelpdesk\Desktop\test\Output" & '\' & $FileList[$i]) == 1 Then FileMove ($folder & '\' & "*.xlsx", "C:\Users\dacohelpdesk\Desktop\test\Archiv")
    EndIf
    _ExcelBookClose($oExcel, 1, 0)
    Next

    [/autoit]
  • Nein der Script funktioniert noch nicht wie er sollte. Die Exel Daten werden nach dem erstellen der txt Datei nich verschoben. Ablauf wäre wie folgt:

    Exel Daten werden zu Datendateien verarbeitet.--> Kontrolle an Exel und Datendatei wenn gleicher Name vorhanden dann Exel ins Archiv verschieben.

    LG Ru642

  • Ich nutze jetzt doch Exel. Da ich es mit Open Office nicht hinkriege.

    Naja ne Exel Lizenz ist ja im Grunde keine herausgeworfenes Geld da man es ja trotzdem immer mal brauchen kann =D

    Kannst du dir den Script vielleicht mal anschauen. Könnte daran liegen das beim Check von $Filelist ein Problem auftritt. Hier der Script:

    Spoiler anzeigen
    [autoit]

    #include <Excel.au3>
    #include <File.au3>
    #include <Array.au3>
    Local $folder = "C:\Users\dacohelpdesk\Desktop\test\Input"
    Local $FileList = _FileListToArray($folder, '*.xls', 1)
    For $i = 1 To UBound($FileList) - 1
    $oExcel = _ExcelBookOpen($folder & '\' & $FileList[$i], 0)
    If StringRight($FileList[$i],3) = "xls" Then
    _ExcelBookSaveAs($oExcel, "C:\Users\dacohelpdesk\Desktop\test\Output" & '\' & StringTrimRight($FileList[$i], 4), "txt")
    If FileExists("C:\Users\dacohelpdesk\Desktop\test\Output" & '\' & $FileList[$i]) = 1 Then FileMove ($folder & '\' & "*.xls", "C:\Users\dacohelpdesk\Desktop\test\Archiv") ; Stimmt das so? Bei $Filelist wird ja auch ein Pfad mitgegeben.
    Else
    _ExcelBookSaveAs($oExcel, "C:\Users\dacohelpdesk\Desktop\test\Output" & '\' & StringTrimRight($FileList[$i], 5), "txt") ;, 0, 1, "ReadOnly") ; für xlsx
    If FileExists("C:\Users\dacohelpdesk\Desktop\test\Output" & '\' & $FileList[$i]) = 1 Then FileMove ($folder & '\' & "*.xlsx", "C:\Users\dacohelpdesk\Desktop\test\Archiv")
    EndIf
    _ExcelBookClose($oExcel, 1, 0)
    Next

    [/autoit]
    • Offizieller Beitrag

    Musst nur deine Pfade wieder einkommentieren und meine auskommentieren.

    Spoiler anzeigen
    [autoit]

    #include <Excel.au3>
    #include <File.au3>

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

    Opt('MustDeclareVars', 1)

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

    ;~ Local $loadPath_excel_format = "C:\Users\dacohelpdesk\Desktop\test\Input"
    Local $loadPath_excel_format = @ScriptDir
    ;~ Local $outputPath_txt_format = "C:\Users\dacohelpdesk\Desktop\test\Output"
    Local $outputPath_txt_format = @ScriptDir & "\Output"
    ;~ Local $archivPath_txt_format = "C:\Users\dacohelpdesk\Desktop\test\Archiv"
    Local $archivPath_txt_format = @ScriptDir & "\Archiv"

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

    Local $oExcel = 0

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

    Local $excelFile_A = _FileListToArray($loadPath_excel_format, '*.xls', 1)

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

    For $i = 1 To UBound($excelFile_A) - 1
    $oExcel = _ExcelBookOpen($loadPath_excel_format & '\' & $excelFile_A[$i], 0)
    If @error Then ConsoleWrite('_ExcelBookOpen : ' & @error & @LF)

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

    _ExcelBookSaveAs($oExcel, $outputPath_txt_format & '\' & StringLeft($excelFile_A[$i], StringInStr($excelFile_A[$i], '.', Default, -1) - 1), "txt")
    If @error Then ConsoleWrite('_ExcelBookSaveAs : ' & @error & @LF)

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

    If FileExists($outputPath_txt_format & '\' & StringLeft($excelFile_A[$i], StringInStr($excelFile_A[$i], '.', Default, -1) - 1) & '.txt') = 1 Then FileMove($loadPath_excel_format & '\' & "*.xls", $archivPath_txt_format)

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

    _ExcelBookClose($oExcel, 1, 0)
    If @error Then ConsoleWrite('_ExcelBookClose : ' & @error & @LF)
    Next

    [/autoit]
  • Mit deinem neuen Script wird gar nichts mehr erstellt geschweige denn verschoben. Es gibt keinen Fehler.
    Wärs villeicht möglich auf der Basis in der mein Script steht kleine Veränderungen zu machen die Grosses bewirken?

    • Offizieller Beitrag

    Muss doch gehen.

    Spoiler anzeigen
    [autoit]

    #include <Array.au3>
    #include <Excel.au3>
    #include <File.au3>

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

    Opt('MustDeclareVars', 1)

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

    ;~ Local $loadPath_excel_format = @ScriptDir
    ;~ Local $outputPath_txt_format = @ScriptDir & "\Output"
    ;~ Local $archivPath_txt_format = @ScriptDir & "\Archiv"

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

    Local $loadPath_excel_format = "C:\Users\dacohelpdesk\Desktop\test\Input"
    Local $outputPath_txt_format = "C:\Users\dacohelpdesk\Desktop\test\Output"
    Local $archivPath_txt_format = "C:\Users\dacohelpdesk\Desktop\test\Archiv"

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

    Local $oExcel = 0

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

    Local $excelFile_A = _FileListToArray($loadPath_excel_format, '*.xls', 1)
    If @error Then ConsoleWrite('_FileListToArray : ' & @error & @LF)

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

    ConsoleWrite('Dateien: ' & @CRLF & _ArrayToString($excelFile_A) & @LF)

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

    For $i = 1 To UBound($excelFile_A) - 1
    $oExcel = _ExcelBookOpen($loadPath_excel_format & '\' & $excelFile_A[$i], 0)
    If @error Then ConsoleWrite('_ExcelBookOpen : ' & @error & @LF)

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

    _ExcelBookSaveAs($oExcel, $outputPath_txt_format & '\' & StringLeft($excelFile_A[$i], StringInStr($excelFile_A[$i], '.', Default, -1) - 1), "txt")
    If @error Then ConsoleWrite('_ExcelBookSaveAs : ' & @error & @LF)

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

    If FileExists($outputPath_txt_format & '\' & StringLeft($excelFile_A[$i], StringInStr($excelFile_A[$i], '.', Default, -1) - 1) & '.txt') = 1 Then FileMove($loadPath_excel_format & '\' & "*.xls", $archivPath_txt_format)

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

    _ExcelBookClose($oExcel, 1, 0)
    If @error Then ConsoleWrite('_ExcelBookClose : ' & @error & @LF)
    Next

    [/autoit]

    Hast du wirklich die richtigen Pfade oben genommen?
    Was steht den unten in der Console drin?

  • Hab da ein kleines Probelm:

    >C:\program files\autoit3\autoit3.exe "C:\Users\dacohelpdesk\Desktop\test\Exel converter - Kopie.au3"
    Der Befehl "C:\program" ist entweder falsch geschrieben oder
    konnte nicht gefunden werden.
    >Exit code: 1 Time: 0.381

  • So habs neu draufgehauen. Hier die Commandoline:

    Code
    >"C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.exe" /run /prod /ErrorStdOut /in "C:\Users\dacohelpdesk\Desktop\test\Exel converter - Kopie.au3" /UserParams 
    +>14:28:10 Starting AutoIt3Wrapper v.2.1.0.33 Environment(Language:0407 Keyboard:00000807 OS:WIN_7/Service Pack 1 CPU:X64 OS:X64) 
    >Running AU3Check (1.54.22.0) from:C:\Program Files (x86)\AutoIt3 
    +>14:28:10 AU3Check ended.rc:0 
    >Running3.3.8.0):C:\Program Files (x86)\AutoIt3\autoit3.exe "C:\Users\dacohelpdesk\Desktop\test\Exel converter - Kopie.au3" 
    --> Press Ctrl+Alt+F5 to Restart or Ctrl+Break to Stop 
    Dateien: 
    1|Consolidated invoice 04.01.2013 Nr 563 ZURICH2.xls 
    +>14:28:11 AutoIT3.exe ended.rc:0 
    >Exit code: 0 Time: 3.070