A Rails plugin to provide templates for the axlsx gem
A Rails plugin to provide templates for the axlsx gem
This gem has been renamed to match other gems in the Axlsx community organization: https://github.com/caxlsx
To better maintain the Axlsx ecosystem, all related gems have been forked or moved to the following community organization:
In your Gemfile:
gem 'caxlsx'
gem 'caxlsx_rails'See previous installations if needed.
Axlsx-Rails provides a renderer and a template handler. It adds the :xlsx format and parses .xlsx.axlsx templates. This lets you take all the caxlsx code out of your controller or model and place it inside the template, where view code belongs! See this blog post for a more complete walkthrough.
To use Axlsx-Rails set your instance variables in your controller and configure the response if needed:
class ButtonController 'cover_sheet', :locals => {:wb => wb}
wb.add_worksheet(name: "Content") do |sheet|
sheet.add_row ['Content']
endWith the partial simply using the passed variables:
wb.add_worksheet(name: "Cover Sheet") do |sheet|
sheet.add_row ['Cover', 'Sheet']
endTo use an xlsx template to render a mail attachment, use the following syntax:
…:xlsx format you do not need to specify formats or handlers.users/export) can refer to only one file (the xlsx.axlsx template), you do not need to specify handlers, provided the formats includes :xlsx.To generate a template within a script, you need to instantiate an ActionView context. Here are two gists showing how to perform this:
There is no built-in way to test your resulting workbooks / templates. But here is a piece of code that may help you to find a way.
………It is easy to get the spelling wrong in the extension name, the format.xlsx statement, or in a render call. Here are some possibilities:
.xlsx.axlsx.uninitialized constant Mime::XSLX you have used format.xslx instead of format.xlsx, or something similar.In API mode Rails does not include ActionView, so axlsx_rails will not work. To render axlsx_rails templates you must include ActionView::Rendering in your controller and override render_to_body:
class MyController < ActionController::API
include ActionView::Rendering
def show
respond_to do |format|
format.xlsx
end
end
private
def render_to_body(options)
_render_to_body_with_renderer(options) || super
end
endSee issue 107
If you are having problems with rendering a template and attaching it to a template, try a few options:
self.instance_variable_set(:@_lookup_context, nil). If you must do this, please open an issue.encoding: 'base64' with the attachment:class UserMailer < ActionMailer::Base
def export(users)
xlsx = render_to_string handlers: [:axlsx], formats: [:xlsx], template: "users/export", locals: {users: users}
attachments["Users.xlsx"] = {mime_type: Mime[:xlsx], content: xlsx, encoding: 'base64'}
# For Rails 4 use Mime::XLSX
# attachments["Users.xlsx"] = {mime_type: Mime::XLSX, content: xlsx, encoding: 'base64'}
# self.instance_variable_set(:@_lookup_context, nil) # If attachments are rendered as content, try this and open an issue
...
end
endIf you get these errors, please open an issue and share code so the bug can be isolated. Or comment on issue #29 or #25.
Have you followed other tutorials for serving Excel from Rails? Have you declared the MIME type already? These may be incompatible with caxlsx_rails (which declares the MIME type for you.) Remove any vestiges of other tutorials from your code and try again.
Both these errors appear to be caused by Rails applying a layout to the template. Passing layout: false to render :xlsx should fix this issue. Version 0.5.0 attempts to fix this issue.
If you get this error, please open an issue and share code so the bug can be isolated.
Before Rails 4.2 you could call:
render xlsx: "users/index"And caxlsx_rails could adjust the paths and make sure the template was loaded from the right directory. This is no longer possible because the paths are cached between requests for a given controller. As a result, to display a template in another directory you must use the :template parameter (which is normal Rails behavior anyway):
render xlsx: "index", template: "users/index"If the request format matches you should be able to call:
render "users/index"This is a breaking change if you have the old syntax!
If you are using turbolinks, you may need to disable turbolinks when you link to your spreadsheet:
# turbolinks 5:
link_to 'Download spreadsheet', path_to_sheet, data: {turbolinks: false}In Rails 7, if you get an error of "Unknown Format" you may need to add (format: "xlsx") to the named route.
A clue to the "format" that Rails is responding with is to look at your log file or console after you click the HTML link or submit your form. You should see Processing by Controller#action as XLSX.
Scenario 1 - HTML Link within index.html.erb
link_to "Download as Excel", my_named_route_path(format: "xlsx")Scenario 2 - Using form_with()
form_with method: :post, url: my_named_route_path(format: :xlsx) do |f|If you are having problems, try to isolate the issue. Use the console or a script to make sure your data is good. Then create the spreadsheet line by line without Axlsx-Rails to see if you are having caxlsx problems. If you can manually create the spreadsheet, create an issue and we will work it out.
Many thanks to contributors:
No open issues yet, or sync has not completed.