30

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

skaffman
  • 398,947
  • 96
  • 818
  • 769
Jack
  • 3,878
  • 15
  • 42
  • 72
  • Out of curiosity, why was the csv file version impractical? Did it require the end user to do to much in order to upload student? I fear that my users may be intimidated by having to covert to csv. – E.E.33 Oct 18 '12 at 16:00

4 Answers4

27

"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

peteretep
  • 307
  • 3
  • 11
clyfe
  • 23,695
  • 8
  • 85
  • 109
15

we use roo which support Open Office,Excel,Google,Excel.xlsx

peteretep
  • 307
  • 3
  • 11
allenwei
  • 4,047
  • 5
  • 23
  • 26
  • but `roo` does not have a functionality to save .xlsx file, only reading and parsing. or I am not right? – bmalets Mar 19 '18 at 18:57
4

There's an excellent RailsCast on how to do this with the roo gem: http://railscasts.com/episodes/396-importing-csv-and-excel

cgenco
  • 3,370
  • 2
  • 31
  • 36
2

Hey I use the spreadsheet gem and wrote a simple app to understand how it all fits together. It currently runs on Rails 3.1 and Carrierwave, hope this helps:

https://github.com/jalagrange/excel_test_app

jalagrange
  • 2,291
  • 2
  • 19
  • 24