如何使用Ruby阅读Excel电子表格的内容?

时间:2022-11-29 07:28:59

I am trying to read an Excel spreadsheet file with Ruby, but it is not reading the content of the file.

我试图用Ruby读取Excel电子表格文件,但它没有读取文件的内容。

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?

我需要获取文件的实际内容。我究竟做错了什么?

3 个解决方案

#1


20  

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.

它看起来像行,其类是Spreadsheet :: Excel :: Row实际上是一个Excel范围,它包括Enumerable或至少暴露一些可枚举的行为,例如#each。

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:

它可能是一个较大的脚本的宿醉,但我会指出,在代码中给出了book和sheet1变量并不是真正需要的,并且Spreadsheet#open需要一个块,所以一个更惯用的Ruby版本可能就像这个:

require 'spreadsheet'    
Spreadsheet.open('MyTestSheet.xls') do |book|
  book.worksheet('Sheet1').each do |row|
    break if row[0].nil?
    puts row.join(',')
  end
end

#2


4  

I don't think you need to require parseexcel, just require 'spreadsheet'

我认为您不需要parseexcel,只需要'电子表格'

Have you read the guide, it is super easy to follow.

你读过这本指南,它非常容易理解。

#3


0  

Is it a one line file? If so you need:

它是一个单行文件吗?如果是这样,你需要:

puts row[0];

#1


20  

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.

它看起来像行,其类是Spreadsheet :: Excel :: Row实际上是一个Excel范围,它包括Enumerable或至少暴露一些可枚举的行为,例如#each。

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:

它可能是一个较大的脚本的宿醉,但我会指出,在代码中给出了book和sheet1变量并不是真正需要的,并且Spreadsheet#open需要一个块,所以一个更惯用的Ruby版本可能就像这个:

require 'spreadsheet'    
Spreadsheet.open('MyTestSheet.xls') do |book|
  book.worksheet('Sheet1').each do |row|
    break if row[0].nil?
    puts row.join(',')
  end
end

#2


4  

I don't think you need to require parseexcel, just require 'spreadsheet'

我认为您不需要parseexcel,只需要'电子表格'

Have you read the guide, it is super easy to follow.

你读过这本指南,它非常容易理解。

#3


0  

Is it a one line file? If so you need:

它是一个单行文件吗?如果是这样,你需要:

puts row[0];