2011-04-18 39 views
0

我想將從我的rails應用程序生成的結果導出爲ex​​cel格式。 distribution_sheet,結果和標本都是模型。這是我的控制器中的腳本:csv xls使用respond_to電子表格導出爲ex​​cel

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 

如果我在數組中添加任何單詞,但是一旦添加sp.id,它就會失敗。我想在sp.id中添加數據。任何線索?

馬克

+0

,而不是你寫的'generate_xls'和'generate_csv'?或者他們來自圖書館?要麼向我們展示這些函數的來源,要麼說出它來自哪個庫。現在我們很難知道這些功能的期望。 – nathanvda 2011-04-19 08:48:24

回答

2

我不知道你使用的庫 - 但也許它需要在這些陣列實際字符串,所以儘量在sp.id.to_s傳遞的只是sp.id