使用Roo Gem解析Rails中的XLS Spreadsheet [英] Parsing XLS Spreadsheet in Rails using Roo Gem

查看:99
本文介绍了使用Roo Gem解析Rails中的XLS Spreadsheet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用roo gem解析一个XLS文件,而不使用文件上传插件。不幸的是,我无法访问文件的数据。

I am trying to parse a XLS file with the roo gem without using a file upload plugin. Unfortunately I can not access the data of the File.

我得到错误:

I get the error:

#<File:0x007ffac2282250> is not an Excel file

所以roo不会将文件识别为Excel文件。我是否需要在本地保存文件才能使用roo,或者有没有办法解决这个问题。我想将excel文件的数据直接分析到数据库中。

So roo is not recognizing the file as an Excel file. Do I need to save the file locally to use roo or is there a way around that. I would like to parse the data of the excel file directly into the database.

正在通过的参数:

The params that are coming through:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"yLqOpSK981tDNYjKSoWBh0VnFEKSk0XA/wOt3r+yWJc=", "uploadform"=>{"name"=>"xls", "file"=>#<ActionDispatch::Http::UploadedFile:0x007ffac22b6550 @original_filename="cities2.xls", @content_type="application/octet-stream", @headers="Content-Disposition: form-data; name=\"uploadform[file]\"; filename=\"cities2.xls\"\r\nContent-Type: application/octet-stream\r\n", @tempfile=#<File:/var/folders/qn/70msrkt90pd390sdr14_0g2m0000gn/T/RackMultipart20120306-3729-1m2xcsp>>}, "commit"=>"Save Uploadform"}

我试图用

I am trying to access the file with

    if params[:uploadform][:file].original_filename =~ /.*\.xls$/i
        oo = Excel.new(params[:uploadform][:file].open)

        rooparse(oo)
    end

我也尝试了params [:uploadform] [:file] .read和params [:uploadform] [:file]已经存在,但我认为.open在这里是正确的方法!?

I also tried params[:uploadform][:file].read and params[:uploadform][:file] already but I think the .open would be the correct method here!?

您会推荐在这里使用回形针或carrierwave吗?

And would you recommend using paperclip or carrierwave here?

感谢您的帮助!

推荐答案

查看 Excel.new ,似乎它想要一个文件名,而不是一个File对象或处理程序。换句话说,它需要将完整路径(包括文件名)的字符串表示形式分配给要解析的文件。另外,它还检查文件的扩展名。因此,如果临时文件不以.xls结尾,则需要先重命名文件。

Looking at the source for Excel.new, it seems that it wants a file name, not a File object or handler. In other words, it needs string representation of the full path, including filename, to the the file you want to parse. Also, it checks the extension of the file. So if the tempfile doesn't end with ".xls" you'll need to rename the file first.

这篇关于使用Roo Gem解析Rails中的XLS Spreadsheet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆