rails 利用 Spreadsheet 导出xls格式数据

 

 

 1、链接
  XXX_path(format: :xls)
 2、Action
  def opt...
     send_data(xls_content_for(@opts),
              :type => "text/excel;charset=utf-8; header=present",
              :filename => "export_opts.xls")
  end
 3、生成xls格式

  private

  def xls_content_for(objs)
    xls_report = StringIO.new
    book = Spreadsheet::Workbook.new
    sheet1 = book.create_worksheet :name => "Operations"
    title_style = Spreadsheet::Format.new :weight => :bold, :size => 12
    sheet1.row(0).default_format = title_style
    sheet1.row(0).concat %w{Controller Action ....}
    count_row = 1
    objs.each do |obj|
      sheet1[count_row, 0]=obj.controller
      sheet1[count_row, 1]=obj.action
      ......
      count_row += 1
    end

    book.write xls_report
    xls_report.string
  end

 

你可能感兴趣的:(Spread)