3

is there a way to generate XLS file using 'axlsx' gem? I'm already using this gem to generate xlsx files, and don't want to move it all to a different gem, especially since axlsx is much easier to work with.

avital
  • 559
  • 1
  • 7
  • 19

2 Answers2

3

Axlsx only generates xlsx files (it does not read them.) You'll have to use spreadsheet or roo or something else to directly create them.

I rely on the downloadable Microsoft converter to change from xlsx to xls. I only generate one file, and as long as the xlsx file is not too complicated there are no errors. It depends on your needs, of course.

If you are generating xlsx with Rails, consider using axlsx_rails. I wrote it, and it lets you put all Axlsx code into view templates.

noel
  • 2,095
  • 14
  • 14
  • do you use the converter as part of your code (before file downloaded)? or do you use it locally before opening it? if axlsx won't give me the solution for generating xls file, I will have to switch to one of the options you mentioned. thanks! – avital Jun 12 '14 at 07:48
  • No, not as a part of the code. It has to be installed on each client machine. It definitely has its limitations (occasionally I have a spreadsheet that fails to convert for no obvious reason.) If you have control over the client desktops, it can get you by until they upgrade Office. – noel Jun 12 '14 at 15:25
1

I generate my XLS files using the Ruby built-in class CSV:

file = CSV.generate do |csv|
  results.each do |r|
    csv << r
  end
end
mime_type = selected_format.downcase # can be either `xls` or `csv`
send_data file, type: mime_type, filename: filename, disposition: 'attachment'

This Railscast can also help you: http://railscasts.com/episodes/362-exporting-csv-and-excel?view=asciicast

MrYoshiji
  • 54,334
  • 13
  • 124
  • 117