ASP源码.NET源码PHP源码JSP源码JAVA源码DELPHI源码PB源码VC源码VB源码Android源码
当前位置:首页 >> 网络编程 >> Ruby教程 >> ruby将mysql查询到的数据保存到excel

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

来源:网络整理     时间:2017-01-14     关键词:

本篇文章主要介绍了" ruby将mysql查询到的数据保存到excel",主要涉及到方面的内容,对于Ruby教程感兴趣的同学可以参考一下: require "win32ole"require 'pathname' require 'mysql2'excel = WIN32OLE.new('excel...

require "win32ole"require 'pathname'
require 'mysql2'excel = WIN32OLE.new('excel.application')  
excel.visible = true     # in case you want to see what happens
client = 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.add
workbook.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 = 36
workSheet.Range("a1:h1").Value = columnName
num = 2
results.each(:as => :array) do |row|
    workSheet.Range("a#{num}:h#{num}").Value = row
    num += 1
end

workbook.save
workbook.close
excel.Quit
------------------------------------------------------------------------------
excel数据效果:
 ruby将mysql查询到的数据保存到excel
 

以上就介绍了 ruby将mysql查询到的数据保存到excel,包括了方面的内容,希望对Ruby教程有兴趣的朋友有所帮助。

本文网址链接:http://www.codes51.com/article/detail_4195211.html

相关图片

相关文章