Ruby on Rails: Why doesn't my formatting work when downloading XLS? -
following railscast http://railscasts.com/episodes/362-exporting-csv-and-excel?autoplay=true trying format xls download, below code doesn't format xls file opens excel (with no data , no file opened).
mime_types.rb
:
mime::type.register "application/xls", :xls
contacts_controller
:
def index @contacts = contact.where(user_id: session[:user_id]) respond_to |format| format.html format.csv { send_data @contacts.to_csv } format.xls end end
contact
model:
def self.to_csv(options = {}) csv.generate(options) |csv| csv << column_names all.each |contact| csv << contact.attributes.values_at(*column_names) end end end
index.xls.erb
:
<table border="1"> <tr> <th>firstname</th> <th>surname</th> <th>email</th> </tr> <% @contacts.each |contact| %> <tr> <td><%= contact.firstname %></td> <td><%= contact.surname %></td> <td><%= contact.email %></td> </tr> <% end %> </table>
can tell me of reason this?
can note when replacing line format.xls
in controller format.xls { send_data @contacts.to_csv(col_sep: "\t") }
does download xls file no formatting.
but opens excel (with no data , no file opened).
perhaps, has fact send html table, not spreadsheet data. in railscast, ryan generates xml markup instead. did try that?
when replacing line format.xls in controller format.xls { send_data @contacts.to_csv(col_sep: "\t") } download xls file no formatting.
actually, happens download csv file pretends xls file, excel trying friendly , lets pass. no formatting, because csv data-only format. not possible embed formatting data there, excel doesn't fetch any.
you'll better results generating real thing instead of generating "fake" spreadsheets , hope excel interprets them correctly. check out axlsx gem, example.
Comments
Post a Comment