Edw*_*uay 1 macos excel vba utf-8
我有以下Excel VBA脚本,在Excel for Mac 2011中使用该脚本将 Excel 文件的所有工作表导出到 .csv 文件。
Sub save_all_csv()
On Error Resume Next
Dim ExcelFileName As String
ExcelFileName = ThisWorkbook.Name
For Each objWorksheet In ThisWorkbook.Worksheets
ExcelFileNameWithoutExtension = RemoveExtension(ExcelFileName)
CsvFileName = ExcelFileNameWithoutExtension & "__" & objWorksheet.Name & ".csv"
Application.DisplayAlerts = False
objWorksheet.SaveAs Filename:="data:storage:original_excel:" & CsvFileName, FileFormat:=xlCSV, CreateBackup:=False
Application.DisplayAlerts = True
Next
Application.DisplayAlerts = False
Application.Quit
End Sub
Function RemoveExtension(strFileName As String)
strFileName = Replace(strFileName, ".xlsx", "")
strFileName = Replace(strFileName, ".xls", "")
RemoveExtension = strFileName
End Function
Run Code Online (Sandbox Code Playgroud)
问题是它们保存为Western Mac OS Roman,我无法通过 PHP 将其转换为 UTF-8,所以我想让 VBA 首先将这些文件保存为 UTF-8 格式。

我找到了一些通过 Stream 对象和CreateObject从 VBA 保存文本的解决方案,但显然不可能在 Mac 上使用 CreateObject 直接写入文件。
如何使用 Excel for Mac 2011 将工作表另存为 UTF-8 格式的 CSV 文件?
小智 5
我遇到了同样的问题,最终只是编写了一个例程来编码为utf-8。这段代码可以在我的 MacBook Pro 的 Mac:Excel 2011 上运行。只需使用此函数创建的字节数组即可进行二进制文件输出。就我而言,我正在处理泰语脚本,以便自动使用 Mac 出色的语音合成器。
Private Function UTF8Encode(b() As Byte) As Byte()
' Function to convert a Unicode Byte array into a byte array that can be written to create a UTF8 Encoded file.
' Note the function supports the one, two and three byte UTF8 forms.
' Note: the MS VBA documentation is confusing. It says the String types only supports single byte charset
' however, thankfully, it does in fact contain 2 byte Unicode values.
' Wrote this routine as last resort, tried many ways to get unicode chars to a file or to a shell script call
' but this was the only way could get to work.
' RT Perkin
' 30/10/2015
Dim b1, b2, b3 As Byte ' UTF8 encoded bytes
Dim u1, u2 As Byte ' Unicode input bytes
Dim out As New Collection ' Collection to build output array
Dim i, j As Integer
Dim unicode As Long
If UBound(b) <= 0 Then
Exit Function
End If
For i = 0 To UBound(b) Step 2
u1 = b(i)
u2 = b(i + 1)
unicode = u2 * 256 + u1
If unicode < &H80 Then
' Boils down to ASCII, one byte UTF-8
out.Add (u1)
ElseIf unicode < &H800 Then
' Two byte UTF-8
' Code path not tested
b1 = &H80 Or (&H3F And u1)
b2 = &HC0 Or (Int(u1 / 64)) Or ((&H7 And u2) * 4)
out.Add (b2) ' Add most significant byte first
out.Add (b1)
ElseIf unicode < &H10000 Then
' Three byte UTF-8
' Thai chars are in this range
b1 = &H80 Or (&H3F And u1)
b2 = &H80 Or (Int(u1 / 64)) Or ((&HF And u2) * 4)
b3 = &HE0 Or (Int(u2 / 16))
out.Add (b3) ' Add most significant byte first
out.Add (b2)
out.Add (b1)
Else
' This case wont arise as VBA strings are 2 byte. Which makes some Unicode codepoints uncodeable.
End If
Next
Dim outBytes() As Byte
ReDim outBytes(1 To out.Count)
For j = 1 To out.Count
outBytes(j) = CByte(out.Item(j))
Next
UTF8Encode = outBytes
End Function
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5270 次 |
| 最近记录: |