|
Option Explicit
Sub TEST()
Dim ar, br, cr, i&, j&, strTxt$, strPath$, strFileName$
strPath = ThisWorkbook.Path & "\"
strFileName = strPath & "模板文件.txt"
If Dir(strFileName) = "" Then MsgBox "模板文件不存在!": Exit Sub
Application.ScreenUpdating = False
strTxt = ReadFromTextFile(strFileName)
ar = Split(strTxt, vbCrLf)
ReDim br(1 To 5)
br(1) = ar(0): br(5) = ar(UBound(ar))
ar = [A1].CurrentRegion.Value
ReDim cr(1 To UBound(ar))
For i = 1 To UBound(ar)
cr(i) = ar(i, 1) & vbTab & ar(i, 2)
Next i
br(3) = Join(cr, vbCrLf)
WriteToTextFile strFileName, Join(br, vbCrLf)
Application.ScreenUpdating = True
Beep
End Sub
Function ReadFromTextFile$(ByVal strFullName$, Optional ByVal strCharSet$ = "UTF-8")
With CreateObject("ADODB.Stream")
.Type = 2
.Mode = 3
.Charset = strCharSet
.Open
.LoadFromFile strFullName
ReadFromTextFile = .ReadText
.Close
End With
End Function
Function WriteToTextFile(ByVal strFullName$, ByVal strTxt$, Optional ByVal strCharSet$ = "UTF-8")
With CreateObject("ADODB.Stream")
.Type = 2
.Mode = 3
.Charset = strCharSet
.Open
.WriteText strTxt
.SaveToFile strFullName, 2
.flush
.Close
End With
End Function
|
评分
-
2
查看全部评分
-
|