'以下读出了文本文件所有行的内容 dim file1,i,j as integer dim filename,temp() as string i=1 filename="c:\test.txt" '要打开的文本文件 open filename for input as file do while not eof(file1) line input #file1,temp(i) '整行读出,存入temp中 i=i+1 loop close file1 ''''''''''''''''''''''''''''''''''''''''''''''''''''''' '以下把文本文件写入EXCEL列中 dim exlApp As Excel.Application 'EXCEL表格 dim exlBook As Excel.Workbook '工作簿 dim exlSheet As Excel.Worksheet '工作表 dim exlFilename as string '要写入的EXCEL文件名 exlfilename="c:\test.xls" Set exlApp = New Excel.Application exlApp.Visible = False '不可见 Set exlBook = exlApp.Workbooks.Open(exlfilename) Set exlSheet = exlBook.Worksheets(1) for j=1 to i exlsheet.cells(1,j)=temp(j) '(行,列) next j exlbook.save exlbook.close exlapp.quit set exlsheet=nothing set exlbook=nothing set exlapp=nothing
哦,上一贴是我回复的。其实你在上次代码的基础上修改一下就可以了,方法有许多,我还是修改一下上次的代码,希望你能读明白: '读出文本内容 Dim pFile As String Dim hFile As Integer Dim sFile As String pFile = "C:\test.txt" hFile = FreeFile()
Open pFile For Binary As hFile sFile = Space(LOF(hFile)) Get hFile, , sFile Close hFile
'处理读取的数据: Dim fLines() As String Dim sRows() As String Dim sExcel() As String '插入Excel的内容 Dim i As Integer Dim row As Integer
fLines = Split(sFile, vbCrLf) '按vbCrLf分组,得到行数据 '比如指定第10行: sRows = Split(fLines(9), Chr(32)) ReDim sExcel(UBound(sRows), 0) For i = 0 To UBound(sRows) If Trim(sRows(i)) <> "" Then sExcel(row, 0) = sRows(i) row = row + 1 End If Next i
'导入Excel Dim oExcel As Object Dim oBook As Object Dim oSheet As Object
Set oExcel = CreateObject("Excel.Application") Set oBook = oExcel.Workbooks.open("C:\test.xls") Set oSheet = oBook.Worksheets(1) '指定插入G列 oSheet.Range("G1").Resize(row, 1).Value = sExcel oExcel.Visible = True Set oSheet = Nothing Set oBook = Nothing oExcel.Quit Set oExcel = Nothing
程序中?还是文本文件里?
在VB里使用com方式打开excel文件,就可以直接向指定单元格写数据。只是没看明白你的指定文本是指什么。
dim file1,i,j as integer
dim filename,temp() as string
i=1
filename="c:\test.txt" '要打开的文本文件
open filename for input as file
do while not eof(file1)
line input #file1,temp(i) '整行读出,存入temp中
i=i+1
loop
close file1
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'以下把文本文件写入EXCEL列中
dim exlApp As Excel.Application 'EXCEL表格
dim exlBook As Excel.Workbook '工作簿
dim exlSheet As Excel.Worksheet '工作表
dim exlFilename as string '要写入的EXCEL文件名
exlfilename="c:\test.xls"
Set exlApp = New Excel.Application
exlApp.Visible = False '不可见
Set exlBook = exlApp.Workbooks.Open(exlfilename)
Set exlSheet = exlBook.Worksheets(1)
for j=1 to i
exlsheet.cells(1,j)=temp(j) '(行,列)
next j
exlbook.save
exlbook.close
exlapp.quit
set exlsheet=nothing
set exlbook=nothing
set exlapp=nothing
Dim pFile As String
Dim hFile As Integer
Dim sFile As String pFile = "C:\test.txt"
hFile = FreeFile()
Open pFile For Binary As hFile
sFile = Space(LOF(hFile))
Get hFile, , sFile
Close hFile
'处理读取的数据:
Dim fLines() As String
Dim sRows() As String
Dim sExcel() As String '插入Excel的内容
Dim i As Integer
Dim row As Integer
fLines = Split(sFile, vbCrLf) '按vbCrLf分组,得到行数据
'比如指定第10行:
sRows = Split(fLines(9), Chr(32))
ReDim sExcel(UBound(sRows), 0)
For i = 0 To UBound(sRows)
If Trim(sRows(i)) <> "" Then
sExcel(row, 0) = sRows(i)
row = row + 1
End If
Next i
'导入Excel
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.open("C:\test.xls")
Set oSheet = oBook.Worksheets(1)
'指定插入G列
oSheet.Range("G1").Resize(row, 1).Value = sExcel
oExcel.Visible = True Set oSheet = Nothing
Set oBook = Nothing
oExcel.Quit
Set oExcel = Nothing