Export to shared folder using axlsx

I am using axlsx stone to create excel files. I get them serialized in the project home directory as an xlsx file. But I want the file to be created in the shared folder of my rails application or directly downloaded by the user without saving it to the server. How can i do this?? Here is the controller that generates the xlsx file

def export_excel p = Axlsx::Package.new wb = p.workbook wb.add_worksheet(:name => "Basic Worksheet") do |sheet| (1..10).each { |label| sheet.add_row [label, rand(24)+1] } sheet.add_chart(Axlsx::Bar3DChart, :start_at => "A14", :end_at => "F24") do |chart| chart.add_series :data => sheet["B1:B10"], :labels => sheet["A1:A10"], :title => sheet["A1"] end end p.serialize('charts.xlsx') end 
+7
source share
2 answers
 p = Axlsx::Package.new # ... outstrio = StringIO.new p.use_shared_strings = true # Otherwise strings don't display in iWork Numbers outstrio.write(p.to_stream.read) outstrio.string 

This will result in the contents of the xls file. Then you can either send_data to transfer it to the user, or save it to a file on disk.

+5
source

Late answer: you can also use the axlsx_rails gem. It adds a template handler for axlsx, which makes it easy to generate and maintain xlsx files:

https://github.com/straydogstudio/axlsx_rails

It allows you to put all your axlsx code in partial views. If you do a lot of xlsx in rails, this is convenient.

Of course, the serializer works fine, or the stream, as mentioned by @fearpi.

0
source

All Articles