Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   How to create an excel file through a C program (http://www.velocityreviews.com/forums/t442506-how-to-create-an-excel-file-through-a-c-program.html)

jeniffer 04-28-2006 09:26 AM

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


jacob navia 04-28-2006 09:45 AM

Re: How to create an excel file through a C program
 
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

Ian Collins 04-28-2006 10:03 AM

Re: How to create an excel file through a C program
 
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.

jacob navia 04-28-2006 10:04 AM

Re: How to create an excel file through a C program
 
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;
}

Vladimir Oka 04-28-2006 10:05 AM

Re: How to create an excel file through a C program
 
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>


Walter Roberson 04-28-2006 10:22 AM

Re: How to create an excel file through a C program
 
In article <1146216365.498762.111980@u72g2000cwu.googlegroups .com>,
jeniffer <zenith.of.perfection@gmail.com> 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

Zero 04-28-2006 10:23 AM

Re: How to create an excel file through a C program
 

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.


Vladimir Oka 04-28-2006 10:28 AM

Re: How to create an excel file through a C program
 
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>


Ben C 04-28-2006 10:34 AM

Re: How to create an excel file through a C program
 
On 2006-04-28, jeniffer <zenith.of.perfection@gmail.com> 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.

jacob navia 04-28-2006 12:14 PM

Re: How to create an excel file through a C program
 
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


All times are GMT. The time now is 09:44 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.