Skip to content

maxd/write_xlsx_rails

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

write_xlsx_rails

Gem for generate Excel files from Rails views with 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:

respond_to do |format|
  format.xlsx
end

or call render directly:

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:

format.xlsx {
	response.headers['Content-Disposition'] = 'attachment; filename="my_new_filename.xlsx"'
}

or

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:

# 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, configure the active record normally, but specify the package in the template:

User.to_xlsx package: xlsx_package, (other options)

####Partials

Partials work as expected:

render partial: 'header', locals: { workbook: workbook }

NOTE: Every partial instantiate own workbook variable. So, it can cause problems. To prevent this just pass workbook as local variable to the partial.

##Dependencies

##Authors

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

About

xlsx renderer for Rails base on write_xlsx gem

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages