-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Maxim Dobryakov
committed
Jul 10, 2013
0 parents
commit 53bf751
Showing
11 changed files
with
254 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
BUNDLE_PATH: vendor/bundle | ||
BUNDLE_DISABLE_SHARED_GEMS: '1' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
*.gem | ||
*.rbc | ||
.config | ||
.yardoc | ||
.idea | ||
Gemfile.lock | ||
InstalledFiles | ||
_yardoc | ||
coverage | ||
doc/ | ||
lib/bundler/man | ||
pkg | ||
rdoc | ||
spec/reports | ||
test/tmp | ||
test/version_tmp | ||
tmp | ||
vendor/bundle/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
source 'https://rubygems.org' | ||
|
||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Copyright (c) 2013 Maxim Dobryakov | ||
|
||
MIT License | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
# write\_xlsx\_rails | ||
|
||
Gem for generate Excel files from Rails views with [write\_xlsx](https://github.com/cxn03651/write_xlsx). | ||
|
||
## Installation | ||
|
||
Add this line to your application's Gemfile: | ||
|
||
gem 'write_xlsx_rails' | ||
|
||
And then execute: | ||
|
||
$ bundle | ||
|
||
Or install it yourself as: | ||
|
||
$ gem install write_xlsx_rails | ||
|
||
## Usage | ||
|
||
write\_xlsx\_rails provides a renderer and a template handler. It adds the :xlsx format and parses .xlsx.wxlsx templates. | ||
|
||
###Controller | ||
|
||
You can either use the typical format: | ||
|
||
```ruby | ||
respond_to do |format| | ||
format.xlsx | ||
end | ||
``` | ||
|
||
or call render directly: | ||
|
||
```ruby | ||
render xlsx: "foobar", filename: "annual-report", disposition: 'inline' | ||
``` | ||
|
||
If you merely want to specify a file name, you can do it one of two ways: | ||
|
||
```ruby | ||
format.xlsx { | ||
response.headers['Content-Disposition'] = 'attachment; filename="my_new_filename.xlsx"' | ||
} | ||
``` | ||
|
||
or | ||
|
||
```ruby | ||
format.xlsx { | ||
render xlsx: "action_or_template", disposition: "attachment", filename: "my_new_filename.xlsx" | ||
} | ||
``` | ||
|
||
> NOTE: Someday it would be nice to merely say something like: | ||
render :filename, 'blah.xlsx" | ||
|
||
###Template | ||
|
||
Use the .xlsx.wxlsx extension in the template, use `workbook` variable, which is set with WriteXLSX.new: | ||
|
||
```ruby | ||
# Add a worksheet | ||
worksheet = workbook.add_worksheet | ||
|
||
# Add and define a format | ||
format = workbook.add_format # Add a format | ||
format.set_bold | ||
format.set_color('red') | ||
format.set_align('center') | ||
|
||
# Write a formatted and unformatted string, row and column notation. | ||
col = row = 0 | ||
worksheet.write(row, col, "Hi Excel!", format) | ||
worksheet.write(1, col, "Hi Excel!") | ||
|
||
# Write a number and a formula using A1 notation | ||
worksheet.write('A3', 1.2345) | ||
worksheet.write('A4', '=SIN(PI()/4)') | ||
``` | ||
|
||
If you use [acts\_as\_xlsx](https://github.com/randym/acts_as_xlsx), configure the active record normally, but specify the package in the template: | ||
|
||
```ruby | ||
User.to_xlsx package: xlsx_package, (other options) | ||
``` | ||
|
||
####Partials | ||
|
||
Partials work as expected: | ||
|
||
```ruby | ||
render partial: 'header', locals: { worksheet: worksheet } | ||
``` | ||
|
||
##Dependencies | ||
|
||
* [write\_xlsx](http://github.com/cxn03651/write_xlsx) | ||
|
||
##Authors | ||
|
||
* [Maxim Dobryakov](https://github.com/maxd) | ||
|
||
## Contributing | ||
|
||
1. Fork it | ||
2. Create your feature branch (`git checkout -b my-new-feature`) | ||
3. Commit your changes (`git commit -am 'Add some feature'`) | ||
4. Push to the branch (`git push origin my-new-feature`) | ||
5. Create new Pull Request | ||
|
||
##Thanks | ||
|
||
* Hideo Nakamura for [write\_xlsx](https://github.com/cxn03651/write_xlsx). | ||
* Noel Peden for [axlsx](https://github.com/randym/axlsx) and [axlsx\_rails](https://github.com/straydogstudio/axlsx_rails). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require "bundler/gem_tasks" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'write_xlsx_rails/version' | ||
require 'write_xlsx_rails/action_controller' | ||
require 'write_xlsx_rails/template_handler' | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require 'action_controller' | ||
|
||
unless defined? Mime::XLSX | ||
Mime::Type.register "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", :xlsx | ||
end | ||
|
||
ActionController::Renderers.add :xlsx do |filename, options| | ||
unless formats.include?(:xlsx) || Rails.version < '3.2' | ||
formats[0] = :xlsx | ||
end | ||
|
||
if filename =~ /^\/([^\/]+)\/(.+)$/ | ||
options[:prefixes][0] = $1 | ||
filename = $2 | ||
end | ||
options[:template] = filename | ||
|
||
disposition = options.delete(:disposition) || 'attachment' | ||
download_name = options.delete(:filename) || filename | ||
download_name += ".xlsx" unless download_name =~ /\.xlsx$/ | ||
|
||
send_data render_to_string(options), filename: download_name, type: Mime::XLSX, disposition: disposition | ||
end | ||
|
||
# For respond_to default | ||
class ActionController::Responder | ||
def to_xlsx | ||
controller.render xlsx: controller.action_name | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require 'action_view' | ||
|
||
module WriteXlsxRails | ||
class WriteXlsxBuilder | ||
|
||
def default_format | ||
Mime::XLSX | ||
end | ||
|
||
def self.call(template) | ||
<<-TEMPLATE | ||
require 'stringio' | ||
__io = StringIO.new | ||
workbook = WriteXLSX.new(__io) | ||
#{template.source} | ||
workbook.close | ||
__io.flush | ||
__io.string | ||
TEMPLATE | ||
end | ||
|
||
end | ||
end | ||
|
||
ActionView::Template.register_template_handler :wxlsx, WriteXlsxRails::WriteXlsxBuilder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module WriteXlsxRails | ||
VERSION = "0.0.1" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# coding: utf-8 | ||
lib = File.expand_path('../lib', __FILE__) | ||
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | ||
require 'write_xlsx_rails/version' | ||
|
||
Gem::Specification.new do |spec| | ||
spec.name = 'write_xlsx_rails' | ||
spec.version = WriteXlsxRails::VERSION | ||
spec.authors = ['Maxim Dobryakov'] | ||
spec.email = ['maxim.dobryakov@gmail.com'] | ||
spec.description = 'xlsx renderer for Rails base on write_xlsx gem' | ||
spec.summary = 'xlsx renderer for Rails' | ||
spec.homepage = 'https://github.com/maxd/write_xlsx_rails' | ||
spec.license = 'MIT' | ||
|
||
spec.files = `git ls-files`.split($/) | ||
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } | ||
spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) | ||
spec.require_paths = ['lib'] | ||
|
||
spec.add_dependency 'write_xlsx' | ||
|
||
spec.add_development_dependency 'bundler', '~> 1.3' | ||
spec.add_development_dependency 'rake' | ||
end |