Click here to Skip to main content
Click here to Skip to main content

Create Excel-files with a simple class

By , 9 Mar 2007
 

Introduction

When I wrote my latest application, I had to find a way to print data from a CListCtrl. Since I am not very familiar with printing in MFC, I thought about exporting the data to Excel and leaving all the formating and printing to Excel.

My first attempt was to create a CSV-file with all required data. It worked fine, but users complained that they had to do the formating all by themselves. So I tried to find out how to create XLS-files.

With Excel 2003 it is possible to import XML-files. So I created an XML-file and analyzed it. The result is a new class that can help generating Excel-files from with your MFC-Application.

How it works

Excel-XML-files contain a number of predefined styles. Each cell that is defined needs its own style. So when using different format-options, you may end up with a large number of styles. These styles have to be defined in the beginning of your XML-file. Furthermore, you have to define every column that you use in your XML file. And you need to know exactly how many lines of data you will have in your XML-file.

So whenever you create an XML-file, a file in your temp-folder is created. Excel can be opened with this XML-file and afterwards you may delete your temporary XML-file. For compatibility reasons, I left the creation of CSV-files in my class, in case you do not have Excel installed on your system, but want to export your data.

Steps

The first step is to add the files Excel.cpp and Excel.h to your project. Include Excel.h in your source wherever you want to create XML-files.

To start the export you have to declare a variable of type CExcel. The constructor can also be used with a part of a filename. All temporary files will begin with this string. You can create your own files in your temp-folder. The default for the beginning of your files is XLS.

CExcel export("TST");

The next step is to add all the columns you want to have in your file:

export.AddColumn(120.3);
export.AddColumn();

Use AddCloumn() for every additional column you need. You can specify the width of this column. If you do not specify a value, Excel uses a default.

The Next step is a bit tricky. You have to define the styles you want to use. Every cell of data may have its own style, fontsize and datatype. AddStyle() returns a unique identifier for every style you create:

int bold,center,bolddate,large;

bold=export.AddStyle(XLS_BOLD);
center=export.AddStyle(XLS_HCENTER|XLS_VCENTER);
bolddate=export.AddStyle(XLS_BOLD|XLS_DATE);
large=export.AddStyle(XLS_NOSTYLE,"16");

The above example creates a style (stored in bold) which simply sets the font to bold. Center centers the value horizontally and vertically. bolddate will hold the identifier for a style that displays a bold date. Finally, large sets the fontsize to 16 pt.

You can use a combination of these flags in setting your styles:

XLS_NOSTYLE         No change in style

XLS_HCENTER         horizontaly centered
XLS_HLEFT           left aligned
XLS_HRIGHT          right aligned
XLS_VTOP            aligned at the top of the cell
XLS_VCENTER         vertically centered
XLS_VBOTTOM         aligned at the bottom of the cell
XLS_WRAP            text is wrapped to fit in the cell
XLS_BORDER          there is a single line border around the cell

XLS_BOLD            the font is bold
XLS_LINE            the font is underlined with a single line
XLS_ITALICS         the font is italics

XLS_DATE            the cell contains a date
XLS_TIME            the cell contains a time
XLS_CURRENCY        the cell contains a currency

Each combination of these flags result in a new style. CExcel stores the styles in a CStringArray, so there is no limitation of styles.

After defining all styles and adding all columns, you can start to create your XML-file with:

export.Open(12,"Test");

Excel needs to know the number of lines before the lines are written, so you have to specify this number now. You also have to give a name for the Excel-worksheet.
If you use Open() without parameters, no XML-file will be created. Instead a CSV-file will be created.

Open() writes a lot of XML-header stuff in your file, defines all the styles and columns you added.

After opening the file, you can add rows of data. Each row must begin with NewRow().

export.NewRow(bold,25.3);

This begins a new row. The row is in the bold-style, and has a height of 25.3. If you do not specify any values, Excel-defaults are used. If you simply want to set the new height and don't want to use a style, observe the following example:

export.NewRow(XLS_NOSTYLE,40.2);

After a row is started, you can add values to every cell in your row:

export.SetCell("This is for strings");
export.SetCell(129.3); // for float
export.SetCell(2); // for integer

You can also specify a style:

export.SetCell("this will be bold",bold);
export.SetCell(2,large); // a really big 2
export.SetCell(23.33,currency); // as far as you have defined currency-style

You can also use a CTime-object in SetCell(). However, you have to specify a style defining the CTime as date or time:

int aTime=export.AddStyle(XLS_TIME|HCENTER);
CTime now;

export.SetCell(now,aTime); // a time horizontally centered

When you have set all values for a cell, you have to end your row with the following command: export.EndRow(); Now you can start your next row.

When you have finished with all data, you have to close your file using: export.Close();

This means all XML-tags will be closed and the temporary file is saved.

export.Run(); will open Excel with your XML-file. Or with your CSV-file, if you did not specify parameters when opening your file.

Use export.Delete(); to delete a CSV-file after showing. XML-files should not be deleted when Excel is still running, so use:

CExcel export;
export.DeleteAll();

instead in your OnClose() function.

Complete Example

Here is a complete example:

void OnExport()
{
    CExcel export("MYTMP");             // Create Files starting with MYTMP
    int x_bold,x_center,x_date,x_money; // all the styles I want
    CTime now=CTime::GetCurrentTime();  // this is now

    export.AddColumn();                 // first column    
    export.AddColumn();    
    export.AddColumn(123.3);            // a wide column

    x_bold=export.AddStyle(XLS_BOLD,"16");     // a bold-Font, 16 pt
    x_center=export.AddStyle
        (XLS_HCENTER|XLS_VCENTER|XLS_WRAP);    // Centered text
    x_date=export.AddStyle(XLS_DATE);          // used to format a date
    x_money=export.AddStyle(XLS_CURRENCY);     // used for currencies

    export.Open(3,"Example");           // Open the file with 3 rows

    export.NewRow(x_bold,23);           // a row of bold Text
    export.SetCell("Date");             // some headlines
    export.SetCell("Income");
    export.SetCell("Why");
    export.EndRow();                    // End of first row
    export.NewRow();                    // an empty line
    export.EndRow();
    export.NewRow();                    // next row
    export.SetCell(now,x_date);         // the CTime formatted as date
    export.SetCell(72637.23,x_money);   // a float value formatted as
                                        // currency
    export.SetCell("This is just long text, 
        which will be centered and wrapped around",x_center);
    export.EndRow();
    export.Close();                     // closing the file
    export.Run();                       // starting Excel and display data
}

void OnClose()
{
    CExcel export("MYTMP");
    export.DeleteAll();            // Delete all export-files
    CDialog::OnClose();
}

History

March 7th, 2007 First release

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

e-DJ
Web Developer
Germany Germany
Member
Juergen Wagner just lives in Augsburg, Germany and writes applications to help him out in his job as CIO in a mental hospital.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionexport.Open() [modified]membernavifb2 Feb '12 - 23:10 
Hello,
 
If I put for example
export.Open(10,"");
CString aMappe = null and then FName=FName+".CSV";
Then when I call export.Run(), the application open Excel without format-->It is OK
 
but if I put for example
export.Open(10,"T");
CString aMappe = is not null and then FName=FName+".XML";
and it is my doubt, I think that it worked ok, but now I call export.Run() and the application create the xml correctly, but no run Excel... it open explorer with xml structure
 
What's your opinion?
 
Other thing,
I believe that MakeXML works ok, with CSV but not with .XML. With norway chraracters the XML return me errors
 
Thank you

modified 3 Feb '12 - 7:01.

Questionerrormembernvd018619 Jul '11 - 13:45 
I am getting an error
error C2440: 'default argument' : cannot convert from 'const char' to 'CString'
at
CExcel(CString aTemp="XLS");
void Open(int aLineCount=0, CString aMappe="");
int AddStyle(WORD aBit, CString aSize="");
 
can anyone help me out with this????
GeneralThank youmemberfuturejo31 Oct '10 - 1:45 
It's very help to me.
Generalerror C2061: syntax error : identifier 'CString'memberRan Aloni14 Oct '10 - 2:29 
Hi
do you know how I could enable the usage of file types like CString, BOOL, DWORD
you files won't compile because it doesn't recognize them
 
please help ....
GeneralError with GetTempPathmemberthaiguy_md14 Jun '10 - 21:38 
Hi!
 
I'm getting errors like:
GetTempPath(sizeof(temppath),temppath);
error C2664: 'GetTempPathW' : cannot convert parameter 2 from 'char [256]' to 'LPWSTR'
 
and here: handle=fopen(FName,"wt");
error C2664: 'fopen' : cannot convert parameter 1 from 'CString' to 'const char *'
 
What it could be?
Thanks
QuestionSetCell has a small bug?memberqifefe20 Sep '09 - 20:40 
hello, Juergen Wagner:
I got u's "Create Excel-files with a simple class" project form www.codeproject.com. It is very usful and terse.
But there is a small bug.
============Bug describe==
I try SetCell in CString format, aData evaluate a word "中国"("china" in chinese);
it can't work fine.
============Describe end==
AnswerRe: SetCell has a small bug?memberqifefe20 Sep '09 - 22:35 
I have found the solution.
"simple chinese" language usually used the encoding = "gb2312", so change the first line
CONVERT <?xml version="1.0"?> TO <?xml version="1.0" encoding="gb2312"?>,
it OK!
Smile | :)
QuestionHow can i use Merge Cells proprty??membermegha_gharote20 Oct '08 - 20:51 
Hello Friends,
 
I have used this code to export data from a dilog box to excel. I want to use Merge cells property as a style..
 
Can anybody help me out with this??
 
Thanks in advance
Megha
QuestionHow to export grid line of ListCtrl to Excel file?membergullitlee21 May '08 - 17:33 
I can export to excel,but without grid line,need set again...
How to export grid line of ListCtrl to Excel file?
If anyone know ....
Please....Thanks!
GeneralThe first step is to add the files Excel.cpp and Excel.h...memberGordon Smith20 Dec '07 - 4:53 
...but from where?
 
Are these available from Microsoft? Do they come with Visual Studio? I've had a look around but to no avail.
 
Thanks.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 9 Mar 2007
Article Copyright 2007 by e-DJ
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid