Ruby 操作 WPS 表格时遇到的中文文件名比较的问题

africwildman · 2014年10月27日 · 最后由 africwildman 回复于 2014年10月27日 · 2218 次阅读

win7 系统,ruby2.1.3。 有个文件叫"答题卡.xls",要确认这个文件是否已经打开,写了如下代码:

require 'win32ole' et=WIN32OLE.connect "et.application" wb=et.Workbooks wb.each{ |w| puts "ok" if w.name=="答题卡.xls" }

确认文件打开后 w.name=="答题卡.xls" 不能返回 true,改成 w.name=~/答题卡.xls/ 出现如下错误: clean2.rb:4:in block in <main>': incompatible encoding regexp match (UTF-8 rege xp with GBK string) (Encoding::CompatibilityError) from clean2.rb:4:ineach' from clean2.rb:4:in `'

我看着是文件系统 GBK 编码跟 ruby2 默认 utf8 冲突问题,怎么解决啊?我目前是把"答题卡.xls"改成英文名。

require 'win32ole'
WIN32OLE.codepage = WIN32OLE::CP_UTF8
et=WIN32OLE.connect "et.application"
# utf8 code

保存代码文件的时候要用 utf8 no bom.

#1 楼 @lyfi2003 通过了,非常感谢!

需要 登录 后方可回复, 如果你还没有账号请 注册新账号