首页 > 解决方案 > Vbscript 将文本文件中的数据提取到 Excel 中

问题描述

我是 VBScript 的新手。我需要将文本文件转换为 Excel 文件。试了几天还是失败。

我在文本文件中有数千行,但是在“====Succeeded MML Command====”行之后,我只需要将某些数据,例如 NE 放在第一列,Time 在第二列,MML SN 在第三列是 MML 命令、Retcode、Explain_info、机柜号、子架号、插槽号、TX 通道号、VSWR(0.01) 在接下来的列中。也一直在读取从文本文件中提取数据到 Excel中的代码看起来不错,但我更喜欢使用 vbscript。

示例文本

输出

目标输出

这是我所拥有的:

Dim objFSO
Dim TextFile
Dim TextRead
Dim Line, Line1, Line2, Line3
Dim Count

ExcelFilePath = "D:\Test\output.xlsx"

Set objExcel = CreateObject("Excel.Application")
objExcel.visible = true
Set objWB = objExcel.Workbooks.Open(ExcelFilePath)  
Set SheetObject = objWB.Worksheets("Sheet1") 

Const ForReading = 1 

TextFile = "D:\Test\*.txt"

set objFSO = CreateObject("Scripting.FileSystemObject")

set TextRead = objFSO.OpenTextFile(TextFile,ForReading)

row = 2 
With SheetObject

  .Range(.Columns(1),.Columns(11)).NumberFormat = "@"

  .Cells(1, 1).Value = "eNodeBName"
  .Cells(1, 2).Value = "Time"
  .Cells(1, 3).Value = "MML SN"
  .Cells(1, 4).Value = "MML Command"
  .Cells(1, 5).Value = "Retcode"
  .Cells(1, 6).Value = "Explain_info"
  .Cells(1, 7).Value = "Cabinet No."
  .Cells(1, 8).Value = "Subrack No."
  .Cells(1, 9).Value = "Slot No."
  .Cells(1, 10).Value = "TX Channel No."
  .Cells(1, 11).Value = "VSWR(0.01)"


 End With

  if instr(strText, "Succeeded") > 0 then
   msgbox("Found it!")
  end if
   msgbox("DONE")  

 objWB.Save
 objWB.Close
 objExcel.Quit

标签: exceltextvbscript

解决方案


推荐阅读