Open 文件名 for 打开方式 as 文件编号

打开方式: Input :只能读,不能写 Append:允许读也允许写,如果存在文件就追加记录,如果没有就新建文件 Output:可以读,也可以写。但总会把原来的同名文件删除,再新建一个

读取txt文件内容方法 input:从文件中读取指定数量的字符。 Input #:把数据读出放在变量里,变量用逗号分隔 Line Input #:取出完整的一行

向文件中写入数据
 write #:向文件中写入值,值用引号引起来。如果想在同一行中继续写入,可以在前一次写时结尾添加“;”号
 Print #:向文件中写入值,如果想在同一行中继续写入,可以在前一次写时结尾添加“;”
字符的间隔符
 Spc(n)表示输入n个空字符

范例:用Print写入 1、分行输入 Sub t1() Dim f As String f = ThisWorkbook.path & "\a.txt" Open f For Output As #1 Print #1, "产品名称" Print #1, Date Close #1 End Sub 2、在同一行输入 Sub t2() Dim f As String f = ThisWorkbook.path & "\a.txt" Open f For Output As #1 Print #1, "产品名称"; Print #1, "A产品" Close #1 End Sub 3、输入时添加空格符 Sub t3() Dim f As String f = ThisWorkbook.path & "\a.txt" Open f For Output As #1 Print #1, "产品名称"; Spc(5); Print #1, "A产品" Close #1 End Sub

4、在指定的列数输入 Sub t4() Dim f As String f = ThisWorkbook.path & "\a.txt" Open f For Output As #1 Print #1, "产品名称"; Tab(8); '在第10列输入下面的,如果为空则插入到下一个打印的位置 Print #1, "A产品" Close #1 End Sub

范例:用Write写入 Sub t5() Dim f As String f = ThisWorkbook.path & "\a.txt" Open f For Output As #1 Write #1, "产品名称" Write #1, 5 Close #1 End Sub

Sub t6() Dim f As String f = ThisWorkbook.path & "\a.txt" Open f For Output As #1 Write #1, "产品名称"; Write #1, 5 Close #1 End Sub

Sub t7() Dim f As String f = ThisWorkbook.path & "\a.txt" Open f For Output As #1 Write #1, "产品名称"; 5 '这里逗号等同于"; "(分号)" Close #1 End Sub