Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Print all Excel files
#! ruby -Ks
require 'win32ole'
# usage
# ruby xls_printer.rb (対象ディレクトリ:省略時はカレントディレクトリを対象にする)
module DirectoryDigger
def getAbsolutePath(filename)
@fso = WIN32OLE.new('Scripting.FileSystemObject')
return @fso.GetAbsolutePathName(filename)
end
def dir_iter(item, &block)
path = item[:path]
regexp = item[:regexp]
unless(path.nil?)
if(File.directory?(path))
Dir.new(path).each do |child_path|
new_path = "#{path}/#{child_path}"
if(child_path !~ /^\.*$/)
dir_iter({:path => new_path}, &block)
else
end
end
elsif(File.file?(path))
if(regexp.nil?)
yield path
else
yield path if(regexp =~ path)
end
else
puts "raise"
raise path
end
end
end
end
class XlsPrinter
include DirectoryDigger
def initialize
@xl = WIN32OLE.new('Excel.Application')
@xl.DisplayAlerts = false
@xl.Visible = true
end
def print_book(path)
book = @xl.Workbooks.Open(path)
book.Worksheets.select
book.PrintOut
book.Close
end
def print_all(item)
target_path = item[:path]
regexp = item[:regexp]
target_path = Dir.pwd if(target_path.nil?)
regexp = /./ if(regexp.nil?)
dir_iter(:path => target_path, :regexp => regexp) do |path|
begin
print_book(path)
puts "printed #{path}"
rescue => e
raise "#{$@} #{e}"
end
end
end
end
p = XlsPrinter.new
p.print_all(:path => ARGV[0], :regexp => /\.xls$/)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.