【发布时间】:2014-06-16 09:56:11
【问题描述】:
我一直在尝试编写一些代码来遍历一个目录中的每个 excel 文件,并将 excel 工作簿转换为单个工作表到 CSV 文件。一般来说,我对 ruby 和编码都很陌生,所以我不知道我的解决方案是否是最好的方法。我遇到的问题是 Dir.foreach 似乎没有正确迭代,而是返回“C:\gernericfolder\”。这会导致下一个 win32ole 代码块一大堆问题。
这是我的代码:
require 'rubygems'
require 'iconv'
require 'win32ole'
require 'csv'
require 'roo'
begin
puts("=================================================================================================================================")
inputFolder = ARGV[0]
outputFolder = ARGV[1]
#Check if the file actually exists + UI Feedback
if File.exists?(inputFolder) == false
puts("IGM: DIRECTORY NOT FOUND. Please check your path exists\n")
Process.exit
end
Dir.foreach(inputFolder) { |nextFile|
#Form the file path and open the file
filePath = "#{inputFolder}\\#{nextFile}"
puts("Next file = #{filePath}")
xl = WIN32OLE.new('excel.application')
book = xl.workbooks.open(filePath)
xl.displayalerts = false
end
end
【问题讨论】:
-
很难回答“一大堆问题”,您应该提供实际的堆栈跟踪(您可以将其添加到您的问题中)。毕竟这是 StackOverflow,所以您应该向我们提供有关您的问题的准确信息。 P.S.:请务必提供您的脚本的实际调用(即完整的命令行)。