lib/exporters/excel_exporter.rb in datashift-0.9.0 vs lib/exporters/excel_exporter.rb in datashift-0.10.0

- old
+ new

@@ -10,110 +10,95 @@ # module DataShift require 'exporter_base' - if(Guards::jruby?) + require 'excel' - require 'jruby/jexcel_file' + class ExcelExporter < ExporterBase - class ExcelExporter < ExporterBase - - attr_accessor :filename + attr_accessor :filename - def initialize(filename) - @filename = filename - end + def initialize(filename) + @filename = filename + end - # Create an Excel file from list of ActiveRecord objects - def export(records, options = {}) + # Create an Excel file from list of ActiveRecord objects + def export(records, options = {}) - raise ArgumentError.new('Please supply array of records to export') unless records.is_a? Array + raise ArgumentError.new('Please supply array of records to export') unless records.is_a? Array - excel = JExcelFile.new() + excel = Excel.new - if(options[:sheet_name] ) - excel.create_sheet( options[:sheet_name] ) - else - excel.create_sheet( records.first.class.name ) - end + if(options[:sheet_name] ) + excel.create_worksheet( :name => options[:sheet_name] ) + else + excel.create_worksheet( :name => records.first.class.name ) + end - excel.ar_to_headers(records) + excel.ar_to_headers(records) - excel.ar_to_xls(records) - - - # => :methods => List of methods to additionally export on each record - - excel.save( filename() ) - end + excel.ar_to_xls(records) + + excel.write( filename() ) + end - # Create an Excel file from list of ActiveRecord objects - # Specify which associations to export via :with or :exclude - # Possible values are : [:assignment, :belongs_to, :has_one, :has_many] - # - def export_with_associations(klass, items, options = {}) + # Create an Excel file from list of ActiveRecord objects + # Specify which associations to export via :with or :exclude + # Possible values are : [:assignment, :belongs_to, :has_one, :has_many] + # + def export_with_associations(klass, items, options = {}) - excel = JExcelFile.new() + excel = Excel.new - if(options[:sheet_name] ) - excel.create_sheet( options[:sheet_name] ) - else - excel.create_sheet( items.first.class.name ) - end + if(options[:sheet_name] ) + excel.create_worksheet( :name => options[:sheet_name] ) + else + excel.create_worksheet( :name => items.first.class.name ) + end - MethodDictionary.find_operators( klass ) + MethodDictionary.find_operators( klass ) - MethodDictionary.build_method_details( klass ) + MethodDictionary.build_method_details( klass ) - work_list = options[:with] || MethodDetail::supported_types_enum + work_list = options[:with] || MethodDetail::supported_types_enum - headers = [] - puts "work_list : [#{work_list.inspect}]" + headers = [] - details_mgr = MethodDictionary.method_details_mgrs[klass] - - - data = [] - # For each type belongs has_one, has_many etc find the operators - # and create headers, then for each record call those operators - work_list.each do |op_type| + details_mgr = MethodDictionary.method_details_mgrs[klass] + + data = [] + # For each type belongs has_one, has_many etc find the operators + # and create headers, then for each record call those operators + work_list.each do |op_type| - list_for_class_and_op = details_mgr.get_list(op_type) + list_for_class_and_op = details_mgr.get_list(op_type) - next if(list_for_class_and_op.nil? || list_for_class_and_op.empty?) + next if(list_for_class_and_op.nil? || list_for_class_and_op.empty?) - # method_details = MethodDictionary.send("#{mdtype}_for", klass) + # method_details = MethodDictionary.send("#{mdtype}_for", klass) - list_for_class_and_op.each do |md| - headers << "#{md.operator}" - items.each do |i| - data << i.send( md.operator ) - end - + list_for_class_and_op.each do |md| + headers << "#{md.operator}" + items.each do |i| + data << i.send( md.operator ) end + + end - excel.set_headers( headers ) + excel.set_headers( headers ) - row_index = 1 + row = 1 + column = 0 - items.each do |datum| - excel.create_row(row_index += 1) - excel.ar_to_xls_row(1, datum) - end - - excel.save( filename() ) + items.each do |row_of_data| + excel.ar_to_xls_row(row, column, row_of_data) + row += 1 end + + excel.write( filename() ) end - end # ExcelGenerator - - else - class ExcelExporter < ExporterBase - - def initialize(filename) - raise DataShift::BadRuby, "Apologies but DataShift Excel facilities currently need JRuby. Please switch to, or install JRuby" - end end - end # jruby + end # ExcelGenerator end # DataShift \ No newline at end of file