Class: Spreet::Handlers::ExcelCSV

Inherits:
Base
  • Object
show all
Defined in:
lib/spreet/handlers/excel_csv.rb

Class Method Summary collapse

Class Method Details

.read(file, options = {}) ⇒ Object

Read a CSV file and create its Spreet document



9
10
11
12
13
14
15
16
17
# File 'lib/spreet/handlers/excel_csv.rb', line 9

def self.read(file, options={})
  spreet = Spreet::Document.new
  sheet = spreet.sheets.add
  options = {:col_sep=>';', :encoding => "CP1252"}.merge(options)
  ::CSV.foreach(file, options) do |row|
    sheet.row *(row.map{|v| v.to_s.encode('utf-8')}) # collect{|v| v.to_s.encode('cp1252')}
  end
  return spreet
end

.write(spreet, file, options = {}) ⇒ Object

Write a Spreet to a CSV file



21
22
23
24
25
26
27
28
29
# File 'lib/spreet/handlers/excel_csv.rb', line 21

def self.write(spreet, file, options={})
  sheet = spreet.sheets[options[:sheet]||0]
  options = {:col_sep=>';', :encoding => "CP1252"}.merge(options)
  ::CSV.open(file, "wb", options) do |csv|
    sheet.each_row do |row|
      csv << row.map(&:text)
    end
  end
end