使用win32ole的Ruby程序在Windows7 64位下不能工作
我有一个古老的ruby程序,从Excel文件中提取值并将汇总存储在另一个Excel文件中。 为此,程序使用Ruby的库win32ole。 切换到Windows 7 64位(而不是Windows XP 32位),Office 2007而不是Office 2003的新计算机后,程序现在在存储生成的Excel文件时引发错误:
ana.rb:120:in `method_missing': SaveAs (WIN32OLERuntimeError) OLE error code:800A03EC in Microsoft Office Excel 'c:/my/dir' could not be accessed. The file could be corrupt, is on a server that does not react, or the file is write protected. (German: Auf 'c:/my/dir' konnte nicht zugegriffen werden. Unter Umstaenden ist die Datei beschaedigt, befindet sich auf einem Server, der nicht mehr reagiert, oder die Datei ist schreibgeschuzetzt.) HRESULT error code:0x80020009 Ausnahmefehler aufgetreten. from ana.rb:120:in `save' from ana.rb:54:in `generateReport' from ana.rb:13:in `ana' from ana.rb:191
该计划的相关部分是:
def generateReport ... report.save(basicdir + reportfile) ... end
与报告:
class EasyExcel def initialize(path) @path = path @excel = excel = WIN32OLE.new("excel.application") @workbook = @excel.Application.Workbooks.Open(@path) @cache = Array.new end def save(filename) saveCache @workbook.SaveAs(filename) end
第120行是@workbook.SaveAs(filename)
。 那个时候filename
的值是c:/projekte/itcampus/feedback-analyse/feedback_report.xls
。 经过一些debugging之后,我发现由于rubyexception处理不好,在ruby解释器停止之后,有两个excel挂起。 所以这个问题似乎是由于Windows 7中Excel中处理path的变化所致。
有没有人知道以下问题的答案:
- 什么可能是失败的原因:64位而不是32位,使用Office 2007而不是2003,或两者兼而有之?
- 有没有解决方法或修复使用桥接到Windows 7 64位和应用程序,如Word或从Ruby的Excel?
- 如何从Ruby的Windows应用程序中find哪些API可用?
我试过的Ruby解释器是:
- ruby1.8.7(2011-02-18 patchlevel 334)[i386-mingw32]
- ruby1.9.2p180(2011-02-18)[i386-mingw32]
感谢所有为我的问题添加了想法和意见的人。 最后,我find了一个解决方法。
class EasyExcel .... def save(filename) saveCache dos_file = filename.gsub(/\//, "\\\\") @workbook.SaveAs(filename) end
这将在每个正斜杠(ruby)中用两个反斜杠代替,然后在最后评估为1个反斜杠。
所以打开一个excel
@workbook = @excel.Application.Workbooks.Open(@path)
(用@path
类似
C:/projekte/itcampus/feedback-analyse/feedback/Bewertungsbogen_XX1404.xls
)的作品,但是
@workbook.SaveAs("c:/projekte/itcampus/feedback-analyse/feedback_report.xls")
才不是。 很奇怪!
我在Windows Server 2008(64位)上遇到了类似的问题,使用filename.gsub(/\//, "\\\\")
和expand_path
的解决scheme没有帮助。
当我用我的用户调用它,它的工作,相同的程序在后台进程中的OLE错误。
解决scheme(我不是在开玩笑,它的工作):创build一个文件夹C:\Windows\SysWOW64\config\systemprofile\Desktop
。
对于Windows 2008 Server x64:创build以下目录:
C:\ WINDOWS \ Syswow64资料\ CONFIG \ systemprofile \桌面
对于Windows 2008 Server x86:创build以下目录:
C:\ WINDOWS \ system32 \设置\ systemprofile \桌面
而已!! 瞧! 你们都准备好了
具有类似详细信息的替代链接: https : //social.msdn.microsoft.com/Forums/en-US/b81a3c4e-62db-488b-af06-44421818ef91/excel-2007-automation-on-top-of-a-windows-服务器-2008-64?论坛= innovateonoffice
使用COM和切换到Windows 7时遇到的许多问题都与用户权限有关。 你有没有尝试用pipe理员权限运行你的程序?