class CsvXlsxConverter::CsvToXlsx

Public Class Methods

new(input_file) click to toggle source
# File lib/csv_xlsx_converter/converter.rb, line 8
def initialize(input_file)
  raise ArgumentError, "input file is not csv" unless CsvXlsxConverter::csv_filename? input_file
  @input_file = input_file
end

Public Instance Methods

convert(output_file) click to toggle source
# File lib/csv_xlsx_converter/converter.rb, line 13
def convert(output_file)
  raise ArgumentError, "output file is not xlsx" unless CsvXlsxConverter::xlsx_filename? output_file

  workbook = RubyXL::Workbook.new
  worksheet = workbook[0]

  options = {:encoding => 'bom|UTF-8', :skip_blanks => true}
  CSV.foreach(@input_file, options).each_with_index do |row, row_idx|
    # http://stackoverflow.com/questions/12407035/ruby-csv-get-current-line-row-number
    row.each_with_index do |item, index|
      worksheet.add_cell row_idx, index, item
    end
  end

  workbook.write output_file
end