这是对我有用的代码:
Sub Export_File()
Dim myFile As String, r As Integer, mypath As String, dline As String
mypath = "C:\RESULTS\" 'path for file
myFile = "file.txt" 'file name
dline = ""
For r = 2 To Range("A" & Rows.Count).End(xlUp).Row
A = Cells(r, "A") & Application.WorksheetFunction.Rept(" ", 3 - Len(Cells(r, "A")))
B = Cells(r, "B") & Application.WorksheetFunction.Rept(" ", 5 - Len(Cells(r, "B")))
C = Cells(r, "C") & Application.WorksheetFunction.Rept(" ", 5 - Len(Cells(r, "C")))
D = Cells(r, "D") & Application.WorksheetFunction.Rept(" ", 5 - Len(Cells(r, "D")))
E = Cells(r, "E") & Application.WorksheetFunction.Rept(" ", 7 - Len(Cells(r, "E")))
F = Cells(r, "F") & Application.WorksheetFunction.Rept(" ", 7 - Len(Cells(r, "F")))
G = Cells(r, "G") & Application.WorksheetFunction.Rept(" ", 5 - Len(Cells(r, "G")))
H = Cells(r, "H") & Application.WorksheetFunction.Rept(" ", 5 - Len(Cells(r, "H")))
I = Cells(r, "I") & Application.WorksheetFunction.Rept(" ", 5 - Len(Cells(r, "I")))
dline = dline & A & B & C & D & E & F & G & H & I & vbLf & vbCr
Next r
Open mypath & myFile For Output As #1 'Replaces existing file
Print #1, dline
Close #1
MsgBox ("File Created " & mypath & myFile)
End Sub