close

 

sample code 

 

=begin
Spreadsheet library
Ref. http://spreadsheet.rubyforge.org/GUIDE_txt.html
=end
require 'spreadsheet'

file_name='C:/Ruby192/atm_ruby/practice_myself/access_xls/test2.xls'

=begin
Write data into excel
=end

book = Spreadsheet::Workbook.new(file_name)

##########################################
#
# Create a Worksheet with the Name
#
##########################################
# method 1
sheet1 = book.create_worksheet
sheet1.name = 'My First Worksheet'
# method 2
sheet2 = book.create_worksheet :name => 'My Second Worksheet'

##########################################
#
# Add data into the Worksheet
# Method: 1.concat
# 2.push
# 3.replace
# 4.update_row
##########################################
# About concat->Concatenates Concatenates the given object to str
sheet1.row(0).concat %w{Name Country Acknowlegement}
sheet1[1,0] = 'Japan'
row = sheet1.row(1)
row.push 'Creator of Ruby'
# About: unshift->Prepends objects to the front of self, moving other elements upwards.
row.unshift 'Yukihiro Matsumoto'
sheet1.row(2).replace [ 'Daniel J. Berger', 'U.S.A.',
'Author of original code for Spreadsheet::Excel' ]
sheet1.row(3).push 'Charles Lowe', 'Author of the ruby-ole Library'
sheet1.row(3).insert 1, 'Unknown'

sheet1.update_row 4, 'Hannes Wyss', 'Switzerland', 'Author'
##########################################
#
# Add font format
#
##########################################
sheet1.row(0).height = 18

format = Spreadsheet::Format.new :color => :blue,
:weight => :bold,
:size => 18
sheet1.row(0).default_format = format

bold = Spreadsheet::Format.new :weight => :bold
4.times do |x|
sheet1.row(x + 1).set_format(0, bold)
end

# Write excel
book.write(file_name)

=begin
Parse excel data
=end

book = Spreadsheet.open file_name

# Select worksheet
# method 1
sheet1 = book.worksheet 0
# method 2
sheet2 = book.worksheet 'My Second Worksheet'

sheet1.each do |row|
puts "%s %s\n" % [row[0],row[1]]
end

printf "\n\n\n"

sheet1.each 1 do |row|
puts "%s %s\n" % [row[0],row[1]]
end

 

 

 

font format function 

 

#######################################################
#
# Function: Font format
# Description:
# Parameter: color, weight, size
# Valid values of color -> :black, :red, :white, :cyan, :silver, :blue, :grey, :darkblue
# :lightblue, :orange, :purple, :brown, :yellow, :maroon, :lime,
# :green, :fuchsia and :olive.
# Valid values of weight -> :normal, :bold or any positive Integer.
# Valid values of size -> Any positive Integer.
# Valid values of underline -> :none, :single, :double, :single_accounting and
# :double_accounting.
# Valid value of family -> :none, :roman, :swiss, :modern, :script, :decorativ.
#######################################################
def font_format(f_color, f_weight, f_size)
format = Spreadsheet::Format.new :color => f_color,
:weight => f_weight,
:size => f_size
return format
end

 

$sheet.row(0+$j*16).default_format = font_format(:blue, :bold, 15)
$sheet.row(i+1+$j*16).default_format = font_format(:green, :normal, 10)
$sheet.row(i+2+$j*16).default_format = font_format(:black, :normal, 10)

 

link

#Spreadsheet::Link.new(url='http://www.google.fr',fragment="google")
#origin link string: #'P1'!A1
$title_sheet[$x,0] = Spreadsheet::Link.new url="#'#{elements[0]}'!A1", fragment=elements[0]

 
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 = = 的頭像
    = =

    逗點大的雨滴

    = = 發表在 痞客邦 留言(0) 人氣()