问题
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.
回答1:
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.
回答2:
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
来源:https://stackoverflow.com/questions/24146095/generating-xls-file-using-axlsx