【问题标题】:Download XLSX file created by XlsxWriter in rails在 rails 中下载由 XlsxWriter 创建的 XLSX 文件
【发布时间】:2018-07-23 03:37:03
【问题描述】:

我正在尝试将 xlsx 文件保存到浏览器下载中,单击浏览器按钮时会激活以下功能

def trip_bit
@car = Car.find(params[:id])
@t = @car.trips.all
@trips = @t.order(:started_at)

if @trips then
  doc = XlsxWriter.new
  doc.quiet_booleans!
  sheet = doc.add_sheet("Vitácora de Viajes "+@car.model+' '+@car.year.to_s)
  sheet.freeze_top_left = 'A2'
  sheet.add_row(['Salida', 'Llegada', 'Origen', 'Destino', 'Distancia', 'Duración', 'Score'])
  @trips.each do |trip|
    sheet.add_row([trip.started_at.to_s, trip.finished_at.to_s, if trip.origin then trip.origin.address end, if trip.destination then trip.destination.address end, trip.distance.to_s+'km', trip.duration.to_s+'min', trip.grade])
  end
  sheet.add_row(["","","","Total", @trips.map { |trip| trip.distance }.sum.to_s+"km", @trips.map { |trip| trip.duration}.sum.to_s+"min", ""])
else
  redirect_to my_car_details_path
  flash.now[:alert] = 'Este coche no tiene viajes registrados'
end

send_file doc, :filename => "bitacora.xlsx"

end

xlsx 文件doc 被创建并保存到本地文件系统,但我找不到将其发送到浏览器下载的方法。

【问题讨论】:

  • 你的意思是如果有人点击文件就会被保存,而不是在计算机的“下载”文件夹中?或者它没有显示在浏览器菜单下拉列表中的下载列表中?你用什么浏览器测试过?
  • 你的意思是这样的:stackoverflow.com/questions/4868945/…
  • 我尝过Chrome和Safari,我想说的是xlsx文件退出了,但我不知道如何将它发送到浏览器下载。使用 send_file 我得到一个红色屏幕,上面写着 没有将 XlsxWriter 隐式转换为字符串
  • @Beartech 我相信 send_file 不适用于 XlsxWriter
  • 仍然不清楚你的意思:“我想说的是xlsx文件退出了,但我不知道如何将它发送到浏览器下载”。

标签: ruby-on-rails ruby


【解决方案1】:

查看send_file 和 XlsxWriter 的文档后,错误告诉您它需要一个字符串,但您正在向它传递一个 XlsxWriter 类型的对象。你可以试试path方法告诉sendfile文件在哪里...

send_file(doc.path, :disposition => 'attachment', :filename => "bitacora.xlsx", type: "application/xml")

您没有指明 Rails 应用程序的任何结构,所以我假设这是一个控制器方法。您可能可以按照以下方式做一些事情:

def trip_bit
@car = Car.find(params[:id])
@t = @car.trips.all
@trips = @t.order(:started_at)

if @trips then
  doc = XlsxWriter.new
  doc.quiet_booleans!
  sheet = doc.add_sheet("Vitácora de Viajes "+@car.model+' '+@car.year.to_s)
  sheet.freeze_top_left = 'A2'
  sheet.add_row(['Salida', 'Llegada', 'Origen', 'Destino', 'Distancia', 'Duración', 'Score'])
  @trips.each do |trip|
    sheet.add_row([trip.started_at.to_s, trip.finished_at.to_s, if trip.origin then trip.origin.address end, if trip.destination then trip.destination.address end, trip.distance.to_s+'km', trip.duration.to_s+'min', trip.grade])
  end
  sheet.add_row(["","","","Total", @trips.map { |trip| trip.distance }.sum.to_s+"km", @trips.map { |trip| trip.duration}.sum.to_s+"min", ""])
else
  redirect_to my_car_details_path
  flash.now[:alert] = 'Este coche no tiene viajes registrados'
end

respond_to do |format|
    format.xlsx
  end

end

上面的代码是一个直接的猜测,因为 XlsxWriter 是一个 C 库的绑定,从 Rails 的角度来看,文档真的很薄,对我来说几乎没用。我一直在使用 axlsx_rails gem。它允许您定义一个模板,然后将该模板交给 Rails 数据结构。我强烈建议您查看:https://github.com/straydogstudio/axlsx_rails

它允许您继续以类似 Rails 的方式工作。当我想将用户电子表格导出到 xlsx 时,我只需在我的控制器中执行此操作:

  def index
    @users = User.active
    respond_to do |format|
      format.html
      format.xlsx
    end
  end

然后在我的视图中,在我的 index.html.erb 旁边,我有一个名为 index.xlsx.axlsx 的文件,看起来像:

wb = xlsx_package.workbook
wb.add_worksheet(name: "Users") do |sheet|
sheet.add_row ['User', 'User Access Role']
  @users.each do |user|
    sheet.add_row [user.email, user.role.name]
  end
end

如您所见,您在模板中有创建电子表格的逻辑。你这样做的方式非常hack-is和混乱。

【讨论】:

  • 感谢我现在正在使用 axlsx gem,它运行良好!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-05
  • 2019-09-15
  • 1970-01-01
  • 1970-01-01
  • 2020-03-21
  • 1970-01-01
  • 2019-07-23
相关资源
最近更新 更多