在tbscript中将txt文件转换为excel

我正试图将文本文件转换为Excel表格。 这是格式的样子。

数据

我已经尝试编写一个脚本,但目前它所做的是覆盖我的当前文本文件添加我的列标题。 它不会添加我的文本文件中的任何数据。 任何人都可以帮助我了解我做错了什么。

Set objExcel = CreateObject("Excel.Application") objExcel.Visible = True strInput=InputBox("Enter name of File in C:\Users\spencerr\Desktop\MyProject\bin\") 'ask user for file name Set wb = objExcel.Workbooks.Open("C:\Users\bob\Desktop\MyProject\bin\" & strInput) 'Delete labels in log For i = 1 To 5 Set objRange = objExcel.Cells(1, i).EntireColumn objRange.Delete Next Set activeCell = objExcel.Cells(1, 2) Dim intVal Dim comVal Dim primeRow Dim largestRow Dim largestDec Dim row primeRow = 0 'filter out one measurement per second Do Until IsEmpty(activeCell) primeRow = primeRow + 1 'get base integer of first value by chopping off decimal intVal = Fix(activeCell.Value) comVal = intVal 'get all consecutive rows that have same base integer Do While intVal = comVal row = activeCell.Row Set activeCell = objExcel.Cells((row + 1), 2) comVal = Fix(activeCell.Value) Loop 'highest row number that contains the base integer largestRow = row 'delete all the rows up to the largest row j = primeRow Do While j < largestRow Set deleteRow = objExcel.Cells(primeRow, 2).EntireRow deleteRow.Delete j = j + 1 Loop 'compare the value right below the exact second and the value right above to see 'which is closer to the exact second Set activeCell = objExcel.Cells(primeRow, 2) largestDec = activeCell.Value Set activeCell = objExcel.Cells((primeRow + 1), 2) comVal = activeCell.Value if (((intVal + 1) - largestDec) > (comVal - (intVal + 1))) Then objExcel.Cells(primeRow, 2).EntireRow.Delete End If Loop 'round all the seconds that are left to the nearesr second Set activeCell = objExcel.Cells(1, 2) Do Until IsEmpty(ActiveCell) row = activeCell.row objExcel.Cells(row, 2) = Round(activeCell.Value) Set activeCell = objExcel.Cells(row + 1, 2) Loop 'add labels for KML conversion objExcel.Cells(1,1).EntireRow.Insert objExcel.Cells(1, 2).Value = "Description" objExcel.Cells(1, 3).Value = "Latitude" objExcel.Cells(1, 4). Value = "Longitude" wb.Save wb.Close objExcel.Quit 

我会使用正则expression式将数据转换为CSV格式:

 Set fso = CreateObject("Scripting.FileSystemObject") Set inFile = fso.OpenTextFile("C:\path\to\input.txt") Set outFile = fso.OpenTextFile("C:\path\to\output.csv", 2, True) Set re = New RegExp re.Pattern = "^week: (\d+) seconds: (\d+\.\d+) x: (\d+\.\d+) " & _ "y: (-\d+\.\d+) heading: (\d+)$" re.IgnoreCase = True outFile.WriteLine "Week,Seconds,X,Y,Heading" Do Until inFile.AtEndOfStream For Each m In re.Execute(inFile.ReadLine) outFile.WriteLine m.Submatches(0) & "," & m.Submatches(1) & "," & _ m.Submatches(2) & "," & m.Submatches(3) & "," & m.Submatches(4) Next Loop inFile.Close outFile.Close 

然后,您可以使用Excel打开CSV文件并将其保存为工作簿。

我会抛出另一个解决scheme。 只需使用Excel的TextToColumns()函数。 告诉它使用Space (第8个参数= True )作为分隔符,并将Treat consecutive delimiters as one (第四个参数= True )。

 Const xlDelimited = 1 Const xlDoubleQuote = 1 Set objExcel = CreateObject("Excel.Application") objExcel.Visible = True objExcel.Workbooks.Open "c:\path\to\text.txt" With objExcel.ActiveSheet .Columns("A:A").TextToColumns .Range("A1"), xlDelimited, xlDoubleQuote, True, , , , True End With 

或者,长的forms:

 With objExcel.ActiveSheet .Columns("A:A").TextToColumns _ .Range("A1"), _ ' Destination xlDelimited, _ ' Data Type xlDoubleQuote, _ ' Text Qualifier True, _ ' Consecutive Delimiters? , _ ' Use Tab for Delimiter? , _ ' Use Semicolon for Delimiter? , _ ' Use Comma for Delimiter? True ' Use Space for Delimiter? End With 

这将使您的数据进入适当的列。 然后只需删除“标签”列:

 .Range("A:A,C:C,E:E,G:G,I:I").Delete 

并保存为CSV

从评论,我认为可能会更容易跳过VBA,更改.txt文件扩展名为.csv。 然后,当你在Excel中打开这个文件时,你会得到一个包含所有数据的列。

在“数据”标签下,您会看到“文本到列”。 突出显示列A,select“Text to Columns”,然后select“Delimited”,然后点击“Next”,然后select“Space”。 您会看到数据将如何在其下分割的预览。 如果看起来不错,您可以点击“完成”,使用新的分割数据覆盖列A,或者点击“下一步”,select一个特定的单元格开始。

这应该让你很远,也许不完美,所以让我知道这是什么样子(或者如果你有任何问题)。

更快的方法:将文件转换为csv。 由于你的源是固定的宽度,最简单的方法就是将你需要的位复制到一个新的行。

 sep = ";" 'or , (depends on your language settings)' header = "week" & sep & "seconds" & sep line = "WEEK: 1799 SECONDS: 251731.358 X:32.896391 Y:-117.200281 Heading: 178" csvLine = mid(line,7,4) & sep & mid(line,22,10) & sep 'etc..' 'write to your csv file, here I only echo to the screen Wscript.echo header Wscript.echo csvLine 'week;seconds; '1799;251731.358; 

使用这种方法是更快的结束你不需要在电脑上安装Excel