Не претендую на точность (код не проверял), но думаю, что должно быть типа такого:
require 'oci8'
require 'spreadsheet'
def build_report(report_name, worksheet name)
Spreadsheet.client_encoding = 'UTF-8'
workbook = Spreadsheet::Workbook.new
worksheet = workbook.create_worksheet name: worksheet name
yeild(worksheet) # выполнение блока внутри do |worksheet|
worksheet.write(report_name)
end
def run
Login = "admin"
Password = "admin"
Server = "admin"
connection = OCI8.new(Login, Password, Server)
cursor = connection.parse("Select Number1, Number2, Number3 From Table1")
cursor.exec
#Создание Excel файла, запись в него данных
build_report( "report_#{Time.now.strftime("%F")}.xls", "Название" ) do |worksheet|
row_num = 0
heading = Spreadsheet::Format.new(:weight => :bold, :size =>10, :border => :thin, :align => :merge, :pattern_fg_color => :yellow, :pattern => 1)
normal = Spreadsheet::Format.new(size: 10)
cursor.fetch_hash do |row|
if row_num == 0
heading_row = worksheet.row(row_num)
heading_row.set_format(0, heading)
heading_row.set_format(1, heading)
heading_row.set_format(2, heading)
descr = row['Number1'].nil? : nil : row['Number1'].encode('utf-8')
worksheet.row(row_num).concat([descr, row['Number2'], row['Number3']])
else
worksheet.row(row_num).concat([row['Number1'], row['Number2'], row['Number3']])
end
row_num += 1
end
worksheet.column(0).width=10
worksheet.column(1).width=55
worksheet.column(2).width=15
end
cursor.close
connection.logoff
end
run
Имеет смысл взять что-то более высокоуровневое, попроще в использовании, из
Object-relational mapping, например тот же
Sequel.
Sequel currently has adapters for ADO, Amalgalite, CUBRID, DataObjects, IBM_DB, JDBC, MySQL, Mysql2, ODBC, Oracle, PostgreSQL, SQLAnywhere, SQLite3, Swift, and TinyTDS.