把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

  

 

上一篇:resMLP的PyTorch代码和分析(一)


下一篇:我国共计34个省级行政区,包括23个省、5个自治区、4个直辖市、2个特别行政区。