简体   繁体   中英

Unable to convert .xls to .csv using Roo

Goal for my code is to process all the .xls files in a directory and convert them all to .csv. This has worked in the past but after updating Ruby and Roo it is showing the error below. I am using Ruby 1.9.3-p362 and Roo 1.11.2.

require 'rubygems'
require 'csv'
require 'iconv'
require 'mysql2'
require 'mysql'
require 'roo'

begin
    Dir["#{@work_path}/*.xls"].each do |file|  
        begin
          file_path = "#{file}"
          file_basename = File.basename(file, ".xls")
          xls = Excel.new(file_path)
          xls.to_csv("#{@kenshoo_work_path}#{file_basename}.csv")
          @log.info("Converted file #{file}")
          FileUtils.remove(file)
          @log.info("Deleted file #{file}")
        rescue Exception => e
          @log.warn("Unable to convert file: #{file_basename} into csv because #{e.message}")
        end
    end
end

For every file I get the following error message:

Unable to convert file: #{file} into csv because uninitialized constant Excel

Any help is appreciated. Thanks.

roo has Excel class within the Roo:: namespace. You need to replace all instances of Excel with Roo::Excel

The older versions did not namespace classes under Roo:: , so that would explain why your code broke after update.

The new repository is here: https://github.com/Empact/roo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM