把Excel导入TXT文件
作者:互联网
一,Open语句方式,代码如下:
Sub Excel数据存入txt文件() Dim arr, brr Dim str As String, ptr As String Dim row As Integer, num As Integer arr = Range("A1").CurrentRegion ptr = ThisWorkbook.Path & "\database\工作表数据.txt" num = FreeFile Open ptr For Output As #num For row = 1 To UBound(arr) brr = WorksheetFunction.Index(arr, row, 0) str = Join(brr, ",") Print #num, str Next row Close #num End Sub
二,ADO的Stream对象连接方式,代码如下:
Sub Excel写入txt文件() Dim stm As New ADODB.Stream Dim ptr As String Dim str As String Dim arr, brr, i As Integer arr = Range("A1").CurrentRegion ptr = ThisWorkbook.Path & "\database\数据.txt" With stm .Charset = "utf-8" '文件编码格式,可以GB2312,Unicode .Open For i = 1 To UBound(arr) brr = WorksheetFunction.Index(arr, i, 0) str = Join(brr, ",") .WriteText str, adWriteLine Next i .SaveToFile ptr, adSaveCreateOverWrite .Close End With Set stm = Nothing End Sub
标签:Dim,arr,num,Excel,brr,导入,str,TXT,ptr 来源: https://www.cnblogs.com/mrlajie/p/15569723.html