简体   繁体   English

在导轨应用程序中导入 excel 到 sqlite

[英]importing excel to sqlite in rails application

I am making an application in Ruby on Rails which will ask users multiple choice questions.我正在 Ruby on Rails 中提出一个应用程序,它将询问用户多项选择问题。 I want to upload questions to the database from an excel file.我想将问题从 excel 文件上传到数据库。 How can I do it?我该怎么做?

Save the Excel spreadsheet as a CSV file then use a CSV parser, perhaps in a rake file:将 Excel 电子表格保存为 CSV 文件,然后使用 CSV 解析器,可能在 rake 文件中:

In lib/taks/import.rake:在 lib/taks/import.rake 中:

require 'fastercsv'
namespace :import => :environment do
   task :questions do
     FasterCSV.foreach("path/to/file.csv") do |row|
           q = Question.create(:question=>row[0], etc...)
           PossibleAnswer.create(:question=>q, :answer=>row[1], etc....) #providing PossibleAnswer belongs_to Question
      end
   end 
end

Then run "rake import:questions"然后运行“rake import:questions”

You could use the spreadsheet gem to read in the data from an excel file: http://rubygems.org/gems/spreadsheet您可以使用电子表格 gem 从 excel 文件中读取数据: http://rubygems.org/gems/spreadsheet

This is most useful if you want to allow users to upload their own excel documents, to import some questions, or if you want users to be able to download questions in excel file format.如果您希望允许用户上传他们自己的 excel 文档,导入一些问题,或者如果您希望用户能够下载 excel 文件格式的问题,这将非常有用。

If you just want to do a one-off import of some data i would go with Yule's idea and just do it via csv which is much easier to get to grips with.如果你只想一次性导入一些数据,我会用 Yule 的想法 go 并通过 csv 来完成,这更容易掌握。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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