reading/writing Excel formats (or CSV)

P

Phil Tomson

I'm supposed to put together an Excel spreadsheet. I've always been the
type of person who would rather write a script than use Excel, but in
this case I need to share data with people who use Excel.

I'm wondering if it's possible to get Ruby to create files that Excel can
read? I suspect that I should be able to use the CSV module for doing
this. Do I need to create a seperate header file or can the header be
placed at the top of the CSV file? (I don't have Excel, so I can't test
this)

So for example, I need to make a spredsheet of students (columns) and
quiz/homework/test scores, something like:

desc student1 student2 student3 student4
quiz1 80 92 78 87
hw1 85 90 81 91
test1 92 100 88 79
hw2 100 99 83 100
final 84 80 77 85

How would I set this up so that the CSV file written by Ruby can be read
by Excel?

I suspect that at the first line of the file I would need to be:
desc, student1, student2, student3, student4
correct? Then I would use CSV to write out the data which I would
probably store in hashes.



Phil
 
C

Caleb Tennis

desc student1 student2 student3 student4
quiz1 80 92 78 87
hw1 85 90 81 91
test1 92 100 88 79
hw2 100 99 83 100
final 84 80 77 85

How would I set this up so that the CSV file written by Ruby can be read
by Excel?

What you have here is perfect. Excel is pretty flexible - you can delimit
by commas, tabs, spaces, whatever you like. The above is definitely more
easily readable than comma delimitation, but I think you'll have yourself
an extra mouseclick or two if you do use commas.

decs,student1,student2,student3,student4
quiz1,80,92,78,87
hw1,85,90,81,91
...
 
C

Caleb Tennis

easily readable than comma delimitation, but I think you'll have yourself
an extra mouseclick or two if you do use commas.

Make that "don't use" commas.
 
H

Hal Fulton

Caleb said:
Make that "don't use" commas.

Funny, I thought you typed "have" instead of "save."
But whatever...


Hal
"Don't use no double negatives."
 
C

Caleb Tennis

Caleb said:
Funny, I thought you typed "have" instead of "save."
But whatever...

You're right. I guess that's what I get for multitasking, and typing
faster than I think.

Let's try it one last time:

Using commas will make the import process go the fastest (in my experience).
 
B

Belorion

I'm supposed to put together an Excel spreadsheet. I've always been the
type of person who would rather write a script than use Excel, but in
this case I need to share data with people who use Excel.

I'm wondering if it's possible to get Ruby to create files that Excel can
read? I suspect that I should be able to use the CSV module for doing
this. Do I need to create a seperate header file or can the header be
placed at the top of the CSV file? (I don't have Excel, so I can't test
this)

So for example, I need to make a spredsheet of students (columns) and
quiz/homework/test scores, something like:

desc student1 student2 student3 student4
quiz1 80 92 78 87
hw1 85 90 81 91
test1 92 100 88 79
hw2 100 99 83 100
final 84 80 77 85

How would I set this up so that the CSV file written by Ruby can be read
by Excel?

I suspect that at the first line of the file I would need to be:
desc, student1, student2, student3, student4
correct? Then I would use CSV to write out the data which I would
probably store in hashes.

Check out Win32OLE for Ruby. You can actually interface directly with
Excel if your heart so desires.
http://homepage1.nifty.com/markey/ruby/win32ole/index_e.html

Matt
 
V

vruz

I'm wondering if it's possible to get Ruby to create files that Excel can
read? I suspect that I should be able to use the CSV module for doing
this. Do I need to create a seperate header file or can the header be
placed at the top of the CSV file? (I don't have Excel, so I can't test
this)

No need for extra modules if you don't need formulas or visual styling.

CSV headers will do. Tab separated fields will work regardless of
locale setting.
If you rename your CSV file as .XLS and double-click it in the
explorer, Excel will open it just like any other spreadsheet.

Also, if you copy tab-delimited CSV data to the clipboard, Excel will
paste it just as well.

If you need more complex stuff like formulas, and visual styles, you
can create Excel XML files, following Microsof'ts schemas:
http://www.microsoft.com/office/xml/default.mspx

If you need interoperability among platforms and open formats you may
also want to have a look at:
http://xml.openoffice.org/

cheers,
vruz
 
D

Dave Baldwin

I'm supposed to put together an Excel spreadsheet. I've always been
the
type of person who would rather write a script than use Excel, but in
this case I need to share data with people who use Excel.

<snip>

I have had some success generating html files and importing these into
Excel. The data is held in a table. One advantage of this over CVS is
the formatting embodied in the html is also imported into Excel.

Dave.
 
L

Lyndon Samson

<snip>

I have had some success generating html files and importing these into
Excel. The data is held in a table. One advantage of this over CVS is
the formatting embodied in the html is also imported into Excel.

Excel as source code control? :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top