龙空技术网

(12)文本文件操作参考

实用至上 69

前言:

今天朋友们对“opentextfile函数”大致比较关切,小伙伴们都想要分析一些“opentextfile函数”的相关文章。那么小编在网络上搜集了一些关于“opentextfile函数””的相关知识,希望姐妹们能喜欢,大家快快来学习一下吧!

打开文本文件

Workbooks.OpenText Filename:="路径\文件名",StartRow:=1,DataType:=xlDelimited,x

x为:

Comma:=True 如果为 True,则将逗号用作分隔符(DataType 必须为 xlDelimited). 默认值为 False。

Semicolon:=True 如果为 True,则将分号用作分隔符(DataType 必须为 xlDelimited). 默认值为 False。

Space:=True 如果为 True,则将空格用作分隔符(DataType 必须为 xlDelimited). 默认值为 False。

Other:=True,OtherChar:="符号" 如果为 True,则将 OtherChar 参数指定的字符用作分隔符(DataType 必须为 xlDelimited)。默认值为 False。

建立查询表

With ActiveSheet.QueryTables.Add(Connection:="TEXT;"&路径\文件名,Destination:=Range("A1"))

.TextFilePlatform=936

.TextFileCommaDelimiter=True

.Refresh

End With

写入文本文件

Setfso=NewScripting.FileSystemObject

Set myTxt=fso.CreateTextFile(Filename:="路径\文件名",OverWrite:=True)

With myTxt

.Write内容

.WriteBlankLines5

.WriteLine内容

.WriteLine内容

.Write内容&vbCrLf&内容

.Close

End With

读取文本文件

Setfso=NewScripting.FileSystemObject

Set myTxt=fso.OpenTextFile(Filename:="路径\文件名",IOMode:=ForReading)

With myTxt

i=1

DoUntil.AtEndOfStream

Cells(i,1)=.ReadLine

i=i+1

Loop

.Close

End With

读取文本文件字符

Setfso=NewScripting.FileSystemObject

Set myTxt=fso.OpenTextFile(Filename:="路径\文件名",IOMode:=ForReading)

MsgBox文本文件的前5个字符为:&myTxt.Read(5)

myTxt.Close

读取文本文件某行内容

Setfso=NewScripting.FileSystemObject

Set myTxt=fso.OpenTextFile(Filename:="路径\文件名",IOMode:=ForReading)

With myTxt

Fori=1Ton

.SkipLine

Nexti

MsgBox文本文件的第n+1行数据为:&.ReadLine

.Close

End With

标签: #opentextfile函数