我使用以下代码使用Spreadsheet
gem读取excel文件。
require 'spreadsheet'
Spreadsheet.client_encoding = 'UTF-8'
book = Spreadsheet.open('C:UsersLev BerlinDocumentsPersonalProjectsFactsRusNutritional Analysis ModelsData for Rails model import.xls')
sheet1 = book.worksheet('Sheet1')
但是该文件没有被正确读取。
当我取消对另一个文件中require 'parseexel'
行的注释时,文件将得到正确处理。
请帮帮我;这里怎么了?
提前谢谢。
一旦你加载了工作表(这就是你对"sheet1=…"所做的),你就需要读取该工作表中的行
sheet1.each do |row|
# do something interesting with a row
end
结账http://spreadsheet.rubyforge.org/files/GUIDE_txt.html了解更多信息。
请尝试使用以下内容。。
require 'spreadsheet'
@workbook = Spreadsheet.open(MyFile.first.attachment.to_file)
@worksheet = @workbook.worksheet(0)
0.upto @worksheet.last_row_index do |index|
row = @worksheet.row(index)
@contact = Contact.new
#row[0] is the first cell in the current row, row[1] is the second cell, etc...
@contact.first_name = row[0]
@contact.last_name = row[1]
@contact.save
end