|
|
生成支持中文的excel表格
首先 安装gem
gem install spreadsheet # -excel-0.3.5.1
class ProductController < ApplicationController require 'win32ole' require "spreadsheet/excel" require "iconv" def get_excel catalogs = Product.find(:all) report_arr = [%w{product_number new_product product_name scale_size}] # report_arr =["姓名" ,"编号","邮箱"] report_arr[0] = ["产品编号" ,"新产品编号","产品名称","规格尺寸"] catalogs.each do |c| report_arr << [c.product_number,c.new_product,c.product_name,c.scale_size,] end now = Time.now.to_s(:db) ic = Iconv.new("gbk","utf-8") workbook = Excel.new("#{RAILS_ROOT}/public/reports/catalogs.xls") worksheet = workbook.add_worksheet("Report of catalogs") worksheet.write(0, 0, "#{now}") # worksheet.write(1, 0, ic.iconv(conditions.join(","))) report_arr.each_with_index do |row, row_idx| row.each_with_index do |cell, col_idx| worksheet.write(row_idx+2, col_idx, ic.iconv(cell)) end end workbook.close @msg = "#{now}<br/>导出完毕。" render :layout => true endend 这些代码不用任何修改 你可以直接使用,呵呵 当然你必须有数据库.
下面是我测试的栏数据,大家原谅,这个方法绝对是支持中文的. |
|