PDA

View Full Version : Using ruby to open Excel


nbrooks
November 2nd, 2005, 06:53 PM
I wrote a program that makes a comma seperated value file (.csv) and at the end of the program, I want a line of code that opens that file that it just created using Microsoft Excel.

I tried this
system("EXCEL.EXE #{ofile.path}")
but it didnt work

it makes the csv file in the current directory and it seems to be allright except I want it to open it up. My microsoft excel.exe file is located:
C:\Program Files\Microsoft Office\Office10\EXCEL.exe

Thank you

rob
November 2nd, 2005, 08:24 PM
When Office is installed, running excel.exe will run Excel. I tried this on my Windows machine, and noted that it wouldn't run either, returning false. I tried calc.exe though, and that worked perfectly, and returned true.

I know I'm not providing a solution, I'm just curious as to why this wouldn't work. Anyone else that can do some testing?

Rob

rob
November 2nd, 2005, 08:26 PM
I just remembered I'm running Office 11, and not 10. When I ran:

irb(main):013:0> system('C:\Program Files\Microsoft Office\Office11\excel.exe')

I got:

=> true

and it ran Excel.

nbrooks
November 2nd, 2005, 08:55 PM
I can make it open excel but how do I make it open the file i just created (the filename is created by the user in the program)
Code:
print "What would you like to name this file: "
filename = gets.chop
ofile = File.new("./"+filename+".csv","w+")

rob
November 2nd, 2005, 09:23 PM
Sorry for my trip around the world on the bad path then :)

Can you use irb or echo out what your filename is so we can check it?

Rob

cmcknight
November 3rd, 2005, 12:19 AM
path_to_excel = "c:\\Microsoft Office\\Office\\excel.exe"
filename = "test.csv" # or whatever
system("#{path_to_excel} #{filename}")

This seems to work on my machine from irb and a script......:)