简体   繁体   English

通过gem axlsx导轨生成的Zip Excel工作表

[英]Zip Excel sheet generated through gem axlsx rails

I'm facing an issue of how to zip the excel sheet generated through gem 'axlsx_rails'. 我面临着如何压缩通过gem'axlsx_rails'生成的Excel工作表的问题。 For example: 例如:

class SampleController < ApplicationController::Base

  def export
    if params[:zip]
        xxxx
    else
      render xlsx: 'export', filename: filename, disposition: 'attachment'
    end
  end

end

In the above example, right now the end user is able to download the excel sheet but if the end user requests a zip file of excel sheet, how can we do that. 在上面的示例中,现在最终用户可以下载excel表格,但是如果最终用户请求excel表格的zip文件,我们该怎么做。 Because of code in 'else' block the end user is able to download the excel sheet. 由于“ else”块中的代码,最终用户可以下载excel表格。 What I have to do if the user wants the excelsheet to be zipped before download. 如果用户希望在下载前将excelsheet压缩,该怎么办。 If you need any further info, plz let me know. 如果您需要更多信息,请告诉我。 Thanks 谢谢

This is untested code, but try using Zip::ZipOutputStream : 这是未经测试的代码,但是尝试使用Zip::ZipOutputStream

def export
  if params[:zip]
    compressed_filestream = Zip::ZipOutputStream.write_buffer do |zos|
      content = render_to_string xlsx: 'export', filename: filename
      zos.put_next_entry(filename)
      zos.print content
    end
    compressed_filestream.rewind
    send_data compressed_filestream.read, filename: 'export.zip', type: 'application/zip'
  else
    render xlsx: 'export', filename: filename, disposition: 'attachment'
  end
end

If it doesn't work, create an issue on Github, and after we get it working we can add it to the documentation. 如果它不起作用,请在Github上创建一个问题,然后使其工作,我们可以将其添加到文档中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM