Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Reading & Showing CSV/Excel file

CSV files

  • Please log in to reply
6 replies to this topic

#1 TerryMiddleton

TerryMiddleton

    Passenger

  • Members
  • 3 posts

Posted 08 January 2014 - 05:55 PM

Has anyone imported a CSV or Excel file and outputted the contents.

 

I need to read a csv (or excel) file sitting on an ftp server and do something with the data.  For now if I can simply output it to the browser that would ben fantastic.  

 

I looked at the Class CSV and can't make heads or tails of it, mainly because I'm very new at the Ruby on Rails thing.

 

Any thoughts would be greatly appreciated.

Terry



#2 Ohm

Ohm

    Guard

  • Members
  • 179 posts
  • LocationCopenhagen

Posted 08 January 2014 - 09:55 PM

We do this quite often.

 

I have the CSV-file located the same place as the code, however this is not necessary at all.

 

If it's at the same place you can read through it with:

require 'csv'
CSV.foreach("static_data/csv-file.csv", col_sep: ',') do |row|
  # ... do stuff on each row
end

For reading from the FTP-server, first load the file into a variable and then use CSV.new(file, options) on it e.g.:

require 'open-uri'
require 'csv'

file = open("http://www.justinmrao.com/salary_data.csv")

CSV.new(file, col_sep: ',').readlines do |row|
  # ... do stuff on each row
end

  • TerryMiddleton likes this

Blog: http://ohm.sh | Twitter: madsohm


#3 TerryMiddleton

TerryMiddleton

    Passenger

  • Members
  • 3 posts

Posted 09 January 2014 - 02:36 AM

Ohm,

 

Thank you for this...worked.  I appreciate you for this.

 

Question.  Where would you put the csv.foreach or csv.new?  Does it belong in the controller or the view. 

 

I put the csv.foreach in the view and tested it out on a local file and it worked like a champ.  On the csv.new, RonR freaked and through a few errors.

 

To me the loop belongs in the view where the other output is, however when I put the csv.new in view it throws an error.

 

Terry



#4 Bharat Soni

Bharat Soni

    Dispatcher

  • Members
  • 53 posts
  • LocationAhmedabad

Posted 09 January 2014 - 05:14 AM

Yes, i have implemented  the export to .xls and .csv file in application.


Fun with Ruby on Rails


#5 Ohm

Ohm

    Guard

  • Members
  • 179 posts
  • LocationCopenhagen

Posted 09 January 2014 - 05:49 AM

Are you using require 'csv' in the view? I would say that it belongs in the controller. The controller should generate a simple array, that the view can run through.

 

This seems like doing the work two times, however it does seem more clean to me.


Blog: http://ohm.sh | Twitter: madsohm


#6 Bharat Soni

Bharat Soni

    Dispatcher

  • Members
  • 53 posts
  • LocationAhmedabad

Posted 09 January 2014 - 12:43 PM

Yes, Following lines i have used in my 

/config/application.rb
require 'csv'
require 'rails/all'

 

And did't write required 'csv' in any of views.


Fun with Ruby on Rails


#7 TerryMiddleton

TerryMiddleton

    Passenger

  • Members
  • 3 posts

Posted 10 January 2014 - 12:26 PM

Okay, so the require 'csv' is in the controller and I suspect if you are getting a file from an FTP source the csv.new should be there also since it's creating an IO object.

 

Then in the view you'd do the for each. 

 

How do you reference the IO object in the view though?

Terry






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users