1

是 XLS 文件的链接。我正在尝试使用电子表格 gem 来提取 XLS 文件的内容。特别是,我想收集所有列标题,如(年份、国民生产总值等)。但是,问题是它们不在同一行。例如,国民总收入由三行组成。我还想知道合并了多少行单元格以使单元格为“年”。

我已经开始编写程序,我已经做到了:

require 'rubygems'
require 'open-uri'
require 'spreadsheet'

rows = Array.new
url = 'http://www.stats.gov.cn/tjsj/ndsj/2012/html/C0201e.xls'
doc = Spreadsheet.open (open(url))
sheet1 = doc.worksheet 0
sheet1.each do |row|
      if row.is_a? Spreadsheet::Formula
          # puts row.value
          rows << row.value
     else
          # puts row
          rows << row
     end
  # puts row.value
end

但是,现在我被困住了,真的需要一些指导来继续。任何形式的帮助都将不胜感激。

4

1 回答 1

3
require 'rubygems'
require 'open-uri'
require 'spreadsheet'

rows = Array.new
temp_rows = Array.new
column_headers = Array.new
index = 0
url = 'http://www.stats.gov.cn/tjsj/ndsj/2012/html/C0201e.xls'
doc = Spreadsheet.open (open(url))
sheet1 = doc.worksheet 0
sheet1.each do |row|
   rows << row.to_a
end

rows.each_with_index do |row,ind|
  if row[0]=="Year"
    index = ind
    break
  end
end

(index..7).each do |i|
  # puts rows[i].inspect
  if rows[i][0] =~ /[0-9]/
    break 
  else
    temp_rows << rows[i]
  end 
end

col_size = temp_rows[0].size
# puts temp_rows.inspect

col_size.times do |c|
  temp_str = ""
  temp_rows.each do |row|
    temp_str +=' '+ row[c] unless row[c].nil?
  end
  # puts temp_str.inspect
  column_headers << temp_str unless temp_str.nil?
end
puts 'Column Headers of this xls file are : '
# puts column_headers.inspect
column_headers.each do |col|
  puts col.strip.inspect if col.length >1
end
于 2013-01-28T20:45:44.013 回答