Excel VBA 读取txt文件,把txt中的"#13#10"换成 换行。
发布网友
发布时间:2024-08-20 05:30
我来回答
共3个回答
热心网友
时间:2024-08-27 13:54
源文本文件必须是ANSI码的文本文件.否则不能准确读出和转换.如果不是ANSI编码的可以转换一下再做
Sub RTXT()
Close #1
Dim FilePath, NeiRong As String, Rng As Range, Crng As Range
Dim arr() As String
FilePath = Application.GetOpenFilename("文本文件(*.txt),*.txt")
If FilePath = False Then Exit Sub
Open FilePath For Input As #1
NeiRong = StrConv(InputB(LOF(1), 1), vbUnicode)
Close #1
arr = Split(NeiRong, "#13#10")
FilePath = Left(FilePath, Len(FilePath) - 4) & "-Replaced.txt"
Open FilePath For Output As #1
For i = 0 To UBound(arr)
Print #1, arr(i)
Next
Close #1
MsgBox "已完成!文件另存为:" & FilePath
End Sub
热心网友
时间:2024-08-27 13:53
Sub aa()
cc="C:\123.txt" ‘这里你自己换成要替换文本的路径
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile(cc, 1)
a = f.readall
f.Close
s = Replace(a, "#13#10", vbCrLf)
Set f = fs.OpenTextFile(cc, 2)
f.writeline s
f.Close
set f =nothing
set fs =nothing
End Sub
热心网友
时间:2024-08-27 13:51
步骤一 设置宏先把加载宏安全性设为中或低
步骤二 插入模块alt+f11打开VBA编辑器
打开 菜单栏》插入》模块,把下面代码加进去
Sub DaoChu()
Dim I As Integer, J As Long, RW As Long
For I = 1 To ActiveSheet.UsedRange.Columns.Count
Open ThisWorkbook.Path & "\" & Cells(1, I) & ".txt" For Output As 1
For J = 2 To Cells(65536, I).End(3).Row
Print #1, Cells(J, I).Value
Next J
Close 1
Next I
MsgBox "数据导出完毕!", vbOKOnly, "导出成功"
End Sub
步骤三 保存以后关闭EXCEL步骤四 测试再重新打开excel,按ctrl+shift+P就完成导出了,文件在D盘根目录下
如果想放在其他目录,可以吧Open “D:\” &中的D:\改成要的目录,但是目录不能有中文字符。