I am making a survey app, and I need to export the data to Excel. I added a .xls
mime-type and I have a view called results.xls.slim
that contains an html table.
My controller looks like this:
# GET /survey_groups/1/results
def results
@survey_group = get_survey_group
@survey = @survey_group.survey
@questions = @survey.questions
@members = @survey_group.members
organisation = @survey_group.organisation
respond_to do |format|
# http://stackoverflow.com/questions/11627291/rails-excel-mime-type-how-to-change-default-filename
format.xls { headers['Content-Disposition'] = "attachment; filename=\"#{ organisation.name } - Survey Group Results\"" }
end
end
The problem when I click on the link to export to Excel
= link_to('Excel', survey_results_path(@organisation.id, @survey_group.id, format: 'xls'))
the results first render in the browser (see the screenshot below). Only when I F5 do I get the downloadable file as expected.
In case anyone is wondering, Excel does render basic html and somehow converts it so you can still use formulas etc (which makes for very convenient exporting of tabular data with no interop libraries needed etc.).
I only want the results to be downloaded, not rendered in the browser.