国产一区二区精品久久_蜜桃狠狠狠狠狠狠狠狠狠_午夜视频精品_激情都市一区二区

當(dāng)前位置:首頁 > 網(wǎng)站舊欄目 > 學(xué)習(xí)園地 > 設(shè)計軟件教程 > 每天一劑Rails良藥之Rendering CSV From Your Actions

每天一劑Rails良藥之Rendering CSV From Your Actions
2010-01-14 07:05:42  作者:  來源:
有時候我們需要輸出Comma  Separated Values(CSV)等各種形式的輸出來滿足用戶的需要:
Java代碼 復(fù)制代碼
  1. class ExportController < Application ontroller   
  2.   def orders   
  3.     content_type = if request.user_agent =~ /windows/i   
  4.                      'application/vnd.ms-excel'  
  5.                    else  
  6.                      'text/csv'  
  7.                    end   
  8.   
  9.     CSV::Writer.generate(output = ""do |csv|   
  10.       Order.find(:all).each do |order|   
  11.         csv << [order.id, order.price, order.purchaser, order.created_at]   
  12.       end   
  13.     end   
  14.   
  15.     send_data(output, :type=> content_type, :filename => "orders.csv")   
  16.   end   
  17. end  

安徽新華電腦學(xué)校專業(yè)職業(yè)規(guī)劃師為你提供更多幫助【在線咨詢