- Offizieller Beitrag
Hi,
vielleicht kennt ihr das - ihr habt Daten, die aus einer Textdatei/Datenbank in eine Exceltabelle übertragen werden sollen. Wenn in den Daten z.B. Postleitzahlen oder Telefonnummern enthalten sind, muß ich vor dem Übertragen der Daten die Tabelle formatieren, da sonst die Vornullen weg sind. Früher hatte ich das mit einem Skript für jeden Anwendungsbereich extra gelöst. Jetzt löse ich das, wie folgt:
AutoIt
#include <Excel.au3>
; Bsp: CSV-Datei
#cs
KDNR;NAME;VNAME;STRASSE_NR;PLZ;ORT;TEL;EMAIL;REGISTERDATUM
10123;Mustermann;Max;Mustergasse 123;08150;Musterhausen;089123456789;max.mustermann@mustermann.de;18.01.2017
#ce
; erste Datenzeile einlesen (simuliert: _FileReadToArray($sPathFile, $aFile))
Local $aFile[] = [3, _
"KDNR;NAME;VNAME;STRASSE_NR;PLZ;ORT;TEL;EMAIL;REGISTERDATUM", _
"10123;Mustermann;Max;Mustergasse 123;08150;Musterhausen;089123456789;max.mustermann@mustermann.de;18.01.2017", _
"10124;Musterfrau;Maria;Mustergässle 456;56789;Musterdorf;099987654321;maria.musterfrau@musterfrau.de;21.02.2017"]
; erste Datenzeile splitten
Local $aLine = StringSplit($aFile[2], ';')
; Exceltabelle erstellen und vor Befüllen mit Daten die Spalten formatieren
Local $oExcel = _Excel_Open()
Local $oWorkbook = _Excel_BookNew($oExcel, 1)
Local $oSheet = $oWorkbook.Worksheets(1)
For $i = 1 To $aLine[0]
$oSheet.Columns($i).NumberFormat = _GetExcelFormatByData($aLine[$i])
Next
; Exceltabelle füllen
For $i = 1 To $aFile[0]
$aLine = StringSplit($aFile[$i], ';', 2)
For $j = 0 To UBound($aLine) -1
$oSheet.Range("A1").Offset($i-1, $j).Value = $aLine[$j]
ConsoleWrite('$i-1, $j: ' & $i-1 & ', ' & $j & @CRLF)
Next
Next
; #FUNCTION# ====================================================================================================================
; Name ..........: _GetExcelFormatByData
; Description ...: Gets the (german) Excel format string for a given data
; Syntax ........: _GetExcelFormatByData($sData)
; Parameters ....: $sData - Value for the detection of the format
; Return values .: The Excel format string
; Author ........: BugFix
; ===============================================================================================================================
Func _GetExcelFormatByData($sData)
$sData = String($sData)
Local $sPattDateHMS = '^\d{1,2}\.\d{1,2}\.\d{4} \d{1,2}:\d{1,2}:\d{1,2}$'
Local $sPattDateHM = '^\d{1,2}\.\d{1,2}\.\d{4} \d{1,2}:\d{1,2}$'
Local $sPattDate = '^\d{1,2}\.\d{1,2}\.\d{4}$'
Local $sPattFloat = '^\d+,\d+$'
Local $sPattZero = '^0$' ; alleinstehende Null als Integer nicht als String, wie führende Null in PLZ od. TelefonNr
Local $sPattInteger = '^[1-9]\d*$'
Select
Case StringRegExp($sData, $sPattDateHMS)
Return 'TT.MM.JJJJ hh:mm:ss'
Case StringRegExp($sData, $sPattDateHM)
Return 'TT.MM.JJJJ hh:mm'
Case StringRegExp($sData, $sPattDate)
Return 'TT.MM.JJJJ'
Case StringRegExp($sData, $sPattFloat)
Return '#0,#'
Case StringRegExp($sData, $sPattZero) Or StringRegExp($sData, $sPattInteger)
Return '#'
Case Else
Return '@'
EndSelect
EndFunc ;==>_GetExcelFormatByData
Alles anzeigen