如何在VBA中打开用不正确的字符编码编写的Excel文件

发布于 2024-11-10 09:06:48 字数 1009 浏览 3 评论 0原文

我使用文本编辑器阅读 Excel 2003 文件来查看一些标记语言。 当我在 Excel 中打开该文件时,它显示不正确的字符。检查文件后,我发现编码是 Windows 1252 或类似的编码。如果我手动将其替换为 UTF-8,我的文件可以正常打开。好的,到目前为止一切顺利,我可以手动纠正这个问题。

现在的技巧是这个文件是自动生成的,我需要使用桌面上有限的工具(没有 perl 或其他脚本语言)自动处理它(没有人工交互)。

有没有简单的方法可以使用正确的编码在 VBA 中打开此 XL 文件(并忽略文件中指定的编码)?

请注意,Workbook.ReloadAs 对我不起作用,它会在错误时退出(并且需要手动操作,因为文件已经打开)。

或者是纠正文件以经历一些麻烦的唯一方法?要么:输入文本,检查编码字符串行,如果需要则替换,将每一行写入新文件...;或者导出到csv,然后使用特定编码再次从csv导入,另存为xls?

任何提示表示赞赏。

编辑:

ADODB 对我不起作用(XL 表示用户定义类型,而不是定义)。

我通过解决方法解决了我的问题:

name2 = Replace(name, ".xls", ".txt")
Set wb = Workbooks.Open(name, True, True) ' open read-only
Set ws = wb.Worksheets(1)
ws.SaveAs FileName:=name2, FileFormat:=xlCSV
wb.Close False                    ' close workbook without saving changes
Set wb = Nothing                  ' free memory
Workbooks.OpenText FileName:=name2, _
                   Origin:=65001, _
                   DataType:=xlDelimited, _
                   Comma:=True

I read an Excel 2003 file with a text editor to see some markup language.
When I open the file in Excel it displays incorrect characters. On inspection of the file I see that the encoding is Windows 1252 or some such. If I manually replace this with UTF-8, my file opens fine. Ok, so far so good, I can correct the thing manually.

Now the trick is that this file is generated automatically, that I need to process it automatically (no human interaction) with limited tools on my desktop (no perl or other scripting language).

Is there any simple way to open this XL file in VBA with the correct encoding (and ignore the encoding specified in the file)?

Note, Workbook.ReloadAs does not function for me, it bails out on error (and requires manual action as the file is already open).

Or is the only way to correct the file to go through some hoops? Either: text in, check line for encoding string, replace if required, write each line to new file...; or export to csv, then import from csv again with specific encoding, save as xls?

Any hints appreciated.

EDIT:

ADODB did not work for me (XL says user defined type, not defined).

I solved my problem with a workaround:

name2 = Replace(name, ".xls", ".txt")
Set wb = Workbooks.Open(name, True, True) ' open read-only
Set ws = wb.Worksheets(1)
ws.SaveAs FileName:=name2, FileFormat:=xlCSV
wb.Close False                    ' close workbook without saving changes
Set wb = Nothing                  ' free memory
Workbooks.OpenText FileName:=name2, _
                   Origin:=65001, _
                   DataType:=xlDelimited, _
                   Comma:=True

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩起发的微风 2024-11-17 09:06:48

嗯,我认为您可以从另一个工作簿中完成。添加对 AcitiveX 数据对象的引用,然后添加此子程序:

Sub Encode(ByVal sPath$, Optional SetChar$ = "UTF-8")

    Dim stream As ADODB.stream
    Set stream = New ADODB.stream

    With stream
        .Open
        .LoadFromFile sPath ' Loads a File
        .Charset = SetChar  ' sets stream encoding (UTF-8)
        .SaveToFile sPath, adSaveCreateOverWrite
        .Close
    End With

    Set stream = Nothing
    Workbooks.Open sPath
End Sub

然后使用关闭编码的文件路径调用此子程序。

Well I think you can do it from another workbook. Add a reference to AcitiveX Data Objects, then add this sub:

Sub Encode(ByVal sPath$, Optional SetChar$ = "UTF-8")

    Dim stream As ADODB.stream
    Set stream = New ADODB.stream

    With stream
        .Open
        .LoadFromFile sPath ' Loads a File
        .Charset = SetChar  ' sets stream encoding (UTF-8)
        .SaveToFile sPath, adSaveCreateOverWrite
        .Close
    End With

    Set stream = Nothing
    Workbooks.Open sPath
End Sub

Then call this sub with the path to file with the off encoding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文