简体   繁体   English

从Ruby读取流中的.xlsx文件

[英]Reading .xlsx file from stream in ruby

I am using axlsx gem to write contents to .xlsx file. 我正在使用axlsx gem将内容写入.xlsx文件。 I am using following code to stream the file to the browser: 我正在使用以下代码将文件流式传输到浏览器:

  send_data excel_package.to_stream.read, type: "application/xlsx", filename: filename

Now I want to write test cases for this and want to read the same file in my rspec. 现在,我想为此编写测试用例,并希望在我的rspec中读取相同的文件。 How can I do that ? 我怎样才能做到这一点 ? I looked for other libraries that can read excel file, but all of those read from the disk. 我寻找了其他可以读取excel文件的库,但是所有这些库都是从磁盘读取的。

The package is a zip archive. 该软件包是一个zip存档。

The simplest thing you can do is write the package to disk and load it with RubyZip, however if you want everything working without disk I/O you might take a look at rubyzip's InputStream to load from StringIO, https://github.com/rubyzip/rubyzip/blob/master/lib/zip/input_stream.rb 您可以做的最简单的事情是将包写入磁盘并用RubyZip加载,但是,如果您希望所有工作都不需要磁盘I / O,则可以看看rubyzip的InputStream从StringIO加载, 网址为https://github.com/ rubyzip / ruby​​zip / blob / master / lib / zip / input_stream.rb

Once you have access to the zip archive. 一旦您可以访问zip存档。 From there, you load the entry for the package part you want to spec with tools like Nokogiri and assert as needed. 从那里,您可以使用Nokogiri之类的工具加载要指定的软件包零件的条目,并根据需要进行断言。

You will find a sample list of parts here https://github.com/randym/axlsx/blob/master/test/tc_package.rb#L163 您将在这里找到零件的示例列表https://github.com/randym/axlsx/blob/master/test/tc_package.rb#L163

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

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