Rubrik: Dateisystem · Dateien lesen/schreiben | VB-Versionen: VB6 | 04.06.08 |
Exceldatei im Format "TabStop-getrennt" einlesen Wenn sich eine Excel-Tabelle nicht via ADO-Zugriff öffnen lässt, weil sie im Format "TabStop-getrennt" vorliegt, bietet diese Funktion eine Alternative. | ||
Autor: Norbert Grimm | Bewertung: | Views: 12.149 |
ohne Homepage | System: Win9x, WinNT, Win2k, WinXP, Win7, Win8, Win10, Win11 | Beispielprojekt auf CD |
Mit nachfolgender Funktion lässt sich eine Excelo-Tabelle, die als TXT-Datei im Format "TabStop-getrennt" gespeichert ist, einlesen und bspw. im MS-FlexGrid-Control anzeigen.
' AutomatisierungsCode für Excel_Dateien ' Autor : Norbert Grimm ' ' Einlesen einer Excel_Datei, die im Format [TabStopp getrennt] vorliegt. ' ' Die Datei wird als Text_Datei geöffnet u. der Inhalt ' in ein MS(H]FlexGrid überführt. ' ' Parameter : ' xlPath : Dateipfad ' Flex : MS(H)FlexGrid ' ' Rückgabe : <>0, wenn Fehler Function Text_XLS(ByVal xlPath As String, ByRef Flex As Object) As Long Dim A As Integer Dim B As Integer Dim C As Integer Dim I As Integer Dim intFF As Integer Dim L As Integer Dim P As Integer Dim S As Integer Dim Ret As Long Dim vDaten As Variant Dim V As Variant Dim errMsg As String Dim sTZ As String ' Trennzeichen On Error GoTo Err_TXLS intFF = FreeFile Open xlPath For Input Shared As intFF ' lies ganze Zeile Line Input #intFF, vDaten A = A + 1 C = 0 S = 1 sTZ = vbTab ' Chr(9) ' Anzahl Spalten ermitteln Do P = InStr(S, vDaten, sTZ) If P Then L = P - S C = C + 1 S = P + 1 End If Loop Until P = 0 C = C + 2 V = vbTab & vDaten ' Anzahl Spalten festlegen u. 1.AddItem With Flex .Redraw = False .Cols = C .AddItem V, A End With ' Daten_Schleife Do While Not EOF(intFF) Line Input #intFF, vDaten A = A + 1 V = vbTab & vDaten With Flex .AddItem V, A ' A, [Index] optional End With Loop Close intFF Flex.Redraw = True Exit_TXLS: Text_XLS = Ret Exit Function Err_TXLS: With Err Ret = .Number errMsg = .Description .Clear End With MsgBox Ret & vbCr & errMsg, vbCritical, "Text_XLS" Resume Exit_TXLS End Function
Aufrufbeispiel:
MSFlexGrid1.Clear MSFlexGrid1.Rows = 1 Text_XLS "D:\Mappe1.txt", MSFlexGrid1