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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章