在 Sidekiq 中将 xlsx 文件保存到磁盘作为背景
Save xlsx file to disk in Sidekiq as background
我正在尝试使用 axlsx 在后台生成 excel 文件并保存它 (Rails4.2)。在 axlsx 的 GitHub 页中,显示 As of Rails 4.1 you must use render_to_string to render a mail attachment
。但是,它抛出一个错误 NoMethodError: undefined method 'render_to_string' for #<CreateExcelSheetWorker:0x007fbccf51db30>
我的工人class:
class CreateExcelSheetWorker
include Sidekiq::Worker
include Sidetiq::Schedulable
recurrence { daily }
def perform()
model = SomeModel.where(wanted: true).order(started_at: :desc)
xlsx = render_to_string handlers: [:axlsx], formats: [:xlsx], template: "template/file", locals: {model: model}
path = "/tmp/a.xlsx"
File.open(path, "w+") do |f|
f.write(xlsx)
end
end
end
我不知道如何解决这个问题,感谢您的帮助。
render_to_string
评论是为了在 actionmailers 中使用 gem。在任何类型的视图上下文之外,您必须直接使用 xlsx 构建器 api。像这样:
package = Axlsx::Package.new do |p|
p.workbook.add_worksheet(name: "Summary") do |sheet|
sheet.add_row ["foo", 1]
end
end
File.write(filename, package.to_stream.read)
Sergio 的回答很好。但是,如果你想使用 axlsx_rails 模板,你可以使用这个例子,它构建了一个假的视图上下文来呈现模板:
require 'abstract_controller'
require 'action_controller'
require 'action_view'
require 'active_record'
# require any helpers
require './app/helpers/application_helper'
# active record only if data is here
require './app/models/widget'
ActiveRecord::Base.establish_connection(
adapter: 'sqlite3',
database: 'db/development.sqlite3'
)
ActionController::Base.prepend_view_path "./app/views/"
# place data in view_assigns
view_assigns = {widgets: Widget.all}
av = ActionView::Base.new(ActionController::Base.view_paths, view_assigns)
av.class_eval do
# include any needed helpers (for the view)
include ApplicationHelper
end
# normal render statement
content = av.render template: 'widgets/index.xlsx.axlsx'
# do something with content, such as:
File.open("/tmp/with_runner.xlsx","w+b") {|f| f.puts content }
我正在尝试使用 axlsx 在后台生成 excel 文件并保存它 (Rails4.2)。在 axlsx 的 GitHub 页中,显示 As of Rails 4.1 you must use render_to_string to render a mail attachment
。但是,它抛出一个错误 NoMethodError: undefined method 'render_to_string' for #<CreateExcelSheetWorker:0x007fbccf51db30>
我的工人class:
class CreateExcelSheetWorker
include Sidekiq::Worker
include Sidetiq::Schedulable
recurrence { daily }
def perform()
model = SomeModel.where(wanted: true).order(started_at: :desc)
xlsx = render_to_string handlers: [:axlsx], formats: [:xlsx], template: "template/file", locals: {model: model}
path = "/tmp/a.xlsx"
File.open(path, "w+") do |f|
f.write(xlsx)
end
end
end
我不知道如何解决这个问题,感谢您的帮助。
render_to_string
评论是为了在 actionmailers 中使用 gem。在任何类型的视图上下文之外,您必须直接使用 xlsx 构建器 api。像这样:
package = Axlsx::Package.new do |p|
p.workbook.add_worksheet(name: "Summary") do |sheet|
sheet.add_row ["foo", 1]
end
end
File.write(filename, package.to_stream.read)
Sergio 的回答很好。但是,如果你想使用 axlsx_rails 模板,你可以使用这个例子,它构建了一个假的视图上下文来呈现模板:
require 'abstract_controller'
require 'action_controller'
require 'action_view'
require 'active_record'
# require any helpers
require './app/helpers/application_helper'
# active record only if data is here
require './app/models/widget'
ActiveRecord::Base.establish_connection(
adapter: 'sqlite3',
database: 'db/development.sqlite3'
)
ActionController::Base.prepend_view_path "./app/views/"
# place data in view_assigns
view_assigns = {widgets: Widget.all}
av = ActionView::Base.new(ActionController::Base.view_paths, view_assigns)
av.class_eval do
# include any needed helpers (for the view)
include ApplicationHelper
end
# normal render statement
content = av.render template: 'widgets/index.xlsx.axlsx'
# do something with content, such as:
File.open("/tmp/with_runner.xlsx","w+b") {|f| f.puts content }