Ruby 使用电子表格gem格式化Nokogiri输出
我需要什么 在将Nokogiri输出发送到Excel等电子表格之前,是否可以对其进行格式化 示例: 位于的表格格式很好,可以使用电子表格gem将类似格式应用于Nokogiri输出吗 我的代码Ruby 使用电子表格gem格式化Nokogiri输出,ruby,nokogiri,spreadsheet,Ruby,Nokogiri,Spreadsheet,我需要什么 在将Nokogiri输出发送到Excel等电子表格之前,是否可以对其进行格式化 示例: 位于的表格格式很好,可以使用电子表格gem将类似格式应用于Nokogiri输出吗 我的代码 require 'nokogiri' require 'open-uri' require 'spreadsheet' doc = Nokogiri::HTML(open("http://www.asus.com/Notebooks_Ultrabooks/ASUS_TAICHI_21/#specifica
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'
您的代码创建了一个具有单行的电子表格,其中只有一列。该列是所有行的串联 要将每行放入自己的列中,请执行以下操作:
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' |
简单而雄辩:)谢谢你。