我需要什么
在将Nokogiri输出发送到像Excel这样的电子表格之前,是否可以格式化?
示例: http://www.asus.com/Notebooks_Ultrabooks/ASUS_TAICHI_21/#specifications的表格格式很好,可以使用电子表格宝石将相似的格式应用于Nokogiri输出吗?
我的代码
require 'nokogiri'
require 'open-uri'
require 'spreadsheet'
doc = Nokogiri::HTML(open("http://www.asus.com/Notebooks_Ultrabooks/ASUS_TAICHI_21/#specifications"))
#Grab our product specifications
data = doc.css('div#specifications div#spec-area ul.product-spec li')
#Modify our data
lines = data.map(&:text).join("\n")
#Create the Spreadsheet
Spreadsheet.client_encoding = 'UTF-8'
book = Spreadsheet::Workbook.new
sheet1 = book.create_worksheet
sheet1.name = 'My First Worksheet'
#Output our data to the Spreadsheet
sheet1[0,0] = lines
book.write 'C:/Users/Barry/Desktop/output.xls'
答案 0 :(得分:0)
您的代码会创建一个包含单个列的电子表格。该列是所有行的串联。
将每一行放入自己的列中,然后:
lines = data.map(&:text)
...
lines.each.with_index do |line, i| |
sheet1[0, i] = line |
end |
book.write '/tmp/output.xls' |
要将每一行放入自己的行,那么:
lines = data.map(&:text)
...
lines.each.with_index do |line, i| |
sheet1[i, 0] = line |
end |
book.write '/tmp/output.xls' |