如何使用Excel VBA确定文件编码类型 [英] How to determine file encoding type with Excel VBA

查看:78
本文介绍了如何使用Excel VBA确定文件编码类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个Excel/VBA工具来验证csv文件,以确保它们包含的数据有效.它们的csv可以来自任何地方(来自功能完善的Unix系统或从Excel中保存数据的桌面用户).Excel工具已发送给企业,因此他们可以在自己的环境中验证其csv文件,而不必承担数据离开系统的风险.因此,该解决方案需要在本机VBA中,并且不能链接到外部库中.

I have built an Excel/VBA tool to validate csv files to ensure the data they contain is valid. They csv can come originate from anywhere (from a full blown unix system or a desktop user saving data out from Excel). The Excel tool is sent out to businesses so they can validate their csv files in their own environment and without taking the risk of their data leaving thier systems. Thus, the solution needs to be in native VBA and not link into external libraries.

因此,使用VBA,我需要能够自动检测UTF-8(带有或不带有BOM)或ANSI文件编码,并警告用户这些不是用于csv的文件编码.

So using VBA, I need to be able to automatically detect UTF-8 (with or without BOM) or ANSI file encodings and warn the user if these are not the file encodings used for the csv.

我认为这可能涉及从文件开头读取几个字节并根据字节顺序标记的存在确定编码.

I think this would perhaps involve reading in a few bytes from the start of the file and determining the encoding based on the existance of the byte order mark.

您能帮助我以正确的方式开始吗?

Could you help me get me started on the right track?

推荐答案

假设您可以自由地要求用户选择正确的文件类型,让他们对选择文件的方式负责;)

Assuming you have the freedom to ask user to choose the correct file type, making them responsible for what they choose as a file ;)

这意味着,您可以创建一个表单,用户可以在其中选择文件名和编码类型,就像我们在打开文件向导中所做的那样.

That means, you can create a form where users can choose the filename and the encoding type like how we do on file open wizard.

其他,

我建议您使用 FileSystemObject .它返回一个 TextStream ,可用于确定编码.我怀疑VBA是否支持其他类型的编码,如果可以,请更正:),并且很高兴听到.:)

I suggest you to use the FileSystemObject. It returns a TextStream which can be utilized to determine the encoding. I doubt VBA supports other types of encoding and please correct me if it does :) and happy to hear. :)

这里是进一步考虑的链接:-

Here is a link for further considerations:-

这篇关于如何使用Excel VBA确定文件编码类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆