【发布时间】:2018-06-21 16:09:23
【问题描述】:
我想使用 vb 脚本或宏将我的 XLXS 文件转换为 CSV UTF-8 格式。
if WScript.Arguments.Count < 2 Then
WScript.Echo "Error! Please specify the source path and the destination. Usage: XlsToCsv SourcePath.xls Destination.csv"
Wscript.Quit
End If
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
oBook.SaveAs WScript.Arguments.Item(1), 6
oBook.Close False
oExcel.Quit
WScript.Echo "Done"enter code here
上述脚本适用于普通格式。
请帮助我转换成 UTF-8 格式
我也尝试过以下代码,但它会转换为垃圾字符
Public Sub convert_UnicodeToUTF8()
Dim parF1, parF2 As String
parF1 = "C:\shrangi\SX_Hospital.xlsx"
parF2 = "C:\shrangi\SX_Hospital.csv"
Const adSaveCreateOverWrite = 2
Const adTypeText = 2
Dim streamSrc, streamDst ' Source / Destination
Set streamSrc = CreateObject("ADODB.Stream")
Set streamDst = CreateObject("ADODB.Stream")
streamDst.Type = adTypeText
streamDst.Charset = "UTF-8"
streamDst.Open
With streamSrc
.Type = adTypeText
.Charset = "UTF-8"
.Open
.LoadFromFile parF1
.copyTo streamDst
.Close
End With
streamDst.SaveToFile parF2, adSaveCreateOverWrite
streamDst.Close
Set streamSrc = Nothing
Set streamDst = Nothing
End Sub
【问题讨论】:
-
参考this
-
VBA 的优势在于它与文档一起存储并在文档的上下文中运行。您似乎正在处理外部文件。使用 PowerShell 等不同的编程环境是否更合适?
-
stackoverflow.com/users/2226988/tom-blodget 如何使用 powershell 实现它
标签: excel vba utf-8 character-encoding export-to-csv