从我的代码库中添加多行数据而不是 add_row 的示例继续将单元格添加到行中希望这可能会有所帮助,尽管还有许多其他方法可以在 excel 中编写,但旧版 rails 应用程序仍然使用这个库:
workbook = RubyXL::Workbook.new
## First workbook
worksheet = workbook[0]
## Rename first workbook
worksheet.sheet_name = 'delayed_jobs_data'
header = ['JOB ID', 'PARENT ID', 'STATUS', 'PRIORITY', 'ATTEMPTS', 'JOB TYPE', 'HOST', 'USER', 'PROCESS', 'QUEUE', 'ITEM NUMBER', 'RUN AT', 'LOCKED AT', 'CREATED AT', 'COMPLETED AT']
header.each_with_index do |h, idx|
worksheet.add_cell(0, idx, h)
end
worksheet.change_column_width(0, 10)
worksheet.change_column_width(1, 10)
worksheet.change_column_width(2, 10)
worksheet.change_column_width(3, 10)
worksheet.change_column_width(4, 10)
worksheet.change_column_width(5, 30)
worksheet.change_column_width(6, 20)
worksheet.change_column_width(7, 10)
worksheet.change_column_width(8, 30)
worksheet.change_column_width(9, 20)
worksheet.change_column_width(10, 10)
worksheet.change_column_width(11, 20)
worksheet.change_column_width(12, 20)
worksheet.change_column_width(13, 20)
worksheet.change_column_width(14, 20)
worksheet.change_row_fill(0, '808080')
## Now write the data
# check if the from data and to date present else archive the data of delayed jobs of the Time.now - 30 days current month
from = params[:from_date]
to = params[:to_date]
if !from.blank? && !to.blank?
data = Model.where("created_at between ? and ?", from, to)
file_name = "archiving_persited_jobs_from_#{from}_to_#{to}"
end
dest_file_path = Rails.root.join("public", "persisted_delayed_jobs", "#{file_name}.xlsm")
## After the data os written delete the data from database
data.each_with_index do |row, index|
new_index = index += 1
row_data = [row.id,
(row.parent.id unless row.parent.blank?),
row.status, row.priority, row.attempts, row.job_type, row.locked_by, row.user.full_name,
(row.parent.job_type unless row.parent.blank?),
row.queue, row&.item&.item_number,
(row.run_at.to_datetime.in_time_zone(current_user.time_zone).strftime('%m/%d/%Y %H:%M') rescue row.run_at.strftime('%m/%d/%Y %H:%M')),
(row.locked_at.to_datetime.in_time_zone(current_user.time_zone).strftime('%m/%d/%Y %H:%M') rescue row.locked_at.strftime('%m/%d/%Y %H:%M')),
(row.created_at.to_datetime.in_time_zone(current_user.time_zone).strftime('%m/%d/%Y %H:%M') rescue row.created_at.strftime('%m/%d/%Y %H:%M')),
(row&.completed_at&.to_datetime&.in_time_zone(current_user.time_zone)&.strftime('%m/%d/%Y %H:%M') rescue row&.completed_at&.strftime('%m/%d/%Y %H:%M')),
(row&.failed_at&.to_datetime&.in_time_zone(current_user.time_zone)&.strftime('%m/%d/%Y %H:%M') rescue row&.failed_at&.strftime('%m/%d/%Y %H:%M')),
(row.completed_at - row.created_at unless !row.created_at.blank? & !row.completed_at)
]
# worksheet.insert_row(row_data)
worksheet.add_cell(new_index, 0, row_data[0])
worksheet.add_cell(new_index, 1, row_data[1])
worksheet.add_cell(new_index, 2, row_data[2])
worksheet.add_cell(new_index, 3, row_data[3])
worksheet.add_cell(new_index, 4, row_data[4])
worksheet.add_cell(new_index, 5, row_data[5])
worksheet.add_cell(new_index, 6, row_data[6])
worksheet.add_cell(new_index, 7, row_data[7])
worksheet.add_cell(new_index, 8, row_data[8])
worksheet.add_cell(new_index, 9, row_data[9])
worksheet.add_cell(new_index, 10, row_data[10])
worksheet.add_cell(new_index, 11, row_data[11])
worksheet.add_cell(new_index, 12, row_data[12])
worksheet.add_cell(new_index, 13, row_data[13])
worksheet.add_cell(new_index, 14, row_data[14])
end
workbook.write(dest_file_path)