-
Notifications
You must be signed in to change notification settings - Fork 8
/
README
115 lines (80 loc) · 3.37 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
excel_rails
=================
A Rails plugin to generate xls documents by using rxls templates.
Based on Defv's railsxls plugin (http://github.com/DefV/railsxls) without the Java part.
Also inspired heavily from prawn_rails gem (http://github.com/Volundr/prawn-rails).
The gem has a new cool feature which lets you pick a hand-crafted excel file as a template in your
view. Pass the path to your excel template using the :template_path option and instead of creating
a blank spreadsheet for you to fill, your excel file will be parsed and yielded for you to fill in the blank.
Thanks to Stepan Filatov @ https://github.com/sfilatov
Installation
============
Note that this gem works only with Rails 3
Put the following line in your Gemfile
gem 'excel_rails'
Run 'bundle install'
Name your views as action.xls.rxls (See below for usage)
Make a request to your application with .xls extension to respond to excel.
Dependencies
============
ruby-ole
spreadsheet
iconv
Example
=======
Example spreadsheet usage as found here: http://github.com/jacobat/ruby-spreadsheet
-- in the view "index.xls.rxls"
excel_document(:filename => "all_lines.xls") do |workbook|
sheet = workbook.create_worksheet
sheet.name = "What's in a name"
sheet.row(0).concat %w{Name Country Acknowlegement}
sheet[1,0] = 'Japan'
row = sheet.row(1)
row.push 'Creator of Ruby'
row.unshift 'Yukihiro Matsumoto'
sheet.row(2).replace [ 'Daniel J. Berger', 'U.S.A.',
'Author of original code for Spreadsheet::Excel' ]
sheet.row(3).push 'Charles Lowe', 'Author of the ruby-ole Library'
sheet.row(3).insert 1, 'Unknown'
sheet.update_row 4, 'Hannes Wyss', 'Switzerland', 'Author'
sheet.row(0).height = 18
format = Spreadsheet::Format.new :color => :blue,
:weight => :bold,
:size => 18
sheet.row(0).default_format = format
bold = Spreadsheet::Format.new :weight => :bold
4.times do |x| sheet.row(x + 1).set_format(0, bold) end
end
In the above file excel_document yields a new Spreadsheet:Workbook object to the block.
If you provide a :filename option to the excel_document method, a filename will be provided to the downloaded file.
If your browser supports it you can inline the excel document as well.
If you provide :template_path - path to existed xls file, workbook will be loaded from the file.
excel_document(:filename => "report.xls", :template_path => "/tmp/report_template.xls") do |workbook|
sheet = workbook.worksheet 0
... # any staff form previous example
end
If you provide a :renderer option to the excel_document method, you can use your customized Spreadsheet::Workbook to create a new excel file.
Your view "index.xls.rxls" file could look like this:
excel_document(:filename => "foobar.xls", :renderer => Spreadsheet::CustomWorkbook) do |workbook|
workbook.new_worksheet("#{t_model(Product)}")
end
And the renderer class ("lib/spreadsheet"):
module Spreadsheet
class CustomWorkbook < Spreadsheet::Workbook
attr_accessor :sheet
def new_worksheet
@sheet = workbook.create_worksheet
@sheet.name = "What's in a name"
end
end
end
-- in the controller
def index
@lines = Line.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @lines }
format.xls
end
end
Copyright (c) 2010 Aditya Sanghi, released under the MIT license