Ruby saves the data queried by mysql to excel and rubymysql
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") # obtain the path of the script currently executed under 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'] # sets the workSheet color of a1: h1. 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 Data effect: