Ruby Spreadsheet Gem:如何显示列中所有内容的值?

我有一个Excel电子表格2列A和B,每列1-10行是数字。 当我尝试显示数字值。 结果如下:

#<Spreadsheet::Column:0x007fba83810368> #<Spreadsheet::Column:0x007fba83810d90> ... ... ... 

在电子表格中我有这个:

 xy 32 4 402 6 733 4 1 30 2 3128 2 4 1 1 6 2 10 63 90 333 

这是我遍历文件的方式:

 require 'spreadsheet' importing = Spreadsheet.open 'file.xls' book = importing.worksheet 'Sheet1' book.each do |x, y| x = book.column(1) y = book.column(2) puts x puts y end 

如何让ruby程序以格式化的方式输出数据? 我的预期输出与电子表格格式相同。

这是一个办法。 下面的代码与这个答案中的build议基本相同(build议为更类似于Ruby的方式),我已经添加了两行来生成所需的输出格式:

 puts "xy" require 'spreadsheet' Spreadsheet.open('file.xls') do |book| book.worksheet('Sheet1').each do |row| break if row[0].nil? puts "%6d %6d" % row end end 

您需要根据您要处理的最长号码更改列宽。 我允许每个列(%6d)有6个数字,1个空格(在两个格式说明符之间)。 我还列出了每个左alignment数字的最左边数字的列标题。