karthick nagarajan karthick nagarajan - 1 year ago 165
Ruby Question

How to export a word document in my ruby-on-rails application

Can anyone help me, I want to export a Word document in my ruby-on-rails application..

I already use this one.

Is htmltoword gem the right way?

Answer Source

Try this gem which will allow you to create word document from HTML.

As per their document,

For htmltoword version >= 0.2 An action controller renderer has been defined, so there's no need to declare the mime-type and you can just respond to .docx format. It will look then for views with the extension .docx.erb which will provide the HTML that will be rendered in the Word file.

# On your controller.
respond_to :docx

# filename and word_template are optional. By default it will name the file as your action and use the default template provided by the gem. The use of the .docx in the filename and word_template is optional.
def my_action
  # ...
  respond_with(@object, filename: 'my_file.docx', word_template: 'my_template.docx')
  # Alternatively, if you don't want to create the .docx.erb template you could
  respond_with(@object, content: '<html><body>some html</body></html>', filename: 'my_file.docx')

def my_action2
  # ...
  respond_to do |format|
    format.docx do
      render docx: 'my_view', filename: 'my_file.docx'
      # Alternatively, if you don't want to create the .docx.erb template you could
      render docx: 'my_file.docx', content: '<html><body>some html</body></html>'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download