-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generating xlsx from model #53
Comments
You can do something like this: def some_action
compressed_filestream = Zip::ZipOutputStream.write_buffer do |zos|
content = render_to_string xlsx: 'export', filename: filename, layout: false
zos.put_next_entry(filename)
zos.print content
# add other files
end
compressed_filestream.rewind
send_data compressed_filestream.read, filename: 'export.zip', type: 'application/zip'
end I am assuming you are wanting to return the zip to a request. Let me know if you had something else in mind.
|
I do want to return a zip to a request but it is a zip that includes many different files. I have a method in my model that gets many local files and zips them into a single folder. I am wondering if there is a way to add the file that axlsx generates to that folder. |
You can use the same render_to_string and dump it into a file in your directory: content = render_to_string xlsx: 'export', filename: filename, layout: false
File.open('/tmp/axlsx_temp.xlsx', 'w') {|f| f.write(content) } If you are calling this inside a model, then you may have a bit of an issue getting to render to string. There will likely be posts on the internet about that. I've not done it myself. You may have to do something like this. It is possible to render a template directly without the overhead of ActionView. See this test. |
I am going to close it because it works, but... can someone tell me if there is something really wrong with what I am doing here? (Note: model is the name of one of my models)
Thank you for your help! |
@agustaf9 Glad that worked! I don't see anything wrong in particular. As long as things are working I think you are ok. You are welcome for the help and gem. |
I am trying to get the spreadsheet after it is generated by axlsx into my model so I can zip it up with some other files. How would I go about doing this?
Edit: I know how to zip, I am just wondering how to generate the spreadsheet from my model.
The text was updated successfully, but these errors were encountered: