rails 后台创建Excel(导出数据库中的表结构)

客户要求一份项目的 db 结构 ,只需表结构,以Excel形式,每个表一个sheet

从后台写了部分代码:设计Excel的创建 下载

def export_table_info_excel

    Spreadsheet.client_encoding = 'UTF-8'
    book = Spreadsheet::Workbook.new
    sheet1 = book.create_worksheet :name => "all tables"
    tables = User.find_by_sql "select table_name from information_schema.tables  WHERE table_schema = 'radiq_development'"
    sheet1.column(0).width = 10
    sheet1.column(1).width = 80

    sheet1.insert_row(0,['序號','表名'])
    sheet1.rows[0].set_format(0, @@header_format)
    sheet1.rows[0].set_format(1, @@header_format)
    index= 1
    tables.each  do |table|
      #sheet =  book.create_worksheet :name => table.table_name

        sql =  "SELECT
                  COLUMN_NAME,
                  COLUMN_TYPE,
                  COLUMN_KEY,
                  COLUMN_COMMENT
                FROM
                  information_schema.`COLUMNS`
                WHERE
                  TABLE_SCHEMA = 'radiq_development'
                AND TABLE_NAME = '#{table.table_name}'"
        infos = User.find_by_sql  sql

        sheet =  book.create_worksheet :name => table.table_name
        sheet.insert_row(0,["(#{table.table_name})"])
        sheet.rows[0].set_format(0, @@format)
        sheet.rows[0].set_format(1, @@format)
        sheet.merge_cells(0, 0, 0, 3)
        sheet.column(0).width = 30
        sheet.column(1).width = 30
        sheet.column(2).width = 10
        sheet.column(3).width = 40
        sheet.insert_row(1,['Column Name','Type','Key','Description'])
        sheet.rows[1].set_format(0, @@header_format)
        sheet.rows[1].set_format(1, @@header_format)
        sheet.rows[1].set_format(2, @@header_format)
        sheet.rows[1].set_format(3, @@header_format)
        count = 1
         infos.each do|info|
           count +=1
           sheet.insert_row(count,[info.COLUMN_NAME,info.COLUMN_TYPE,info.COLUMN_KEY=='PRI'? 'PK':'',info.COLUMN_COMMENT])
           sheet.rows[count].set_format(0, @@format)
           sheet.rows[count].set_format(1, @@format)
           sheet.rows[count].set_format(2, @@format)
           sheet.rows[count].set_format(3, @@format)
         end

        autofit_high sheet

      sheet1.insert_row(index,[index,"#{table.table_name}"])
      sheet1.rows[index].set_format(0, @@format)
      sheet1.rows[index].set_format(1, @@format)
      index+=1
    end
    autofit_high sheet1
    book.write "#{Rails.root.to_s}#{EXPORT_BETWEEN_POINT_REPORT_PATH}schema.xls"

    send_file("#{Rails.root.to_s + EXPORT_BETWEEN_POINT_REPORT_PATH}schema.xls",
              :type => 'text/csv; charset=utf-8; header=present', :disposition => "attachment",
              :stream => true, :buffer_size => 4096, :filename => "schema.xls")


  end


@@font_name = Spreadsheet::Font.new client("PMingLiU", 'UTF-8'), :size => 22, :family => :swiss, :weight => :bold
@@font_header = Spreadsheet::Font.new client("PMingLiU", 'UTF-8'), :size => 16, :family => :swiss, :weight => :bold, :color => :white
@@font_content = Spreadsheet::Font.new client("PMingLiU", 'UTF-8'), :size => 14, :family => :swiss
@@font_foot = Spreadsheet::Font.new client("PMingLiU", 'UTF-8'), :size => 16, :family => :swiss

@@format = Spreadsheet::Format.new :vertical_align => :middle, :font => @@font_content,
                                   :align => :center,
                                   :bottom => :thin, :top => :thin, :left => :thin, :right => :thin, :align => :center
@@name_format = Spreadsheet::Format.new :vertical_align => :middle, :align => :center, :font => @@font_name
@@header_format = Spreadsheet::Format.new :pattern => 1, :vertical_align => :middle, :font => @@font_header, :align => :center,
                                          :pattern_fg_color => :green


其中包括了 样式,sheet等,直接在地址栏 输入url即可下载此excel
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值