[英]Generating large xlsx file with axlsx_rails gem using delayed_job
[英]Rails - axlsx_rails, generating xlsx and sending via API
通過 API 將 xlsx 文件發送到前端的最佳方式是什么?如何? 我正在使用axlsx_rails gem生成帶有xlsx模板的報告。
我找到的base64
是: json
。
現在渲染 xlsx:
render xlsx: 'status_report', template: 'api/v1/report/status_report.xlsx.axlsx'
我知道這是一個老問題,但是我選擇了base64選項。
Axlsx::Package.new do |obj|
obj.workbook.add_worksheet(name: "Sheet Name") do |sheet|
# Sheet structure and rows...
end
send_data Base64.encode64(obj.to_stream.read), type: "application/xlsx", filename: 'filename.xlsx'
end
只需使用send_data
rails方法( send_data )
您應該將application_controller ActionController::API
更改為ActionController::Base
你可以像這樣使用send_file
:
def export
Axlsx::Package.new do |obj|
obj.workbook.add_worksheet(name: 'worksheet name') do |sheet|
# Sheet structure and rows...
end
obj.serialize('filename.xlsx')
send_file File.open('filename.xlsx')
end
end
不要忘記將rubyzip
、 axlsx
、 axlsx_rails
gem 添加到您的gemfile
中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.