我正在从宏生成tsv文件,但是数据包含像“tm”符号这样的特殊字符,这反过来将被输入到服务器中的mysqlimport中。但是由于特殊字符,所以它不会在特殊字符之后加载字符串的其余部分。
我有以下宏将其保存到我的预分隔符和附件中,但是现在我想指定我想要将文件保存在其中的编码。我该怎么做?
Sub tsv()
Dim SrcRg As Range
Dim CurrRow As Range
Dim CurrCell As Range
Dim CurrTextStr As String
Dim ListSep As String
Dim FName As Variant
FName = Application.GetSaveAsFilename("", "TSV File (*.tsv), *.tsv")
'FName = Application.GetSaveAsFilename("", "CSV File (*.csv), *.csv")
'assign the character delimiter you want
ListSep = Chr(9)
'ListSep = "|"
'assign the enclosure character you want
ListEnc = "^"
If Selection.Cells.Count > 1 Then
Set SrcRg = Selection
Else
Set SrcRg = ActiveSheet.UsedRange
End If
Open FName For Output As #1
For Each CurrRow In SrcRg.Rows
CurrTextStr = ""
For Each CurrCell In CurrRow.Cells
CurrTextStr = CurrTextStr & ListEnc & CurrCell.Value & ListEnc & ListSep
Next
While Right(CurrTextStr, 1) = ListSep
CurrTextStr = Left(CurrTextStr, Len(CurrTextStr) - 1)
Wend
Print #1, CurrTextStr
Next
Close #1
End Sub发布于 2014-04-25 04:27:47
使用ADODB流对象。
Set BS = CreateObject("ADODB.Stream")
'2 = text so use writetext rather than 1 = binary and use write
BS.type = 2
'Get the list of chartypes by typing in a command prompt ***reg query HKEY_CLASSES_ROOT\MIME\Database\Charset***
BS.Charset = "UTF-8"
BS.open
BS.WriteText "Hi kiddies"
'A=Array(CByte("M"),CByte("Z"))
'BS.Write A
BS.SaveToFile "c:\myfile.txt", 2https://stackoverflow.com/questions/23283553
复制相似问题