vbs将文本文件内容输入到excel中

2024-12-27 06:44:38
推荐回答(1个)
回答1:

'''一,将文本文件内容拆分为二维数组:
dim a()
set fso=CreateObject("Scripting.FileSystemObject")
set fr=fso.opentextfile("G:\data.txt",1)
s=fr.readall
'msgbox s
a1=split(s,chr(13))
cRow=ubound(a1)
a2=split(a1(0),",")
cCol=ubound(a2)
redim a(cRow,cCol)
for i=0 to cRow
a2=split(a1(i),",")
for j=0 to cCol
a(i,j)=a2(j)
next
next
'''建立工作簿,写入数据,保存后退出应用程序:
Dim xlsApp,xlsWorkBook,xlsSheet
Dim Row

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = FALSE
Set xlsWorkBook = objExcel.Workbooks.Add

for i=0 to cRow
for j=0 to cCol
objExcel.Cells(i+2,j+1).Value = a(i,j)
next
next

xlsWorkBook.SaveAs "G:\data.xls"
xlsWorkBook.Close
objExcel.Quit
Set xlsApp = Nothing
Set xlsWorkBook = Nothing
Set objExcel = Nothing
'''============== By daode1212 ,QQ:1501488900 ================