简体   繁体   中英

Importing a CSV to Rails database

I asked this question earlier this week, and it worked fine. I just tried it with a slightly bigger spreadsheet and it doesn't seem to work for some reason.

My code is as follows:

require 'roo'

xlsx = Roo::Spreadsheet.open(File.expand_path('../Downloads/unistats/LOCATION.csv'))
xlsx.each_row_streaming(offset: 1) do |row|
    Location.find_or_create_by(ukprn: row[0].value, accomurl: row[1].value, instbeds: row[3].value, instlower: row[4].value, instupper: row[5].value, locid: row[6].value, locname: row[7].value, lat: row[9].value, long: row[10].value, locukprn: row[11].value, loccountry: row[12].value, privatelower: row[13].value, privateupper: row[14].value, suurl: row[15].value)
end

But unlike last time, this is coming up with this error:

NoMethodError: undefined method `each_row_streaming' for #<Roo::CSV:0xb9e0b78>
Did you mean?  each_row_using_tempdir

This file is a CSV rather than .xlsx but that shouldn't make a difference.

Any ideas what I'm doing wrong?

It does actually makes a difference that you're trying to read a CSV file using the Excel methods.

Excerpts from the Roo documentation.

# Load a CSV file
s = Roo::CSV.new("mycsv.csv")

# Load a tab-delimited csv
s = Roo::CSV.new("mytsv.tsv", csv_options: {col_sep: "\t"})

# Load a csv with an explicit encoding
s = Roo::CSV.new("mycsv.csv", csv_options: {encoding: Encoding::ISO_8859_1})

A neat way to read both Excel and CSV files is to do something like

if File.extname(filename).start_with?('xls')
  workbook = Roo::Excel.new(filename)  
else
  workbook = Roo::CSV.new(filename)
end

workbook.default_sheet = workbook.sheets[0]

(workbook.first_row..workbook.last_row).each do |line|
  ...
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM