用VB实现对一个TXT数据文件进行分割并导出
发布网友
发布时间:2024-10-08 02:06
我来回答
共2个回答
热心网友
时间:2024-10-08 02:25
Option Explicit
Private Sub Command1_Click()
Dim i As Integer
Dim k As Integer
Dim a As String
Dim s As String
a = ""
s = ""
i = 1
Open "d:\abc.txt" For Input As #1 '注意,这里的原文件名,不能与分割后的文件重复,否则会出错
Do While Not EOF(1)
Line Input #1, a
s = s & a & vbCrLf'按行读取
k = k + 1
If k Mod 500 = 0 Then '够500行,则
Open "d:\原文件" & i & ".txt" For Output As #2
Print #2, s '则保存,名为 i.txt ,i=1,2,3...
Close #2
a = ""
s = ""
i = i + 1
End If
Loop
Close #1
If k Mod 500 <> 0 Then '循环结束后,最后的不足500行的部分也要保存!
Open "d:\原文件" & i & ".txt" For Output As #3
Print #3, s
Close #3
End If
MsgBox "文件分割完成"
End Sub
热心网友
时间:2024-10-08 02:25
Private Sub Command1_Click()
Open "被分割文件名" For Input As #1 '为你要分割的文件
Hnum = 0 '用于统计行数
Do While EOF(1) <> True
Line Input #1, s
Hnum = Hnum + 1
Loop '统计行数
Seek #1, 1 '移动到文件的顶部
filenum = Hnum / 500 + 1 '计算需要的文件数
MkDir "D:\新建文件夹" '你要新建的文件夹
For i = 1 To filenum '逐个文件进行写入
Open "d:\新建文件夹\新建文件" & "" & i & "" & ".txt" For Output As i + 1
For j = 1 To 500
If EOF(1) = True Then
Exit For
End If
Line Input #1, s
Print #i + 1, s
Next j
Close #i + 1
Next i
Close #1
End Sub