Skip to content

Instantly share code, notes, and snippets.

@ream88
Created July 19, 2011 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ream88/1094013 to your computer and use it in GitHub Desktop.
Save ream88/1094013 to your computer and use it in GitHub Desktop.
writeexcel Template handler (fileextension is .writeexcel) for Rails 3.0
require 'action_dispatch/http/mime_type'
require 'action_view'
require 'writeexcel'
class WriteExcelTemplateHandler < ActionView::TemplateHandler
include ActionView::TemplateHandlers::Compilable
def compile(template)
%{
tmp = Tempfile.new('writeexcel')
workbook = WriteExcel.new(tmp.path)
#{template.source}
workbook.close
tmp.read
}
end
end
Mime::Type.register('application/vnd.ms-excel', :xls)
ActionView::Template.register_template_handler('writeexcel', WriteExcelTemplateHandler)
@arunmohan
Copy link

It is not exporting xls, it shows some encrypted text in xls

@ream88
Copy link
Author

ream88 commented Nov 20, 2011

@arunmohan which Rails version are you using? This works with Rails 3.0 only.

@arunmohan
Copy link

arunmohan commented Nov 20, 2011 via email

@ream88
Copy link
Author

ream88 commented Nov 20, 2011

Ok, I will try to make a Rails 2.3.5 handler too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment