Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to create an excel file through a C program

Reply
Thread Tools

How to create an excel file through a C program

 
 
jeniffer
Guest
Posts: n/a
 
      04-28-2006
I need to create an excel file through a C program and then to populate
it.How can it be done?

 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      04-28-2006
jeniffer wrote:
> I need to create an excel file through a C program and then to populate
> it.How can it be done?


This is not really on topic here but...

>

#include <windows.h>
#include <shellapi.h>
#include <stdio.h>
int main(void)
{
FILE *ExcelFile = fopen("testdata.csv","w");
if (ExcelFile == NULL)
return -1;
fprintf(ExcelFile,"Column1,Column2,Column3\n1,2,3\ n");
fclose(ExcelFile);
ShellExecute((HWND)0,"open","testdata.csv",NULL,NU LL,SW_SHOW);
}

Compiled with lcc-win32
http://www.cs.virginia.edu/~lcc-win32
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      04-28-2006
jeniffer wrote:
> I need to create an excel file through a C program and then to populate
> it.How can it be done?
>

Ask microsoft for the file format, then write the code to produce the file.

--
Ian Collins.
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      04-28-2006
In 100% standard C you can do it with

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *ExcelFile = fopen("testdata.csv","w");
if (ExcelFile == NULL)
return -1;
fprintf(ExcelFile,"Column1,Column2,Column3\n1,2,3\ n");
fclose(ExcelFile);
system("D:\\Program Files\\Microsoft Office\\Office\\excel.exe
testdata.csv");
return 0;
}
 
Reply With Quote
 
Vladimir Oka
Guest
Posts: n/a
 
      04-28-2006
jeniffer opined:

> I need to create an excel file through a C program and then to
> populate it.How can it be done?


By learning about Excel file format (if M$ lets you), which is
off-topic here. Once you do, and you have some C code that gives you
troubles, feel free to come back here for help.

On the most basic level, you'd need `fopen()`, `fwrite()`, and friends
from <stdio.h>.

--
Under every stone lurks a politician.
-- Aristophanes

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      04-28-2006
In article <(E-Mail Removed) .com>,
jeniffer <(E-Mail Removed)> wrote:
>I need to create an excel file through a C program and then to populate
>it.How can it be done?


fopen() the file with "wb" (write binary) mode, and
then fwrite() or fprintf() or putc() or fputc() whatever you need to.

You should probably avoid putw() and fputs(), though:
putw() works in terms of the type "int", which is not the same
size on all systems; and fputs() includes a terminating newline,
which is not the same character(s) on all systems.


What you need now is to know what the structure is of an excel file.
That's a topic beyond the scope of standard C, and is
subject to change without notice from Microsoft ("Documenting
a file structure hurts our ability to innovate!!") Inc.

You can find a long paper on the file format by googling for
excel file structure
for example, http://sc.openoffice.org/excelfileformat.pdf
has OpenOffice's documentation up to Excel 2003. You will likely
find the mass of information there rather daunting, and chances
are extremely high that if you were to attempt to implement the
full range yourself that you (or anyone) would make mistakes.
I would therefor suggest to you that you should either attempt to
find a pre-written Excel library (perhaps OpenOffice offers one),
or else that you take a big portability hit by confining yourself
to Windows and using one of Microsoft's development APIs.
Microsoft's APIs are discussed in microsoft-specific newsgroups.
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
 
Reply With Quote
 
Zero
Guest
Posts: n/a
 
      04-28-2006

jeniffer schrieb:

> I need to create an excel file through a C program and then to populate
> it.How can it be done?


Better you use in this case Visual Basic! Much easier and comfortable.

 
Reply With Quote
 
Vladimir Oka
Guest
Posts: n/a
 
      04-28-2006
Zero opined:

>
> jeniffer schrieb:
>
>> I need to create an excel file through a C program and then to
>> populate it.How can it be done?

>
> Better you use in this case Visual Basic! Much easier and
> comfortable.


Another possibility: use CSV format (comma separated values).

--
Beware of the Turing Tar-pit in which everything is possible but
nothing of interest is easy.

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

 
Reply With Quote
 
Ben C
Guest
Posts: n/a
 
      04-28-2006
On 2006-04-28, jeniffer <(E-Mail Removed)> wrote:
> I need to create an excel file through a C program and then to populate
> it.How can it be done?


Probably create a .csv file (which you can do with normal fprintf and
string processing in C), and use Excel to import that.

Otherwise there is possibly a library, or some way to use Excel as a
co-process, you'd have to find out about Office and Microsoft
architecture.
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      04-28-2006
Flash Gordon wrote:
> Of course, if you had used the system call provided by the standard you
> would not have needed the non-standard headers and it would have
> compiled and worked with many more compilers. Such as the gcc
> implementation that is part of Cygwin.


I did exactly that, see my other post
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Problem with Excel reports ::::Excel 2003 Migration To Excel 2007 =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?= ASP .Net 15 10-24-2007 01:34 PM
excel.h - Automating Excel... How to Create new worksheets Jona C++ 2 07-06-2007 03:35 PM
ExtenXLS loads data into Excel 2002 file but damages the Excel file. kp2900@gmail.com Java 1 11-21-2006 05:48 PM
exporting an excel file from database; making changes to excel file and updating the database by importing it back Luis Esteban Valencia ASP .Net 1 01-12-2005 12:28 AM



Advertisments