vba txt读写的几种方式

四种方式写txt

1、这种写出来的是ANSI格式的txt

   Dim TextExportFile As String
    
    TextExportFile = ThisWorkbook.Path & "\lcx.txt"
    
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.CreateTextFile(TextExportFile, True)

    f.WriteLine "罗彩霞:lcx"
    f.Close

2、这种是Unicode格式

    Dim TextExportFile As String
    
    TextExportFile = ThisWorkbook.Path & "\lcx.txt"
    
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.CreateTextFile(TextExportFile, True, True)

    f.WriteLine "罗彩霞:lcx"
    f.Close

3、这种是UTF-8格式的

Sub WriteTxt(path_, Filename, k)

    Dim tss As String
     
    'Filename = Application.GetSaveAsFilename(fileFilter:="Text Files (*.txt), *.txt")
    
    Open path_ & "\" & Filename For Output As #1
     
    For i = 1 To k
        If Cells(i, 4).Value <> "" Then
            tss = Cells(i, 4) & vbTab & Cells(i, 5) & vbTab & Cells(i, 6) & vbTab & Cells(i, 7) & vbTab & Cells(i, 8) & vbTab & Cells(i, 9)
            Print #1, tss
        End If
    Next
     
    Close #1
    
End Sub

4、这种方式可以设置字符格式

Sub WriteUTF8()
    Dim WriteStream As Object
    Set WriteStream = CreateObject("ADODB.Stream")
    With WriteStream
        .Type = 2               'adTypeText
        .Charset = "UTF-8"
        .Open
        .WriteText "你好utf-8"
        .SaveToFile ThisWorkbook.path & "\1.txt", 2  'adSaveCreateOverWrite
        .Flush
        .Close
    End With
    Set WriteStream = Nothing
End Sub

 

转载于:https://www.cnblogs.com/LcxSummer/p/10457954.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值