Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import Excel into Rails app

I am creating a small rails app for personal use and would like to be able to upload excel files to later be validated and added to the database. I had this working previously with csv files, but this has since become impractical.

Does anyone know of a tutorial for using the roo or spreadsheet gem to upload the file, display the contents to the user and then add to the database (after validating)? I know this is quite specific, but I want to work through this step by step.

All I have so far is an 'import' view:

<% form_for :dump, :url=>{:controller=>"students", :action=>"student_import"}, :html => { :multipart => true } do |f| -%>     Select an Excel File :     <%= f.file_field :excel_file -%>     <%= submit_tag 'Submit' -%> <% end -%> 

But have no idea how to access this uploaded file in the controller.

Any suggestions/help would be welcomed. Thanks

like image 845
Jack Avatar asked May 12 '10 09:05

Jack


People also ask

How do I read an XLSX file in Ruby?

If you need to read and write . xlsx files, you have a couple of options. You can use rubyXL, which supports reading and writing. Alternatively, you could use two different libraries–one for reading and the other for writing.


1 Answers

  • https://rubygems.org/gems/spreadsheet
  • https://github.com/zdavatz/spreadsheet/blob/master/GUIDE.md

"The Spreadsheet Library is designed to read and write Spreadsheet Documents. As of version 0.6.0, only Microsoft Excel compatible spreadsheets are supported. Spreadsheet is a combination/complete rewrite of the Spreadsheet::Excel Library by Daniel J. Berger and the ParseExcel Library by Hannes Wyss. Spreadsheet can read, write and modify Spreadsheet Documents."

EDIT
To get the uploaded file you have two options:
1. (recommended) Use something a file upload plugin like paperclip and it will handle the bits and bolts.
2. use the IO object params[:dump][:excel_file] as per: http://guides.rails.info/form_helpers.html#what-gets-uploaded

like image 123
clyfe Avatar answered Oct 01 '22 09:10

clyfe