2个回答
展开全部
'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
'三、Print和Write区别
'1 写到到txt文件后,字符会添加“,”(逗号)
'2 除文本外,日期、逻辑值输入结果不一样,两边会加#号
Sub t8()
Dim f As String
f = ThisWorkbook.path & "\a.txt"
Open f For Output As #1
Print #1, Date; 1 = 1; Null
Write #1, Date; 1 = 1, Null
Close #1
End Sub
'四 不同类型数值的输入的
'在用print写入数据时
'1 日期后要加空格
'2 数字前后都加空格
'3 字符前后均不加空格
Sub t9()
Dim f As String
f = ThisWorkbook.path & "\a.txt"
Open f For Output As #1
Print #1, Date; 12
Print #1, Date; "ABC"
Print #1, Date; "我爱你"
Print #1, Date; Date
Print #1, "我爱你"; 12
Print #1, "我爱你"; "abc"
Print #1, "我爱你"; Date
Print #1, "我爱你"; "abc"
Print #1, 12; "abc"
Print #1, 12; "我爱你"
Print #1, 12; 123
Print #1, 12; "123"
Close #1
End Sub
追问
你这只是到处excel中的数据,没有怎样将txt里边的数据导入到excel中啊
展开全部
在excel中用宏
Sub test()
Dim i%, ar(1 To 60000, 1 To 20), ttt$, brr()
Dim wordApp As Object, myword As Object, t As Object
Application.ScreenUpdating = False
Set wordApp = CreateObject("Word.Application")
Set myword = wordApp.Documents.Open(ThisWorkbook.Path & "\全省项目排版1014.doc")
wordApp.Visible = 0
On Error Resume Next
ReDim brr(1 To myword.Tables.Count)
For Each t In myword.Tables
If t.Rows.Count < 19 Then
j = 0
ttt = t.Cell(j + 1, 1).Range.Text
Do While InStr(ttt, "名称") = 0
j = j + 1
ttt = t.Cell(j + 1, 1).Range.Text
If j = 5 Then Exit Do
Loop
If j < 4 Then
i = i + 1
ar(i, 1) = t.Cell(1 + j, 2).Range.Text
ar(i, 2) = t.Cell(2 + j, 2).Range.Text
ar(i, 3) = t.Cell(3 + j, 3).Range.Text
ar(i, 4) = t.Cell(3 + j, 5).Range.Text
ar(i, 5) = t.Cell(4 + j, 3).Range.Text
ar(i, 6) = t.Cell(5 + j, 3).Range.Text
ar(i, 7) = t.Cell(6 + j, 3).Range.Text
ar(i, 8) = t.Cell(6 + j, 5).Range.Text
ar(i, 9) = t.Cell(7 + j, 3).Range.Text
ar(i, 10) = t.Cell(8 + j, 3).Range.Text
ar(i, 11) = t.Cell(9 + j, 3).Range.Text
ar(i, 12) = t.Cell(9 + j, 5).Range.Text
ar(i, 13) = t.Cell(10 + j, 3).Range.Text
ar(i, 14) = t.Cell(11 + j, 3).Range.Text
ar(i, 15) = t.Cell(12 + j, 2).Range.Text
ar(i, 16) = t.Cell(13 + j, 2).Range.Text
ar(i, 17) = t.Cell(14 + j, 3).Range.Text
ar(i, 18) = t.Cell(14 + j, 5).Range.Text
ar(i, 19) = t.Cell(15 + j, 3).Range.Text
ar(i, 20) = t.Cell(15 + j, 5).Range.Text
End If
ElseIf t.Rows.Count > 18 Then
For j = 1 To t.Rows.Count Step 18
i = i + 1
ar(i, 1) = t.Cell(1 + 3, 2).Range.Text
ar(i, 2) = t.Cell(2 + 3, 2).Range.Text
ar(i, 3) = t.Cell(3 + 3, 3).Range.Text
ar(i, 4) = t.Cell(3 + 3, 5).Range.Text
ar(i, 5) = t.Cell(4 + 3, 3).Range.Text
ar(i, 6) = t.Cell(5 + 3, 3).Range.Text
ar(i, 7) = t.Cell(6 + 3, 3).Range.Text
ar(i, 8) = t.Cell(6 + 3, 5).Range.Text
ar(i, 9) = t.Cell(7 + 3, 3).Range.Text
ar(i, 10) = t.Cell(8 + 3, 3).Range.Text
ar(i, 11) = t.Cell(9 + 3, 3).Range.Text
ar(i, 12) = t.Cell(9 + 3, 5).Range.Text
ar(i, 13) = t.Cell(10 + 3, 3).Range.Text
ar(i, 14) = t.Cell(11 + 3, 3).Range.Text
ar(i, 15) = t.Cell(12 + 3, 2).Range.Text
ar(i, 16) = t.Cell(13 + 3, 2).Range.Text
ar(i, 17) = t.Cell(14 + 3, 3).Range.Text
ar(i, 18) = t.Cell(14 + 3, 5).Range.Text
ar(i, 19) = t.Cell(15 + 3, 3).Range.Text
ar(i, 20) = t.Cell(15 + 3, 5).Range.Text
Next
End If
Next
myword.Close False
wordApp.Quit
Set wordApp = Nothing
Set myword = Nothing
ActiveSheet.UsedRange.Offset(2).ClearContents
With [a3].Resize(i, 20)
.Value = ar
.Replace Chr(7), "", xlPart
End With
Application.ScreenUpdating = True
End Sub
Sub test()
Dim i%, ar(1 To 60000, 1 To 20), ttt$, brr()
Dim wordApp As Object, myword As Object, t As Object
Application.ScreenUpdating = False
Set wordApp = CreateObject("Word.Application")
Set myword = wordApp.Documents.Open(ThisWorkbook.Path & "\全省项目排版1014.doc")
wordApp.Visible = 0
On Error Resume Next
ReDim brr(1 To myword.Tables.Count)
For Each t In myword.Tables
If t.Rows.Count < 19 Then
j = 0
ttt = t.Cell(j + 1, 1).Range.Text
Do While InStr(ttt, "名称") = 0
j = j + 1
ttt = t.Cell(j + 1, 1).Range.Text
If j = 5 Then Exit Do
Loop
If j < 4 Then
i = i + 1
ar(i, 1) = t.Cell(1 + j, 2).Range.Text
ar(i, 2) = t.Cell(2 + j, 2).Range.Text
ar(i, 3) = t.Cell(3 + j, 3).Range.Text
ar(i, 4) = t.Cell(3 + j, 5).Range.Text
ar(i, 5) = t.Cell(4 + j, 3).Range.Text
ar(i, 6) = t.Cell(5 + j, 3).Range.Text
ar(i, 7) = t.Cell(6 + j, 3).Range.Text
ar(i, 8) = t.Cell(6 + j, 5).Range.Text
ar(i, 9) = t.Cell(7 + j, 3).Range.Text
ar(i, 10) = t.Cell(8 + j, 3).Range.Text
ar(i, 11) = t.Cell(9 + j, 3).Range.Text
ar(i, 12) = t.Cell(9 + j, 5).Range.Text
ar(i, 13) = t.Cell(10 + j, 3).Range.Text
ar(i, 14) = t.Cell(11 + j, 3).Range.Text
ar(i, 15) = t.Cell(12 + j, 2).Range.Text
ar(i, 16) = t.Cell(13 + j, 2).Range.Text
ar(i, 17) = t.Cell(14 + j, 3).Range.Text
ar(i, 18) = t.Cell(14 + j, 5).Range.Text
ar(i, 19) = t.Cell(15 + j, 3).Range.Text
ar(i, 20) = t.Cell(15 + j, 5).Range.Text
End If
ElseIf t.Rows.Count > 18 Then
For j = 1 To t.Rows.Count Step 18
i = i + 1
ar(i, 1) = t.Cell(1 + 3, 2).Range.Text
ar(i, 2) = t.Cell(2 + 3, 2).Range.Text
ar(i, 3) = t.Cell(3 + 3, 3).Range.Text
ar(i, 4) = t.Cell(3 + 3, 5).Range.Text
ar(i, 5) = t.Cell(4 + 3, 3).Range.Text
ar(i, 6) = t.Cell(5 + 3, 3).Range.Text
ar(i, 7) = t.Cell(6 + 3, 3).Range.Text
ar(i, 8) = t.Cell(6 + 3, 5).Range.Text
ar(i, 9) = t.Cell(7 + 3, 3).Range.Text
ar(i, 10) = t.Cell(8 + 3, 3).Range.Text
ar(i, 11) = t.Cell(9 + 3, 3).Range.Text
ar(i, 12) = t.Cell(9 + 3, 5).Range.Text
ar(i, 13) = t.Cell(10 + 3, 3).Range.Text
ar(i, 14) = t.Cell(11 + 3, 3).Range.Text
ar(i, 15) = t.Cell(12 + 3, 2).Range.Text
ar(i, 16) = t.Cell(13 + 3, 2).Range.Text
ar(i, 17) = t.Cell(14 + 3, 3).Range.Text
ar(i, 18) = t.Cell(14 + 3, 5).Range.Text
ar(i, 19) = t.Cell(15 + 3, 3).Range.Text
ar(i, 20) = t.Cell(15 + 3, 5).Range.Text
Next
End If
Next
myword.Close False
wordApp.Quit
Set wordApp = Nothing
Set myword = Nothing
ActiveSheet.UsedRange.Offset(2).ClearContents
With [a3].Resize(i, 20)
.Value = ar
.Replace Chr(7), "", xlPart
End With
Application.ScreenUpdating = True
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询