如何使用Ruby读取Excel电子表格的内容? [英] How do I read the content of an Excel spreadsheet using Ruby?
问题描述
我试图用Ruby读取Excel电子表格文件,但它不读取文件的内容。
这是我的脚本
book = Spreadsheet.open'myexcel的.xls';
sheet1 = book.worksheet 0
sheet1.each do | row |
放入row.inspect;
把row.format 2;
puts row [1];
出口;
end
它给了我以下信息:
[DEPRECATED]通过要求'parseexcel','parseexcel / parseexcel'和/或
'parseexcel / parser',您正在加载一个兼容性层,其中
为ParseExcel库提供了一个替代品。这
代码使电子表格文档的阅读效率降低,
将在电子表格版本1.0.0中删除
#< Spreadsheet :: Excel :: Row:0xffffffdbc3e0d2 @worksheet =#<电子表格::的Excel工作表:::0xb79b8fe0> @ outline_level = 0 @ idx = 0 @ hidden = false @ height = @ default_format = @ formats = []>
#< Spreadsheet :: Format:0xb79bc8ac>
nil
我需要获取文件的实际内容。我做错了什么?
它看起来像 row
,类是 Spreadsheet :: Excel :: Row
实际上是一个Excel 范围
,它包含Enumerable或者至少暴露例如一些可枚举的行为,例如 #each
。
require'spreadsheet'
book = Spreadsheet.open('myexcel.xls')
sheet1 = book。工作表('Sheet1')#可以使用索引或工作表名称
sheet1.each do | row |
break如果行[0] .nil? #如果第一个单元格为空
puts row.join(',')#看起来像是在每个单元格的值上调用to_s的值
end
请注意,我已经使用括号括起来的参数,这些参数现在通常是可取的,并删除了分号,除非您在一行中写入多个语句(你应该很少 - 如果有的话)。
这可能是一个较大脚本的宿醉,但我会指出,在给定 book
和 sheet1
变量并不是真的需要,并且 Spreadsheet#open
需要一个块,所以一个更习惯的Ruby版本可能是这样的:
require'spreadsheet'
Spreadsheet.open('MyTestSheet.xls')do | book |
book.worksheet('Sheet1')。each | row |
break如果行[0] .nil?
puts row.join(',')
end
end
I am trying to read an Excel spreadsheet file with Ruby, but it is not reading the content of the file.
This is my script
book = Spreadsheet.open 'myexcel.xls';
sheet1 = book.worksheet 0
sheet1.each do |row|
puts row.inspect ;
puts row.format 2;
puts row[1];
exit;
end
It is giving me the following:
[DEPRECATED] By requiring 'parseexcel', 'parseexcel/parseexcel' and/or
'parseexcel/parser' you are loading a Compatibility layer which
provides a drop-in replacement for the ParseExcel library. This
code makes the reading of Spreadsheet documents less efficient and
will be removed in Spreadsheet version 1.0.0
#<Spreadsheet::Excel::Row:0xffffffdbc3e0d2 @worksheet=#<Spreadsheet::Excel::Worksheet:0xb79b8fe0> @outline_level=0 @idx=0 @hidden=false @height= @default_format= @formats= []>
#<Spreadsheet::Format:0xb79bc8ac>
nil
I need to get the actual content of file. What am I doing wrong?
It looks like row
, whose class is Spreadsheet::Excel::Row
is effectively an Excel Range
and that it either includes Enumerable or at least exposes some enumerable behaviours, #each
, for example.
So you might rewrite your script something like this:
require 'spreadsheet'
book = Spreadsheet.open('myexcel.xls')
sheet1 = book.worksheet('Sheet1') # can use an index or worksheet name
sheet1.each do |row|
break if row[0].nil? # if first cell empty
puts row.join(',') # looks like it calls "to_s" on each cell's Value
end
Note that I've parenthesised arguments, which is generally advisable these days, and removed the semi-colons, which are not necessary unless you're writing multiple statement on a line (which you should rarely - if ever - do).
It's probably a hangover from a larger script, but I'll point out that in the code given the book
and sheet1
variables aren't really needed, and that Spreadsheet#open
takes a block, so a more idiomatic Ruby version might be something like this:
require 'spreadsheet'
Spreadsheet.open('MyTestSheet.xls') do |book|
book.worksheet('Sheet1').each do |row|
break if row[0].nil?
puts row.join(',')
end
end
这篇关于如何使用Ruby读取Excel电子表格的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!