Excel and C++

19 views
Skip to first unread message

Misha

unread,
Jan 15, 2006, 4:10:20 PM1/15/06
to C++ Game Programming
I'm sure this doesn't have much to do with gaming but it could. Either
way if someone happens to know some info on the following topic i'd
enjoy some help.

Here's what I need to do: I'm going to use C++ to produce data that I
then would like to enter into an Excel worksheet and make a graph from
it, and then display that graph. All from the user simply opening the
program.

I havn't got a clue as to how to import excel into C++ and make them
share graphs and so forth.

jakob lin

unread,
Jan 15, 2006, 7:37:57 PM1/15/06
to C-Game-Pr...@googlegroups.com
ummm why don't you just do c++ program where all the user has to do is to input data and the program made it excel     no need to do that many steps :P  
--
Jakob Lin Mall

Adam Nester

unread,
Jan 15, 2006, 11:06:42 PM1/15/06
to C-Game-Pr...@googlegroups.com
You can always do a camma seperated variable file with excel which can be imported by c++ rather easily.
--
Adam R. Nester
15052 Old Conroe Road
Conroe TX, 77384
Home: (936)321-9210
Cell: (936)443-4975

Andreas Tropschug

unread,
Jan 16, 2006, 4:11:30 AM1/16/06
to C-Game-Pr...@googlegroups.com
Although I really don't see any way this could be related to gaming,
unless you'd keep very complex highscore lists ;-) ,
here's what I'd do:
Excel provides an ActiveX object model which makes it
easy to remote control excel.

All you have to do is to create an ActiveX excel object:
enslavedExcel = CreateObject ("Excel.Application",...)
(or something like that)
Then you can do something like:
enslavedExcel->NewWorksheet();
You can do anything with it.

How to do this in c++ can be easily found in the msdn.
And if you need some convenient help on what you can do with
your Excel object, check the vba for excel (macro) documentation,
because if I remember correctly code completion will not help you much.

Best regards,
Andreas

2006/1/15, Misha <msha...@gmail.com>:

JeffCameron

unread,
Jan 26, 2006, 8:56:34 PM1/26/06
to C++ Game Programming
void addNumberToExcelSheet(char *fileName, double number)
{
FILE *fp = fopen(fileName, "a");

fprintf(fp, "%f\n", number);

fclose(fp);
}

void clearExcelSheet(char *fileName)
{
fclose(fopen(fileName, "w"));
}

I'm not exactly clear on what exactly it is that you're trying to do,
but these functions should be of some help. Here's how they work.
clearExcelSheet(fileName) is used to create a new Excel worksheet or to
delete the contents of one that already exists. For example, the
following C++ program will create a new Excel sheet called
'MySheet.xls'

#include <stdio.h>

void clearExcelSheet(char *fileName)
{
fclose(fopen(fileName, "w"));
}

int main()
{
clearExcelSheet("MySheet.xls");
return 0;
}

Run this program, then open the folder where the program is running
from. There should be an empty Excel worksheet there. But how do you
fill it with numbers? Just use the addNumberToExcelSheet function,
like this:

#include <stdio.h>

void addNumberToExcelSheet(char *fileName, double number)
{
FILE *fp = fopen(fileName, "a");

fprintf(fp, "%f\n", number);

fclose(fp);
}

void clearExcelSheet(char *fileName)
{
fclose(fopen(fileName, "w"));
}

int main()
{
int i;

clearExcelSheet("MySheet.xls");

addNumberToExcelSheet("MySheet.xls", 3.14);

for (i = 0 ; i < 5 ; i++)
addNumberToExcelSheet("MySheet.xls", (double) i);

addNumberToExcelSheet("MySheet.xls", 1337);

return 0;
}

Compile and run the above C++ program, then look in the folder where
the program file is. There should be an Excel sheet by the name of
MySheet.xls. Inside that Excel sheet, there will be the following
contents:

3.14
0
1
2
3
4
1337

All you have to do now is make a chart. I hope this answers your
question. If you were hoping to do somethign a bit more complex, like
makign a full two-dimensional table of numbers, just let me know and I
can help you out with some code. A clearer description of your problem
always helps get better replies to your posts.

Jeff Cameron

Misha

unread,
Jan 31, 2006, 11:01:38 PM1/31/06
to C++ Game Programming
Thank you guys very much for your replies!

Especially Jeff, that was exactly what I was looking for. Nice and
simple too! So is the program just basically making a file called
MySheet.xls and since it ends with xls windows decides to make it
Excel? Wonderful.

As a side note, I was wondering if there is anything more Excel
specific that I can do with just a C++ program, such as:
1) Forcing this excel file to open after my code executes.
2) Forcing Excel to create a graph for me and have it ready when the
file opens(assuming (1) is possible.

Again, thank you very very much for your reply.

Sincerely,
Mikhail (Misha) Shashkov Jr.

Reply all
Reply to author
Forward
0 new messages