【问题标题】:How to save worksheets as CSV files in UTF-8 format with Excel for Mac 2011?如何使用 Excel for Mac 2011 将工作表保存为 UTF-8 格式的 CSV 文件?
【发布时间】:2011-06-10 23:57:39
【问题描述】:

我有以下 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

问题是它们保存为 Western Mac OS Roman,我无法通过 PHP 转换为 UTF-8,所以我想让 VBA 将这些文件保存为 UTF-8首先是格式。

我找到了一些通过 Stream 对象和 CreateObject 从 VBA 保存文本的解决方案,但它是 apparently not possibile on the Mac to use CreateObject to write directly to files

如何使用 Excel for Mac 2011 将工作表保存为 UTF-8 格式的 CSV 文件?

【问题讨论】:

  • 您是否尝试过使用FileFormat:=xlCSVUTF8

标签: macos utf-8 excel vba


【解决方案1】:

我认为您必须自己创建文件是对的,而且我很确定您不能使用 CreateObject 来制作流。但是,您可能想考虑使用 Open、Write 等(周围有很多示例,但 here's one)。不过,我会先做一个非常小的实验性的,只是为了检查最终的编码。

克里斯

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,最后只是写了一个例程来编码为utf-8。此代码适用于我在 Mac:Excel 2011 中的 MacBook Pro。只需使用 此函数创建的字节数组来进行二进制文件输出。就我而言,我正在处理泰语脚本,以便自动使用 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
    

    【讨论】:

    • 哇!非常感谢!你让我今天一整天都感觉很好。这应该被标记为正确答案。
    • 很高兴它对你有用!
    【解决方案3】:

    我不知道这是否适用于 Excel for Mac 2011,但对于较新的版本,只需使用 FileFormat:=xlCSVUTF8,它将以 UTF-8 保存文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-23
      • 1970-01-01
      • 2014-02-11
      • 2012-10-12
      • 2013-04-20
      • 1970-01-01
      • 2015-03-28
      相关资源
      最近更新 更多