ruby将mysql查询到的数据保存到excel

2017-01-12 19:03:57来源:cnblogs.com作者:Fithon人点击

require "win32ole" require 'pathname'require 'mysql2'excel = WIN32OLE.new('excel.application')  excel.visible = true     # in case you want to see what happensclient = Mysql2::Client.new(:host => "localhost",                            :username => "root",                            :password => "pwd",                            :port =>"3306",                            :database => "mop6"                            )#获取win下当前执行脚本的路径$pwd = Pathname.new(__FILE__).realpath.dirname.to_s.gsub('/','//')workbook = excel.workbooks.addworkbook.saveas("#{$pwd}//sqlresult.xlsx")workSheet = workbook.Worksheets(1) sql = "SELECT * FROM epg_media_publish_epgs_66_test2utv limit 3;"results = client.query(sql)columnName = ['idx','column_id','media_id','sort','status','operator_id','order_date','pub_date']#设置a1:h1的颜色workSheet.Range('a1:h1').Interior.ColorIndex = 36workSheet.Range("a1:h1").Value = columnNamenum = 2results.each(:as => :array) do |row|    workSheet.Range("a#{num}:h#{num}").Value = row    num += 1endworkbook.saveworkbook.closeexcel.Quit


------------------------------------------------------------------------------
excel数据效果:

 

 

 

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台