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

 
 
Andrew Poelstra
Guest
Posts: n/a
 
      04-28-2006
jacob navia wrote:
> 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;
> }

/Please/ quote you context.

And I love how you used the word "standard" and avoided entirely the
word "portable". The point of having standards is that you have a decent
level of portability.

--
"Every prime number in a series as a joke
Made all the patterns clear when I took that final toke"
- - Andrew Poelstra <http://www.wpsoftware.net/blog>
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      04-29-2006
Walter Roberson wrote:
> CBFalconer <> wrote:
>> jeniffer wrote:

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

>
>> FILE *excelfile;

>
>> if (NULL == (excelfile = fopen("filename", "w")))
>> exit(EXIT_FAILURE);

>
> excel files are binary, and are most commonly used on platforms
> where the distinction between binary and text is meaningful.
> The OP would therefore be advised to use "wb" instead of "w".


The OP specified "excel file". What that consists of is not
specified in any C standard, so lacking specific other
specification, I stand by my solution. I think it was the Red
Queen that said "excel file means exactly what I intend it to
mean".

However it could well turn out that the "wb" is better suited.

--
"The power of the Executive to cast a man into prison without
formulating any charge known to the law, and particularly to
deny him the judgement of his peers, is in the highest degree
odious and is the foundation of all totalitarian government
whether Nazi or Communist." -- W. Churchill, Nov 21, 1943


 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      04-29-2006
jacob navia wrote:
> Keith Thompson wrote:
>> jacob navia <> writes:
>>
>>> 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;
>>> }

>>
>> That's 100% standard and 0% portable -- and it doesn't satisfy the
>> original requirements.
>>
>> Apart from the other non-portabilities that have already been
>> mentioned, I do have MS Excel on my computer, but it's not on my DVD
>> drive (D.
>>
>> If your program is inherently non-portable, there's little point in
>> avoiding the use of non-standard extensions that will do the job
>> better than system() can. Of course, the use of such extensions would
>> make the program off-topic in this newsgroup -- which merely means
>> that it should be discussed in a *different* newsgroup, possibly one
>> full of experts who might know better ways of creating Excel files.

>
> The ShellExecute makes possible to ignore the path of the excel
> executable. system() is not that clever, and the path is in this case
> hardwired. The program is thus non-portable, but can be made to work in
> any machine with Excel with very little effort


Actually, in Windows, there are commands you could have used that would
not have relied on knowing where excel is or even knowing what
application to use. However, how to do that is off topic here.

> Of course, lcc-win32 offers the possibility of using the dispatch
> interface of Excel and dynamically populating the cells, making excel
> display a graphic of the data, whatever. But if I would have posted that
> it would have been a 1500 line program full of windowish code... shudder.
>
> Let's keep it at that. If the original posted is interested, we can
> discuss that in the lcc group.


That is an appropriate redirect. Other Windows groups may have other
solutions available as well.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      04-29-2006
Walter Roberson wrote:
> In article < .com>,
> jeniffer <> 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.


fgets() will read a line including the trailing newline and terminate it
with '\0' as a string in memory.

fputs() will write the string including the newline, if it's there, and
not the terminating '\0'. Actually fputs() will write any string without
regard to newline.
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      04-29-2006
Vladimir Oka wrote:
> 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).
>

Yes, CSV is much easier to do in C but be warned that Excel does a poor
job of reading the .csv file. The Excel user generally has to reformat
various columns.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      05-01-2006
(Walter Roberson) wrote:

> In article <44526732$0$21269$>,
> jacob navia <> wrote, without quoting the
> original text:
>
> >#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;
> >}

>
> That does not create an excel file: at most it creates a file that excel
> is able to read.


And write, to a point.

> There is another problem with the code that could arise especially
> in Germany: comma might be the decimal delimeter (the equivilent
> to the decimal point), and the field delimeter expected might be
> the semi-colon. The 1,2 in the file might be interpreted as 1 + 2/10
> (1.2) instead of as indicating the boundary between two cells.


Amazingly, Excel appears able to read real C(!)SV files even when
Windows is set to non-USAnian setting (at least, to Dutch ones); though
I have yet to find the option to make it write one.

At least CSV files can be written with proper C code, without having to
resort to MS-specific libraries. Normal Excel files can only be
approximated.

Richard
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      05-01-2006
"void * clvrmnky()" <> wrote:

> Andrew Poelstra wrote:
> > Zero wrote:
> >> 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.
> >>

> > You scum. VB isn't a language. "Languages" work in more than one
> > environment.
> >

> Well, the requirements include an application that is basically only
> available on one platform.


No, the requirements include writing a file for that application. This
can be done on a platform on which said application does not run itself.

> In this case VB (or one of the embedded
> VB-like thingies in Office) would be a decent choice.


It rarely if ever is.

Richard
 
Reply With Quote
 
Richard G. Riley
Guest
Posts: n/a
 
      05-02-2006
"jeniffer" <> writes:

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


What type of excel file? Excel supports many type formats for
import/export.

If it is the real "xls" file format then you need to familiarise
youeself with writing to and reading from binary files in C. Google will
show you how. Reading and writing binary files has its own issues over
doing it in normal ascii text mode : there can be platform specific
issues too.

Here is the excel file format explained : I can not comment on the
accuracy. There are many many complications to consider : byte ordering,
structure padding to name but 2.

http://sc.openoffice.org/excelfileformat.pdf

However, maybe you just need to write a text CSV file.

Good luck!
 
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
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57