Mark
Mark

Reputation: 1

csv xls export to excel using respond_to spreadsheet

I want to export results generated from my rails app to excel format. distribution_sheet, results and specimens are all models. This is my script in my controller:

    def first

  @distribution_sheet = DistributionSheet.find(:all, :conditions => ["lifecycle_state = ?","closed"]).last

  @results = @distribution_sheet.results

  @specimens = @distribution_sheet.specimens

 end

  include DisplayResultHelper
    def show
     respond_to do |format|
       format.html
        format.csv {
          @specimens.each do |sp|
         send_data(generate_csv([["Lab No","Assay","Batch","Cuttoff"],[sp.id]]),
 :filename => "my_data-#{Time.now.to_date.to_s}.csv",
         :type => 'text/csv')
          end
                   }
         format.xls{
         send_data(generate_xls([["Lab No","Assay","Batch","Cuttoff"],[]]),
         :filename => 'my_date.xls',
         :type => 'application/vnd.ms-excel')
             }

      end
    end
  end

This works if I add any words inside the arrays, but once I add sp.id it fails. I want to add the data in sp.id. Any clues?

Mark

Upvotes: 0

Views: 1302

Answers (1)

smathy
smathy

Reputation: 27961

I'm not sure which library you're using - but perhaps it requires real strings in those arrays, so try passing in sp.id.to_s instead of just sp.id

Upvotes: 2

Related Questions