使用spreadsheet(只支持xls)
gem 'spreadsheet'
- 首先在 config/initializers/mime_types.rb 中添加
Mime::Type.register "text/excel", :xls
1.导出
- controller中添加
def index
@systems = System.all
respond_to do |format|
format.html
format.xls {send_data(System.exprot_to_xls(@systems),
:type => "text/excel;charset=utf-8; header=present",
:filename => "系统资产-#{Time.now.to_date}.xls")
}
end
end
- model中添加
def self.exprot_to_xls(systems)
xls_report = StringIO.new
Spreadsheet.client_encoding = "UTF-8"
book = Spreadsheet::Wo