Click here to Skip to main content
11,934,809 members (42,281 online)
Click here to Skip to main content
Add your own
alternative version


54 bookmarked

Printing to Excel

, 20 Apr 2002
Rate this:
Please Sign up or sign in to vote.
This article shows how to send data to Excel instead of a printer.
<!-- Download Links -->


Printing lists to a printer is a normal thing. But it is better if you can send data to Excel so the user of a business application is able to make changes, send the sheet via e-mail, and so on. Sending data to excel via ODBC is not the best thing, because there is no formatting possible (borders, colours, fonts, ...).

The compiled EXE-file was testet with Win95, Win98, WinNT, Win2000, WinXP and Excel95, Excel97, Excel2000. The developing system was Win98 with Office 97.

How does the system work?

Every report has the following scheme (per page):

  • section: HEADER
  • section: POSITION
  • section: FOOTER

When you send data to excel you have to transform the CRect -structures to rows and columns. The best way is to use only the CRect -structures from the POSITION section for transforming them to columns.

How to use the class CPrintExcel?

  • Append the two files PrintExcel.cpp and PrintExcel.h to your project.

  • Make sure you go to Tools.Options.Directories and add the paths to mso97.dll and vbeext1.olb. Mso97.dll will usually be in c:\Program Files\Microsoft Office\Office, and vbeext1.olb will be in c:\Program Files\Common Files\Microsoft Shared\VBA

    When you look to the demoproject you will find the function: CTestExcelView::OnMenuExcel()

  • The Object PrintExcel is created.

  • The CRect-Elements are transformed to columns.

  • All the data is sent to Excel.

After all this the function

printingToExcel.PrintExcel (ID_EXCEL_CLOSE, NULL, "Clientreport", 0, NULL, ID_FORMAT_PORTRAIT);

is called.

ID_EXCEL_CLOSE sets the sheetname to "Clientreport", printing in portrait-mode and shows Excel.

The function PrintExcel has the following parameters:


is a text, a font, a line, a background sent


the rect-structure you use for printing


the text to print


the backgroundcolor or the textcolor


information for the font to set


justify left, right, center, use masks like "#,##0.00"

when you look at PrintExcel.h you will find the constants for formatting text und the different possible Excel-functions.

As you can see, the body for the functions CTestExcelView::OnDraw(CDC* pDC) and CTestExcelView::OnMenuExcel() is nearly the same. So it should be easy to expand your application with this functionality.

Additional information:

Only the array A## - ZZ## will be transformed, that means there are up to 56 Columns available. It is no problem to expand this. The source for calculating the cols and rows is not difficult, so it is easy to adapt the code to your specific reports.

German uses have to set the variable m_nGerman = TRUE , because the decimal sign must be changed from "," to ".".

English users: Look at "int CPrintExcel::PrintExcel (int nFunction, ..." (File PrintExcel.cpp). You will find

pSheet->Range[COleVariant(cCell)]->PutNumberFormat (COleVariant("#.##0,00"));

If you don't use a German version of Excel it may be necessary to change these lines to " (COleVariant("#,##0.00") ", because the German version uses a different decimal sign. I was not able to test this, because I only have a German version.


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

No Biography provided

You may also be interested in...

Comments and Discussions

GeneralNew email Pin
Franz Maerzinger14-Oct-08 22:36
memberFranz Maerzinger14-Oct-08 22:36 
QuestionExcel File Print Pin
Yaswan28-Oct-07 22:38
memberYaswan28-Oct-07 22:38 
GeneralCOM Functions Pin
sohcher10-Jan-07 22:46
membersohcher10-Jan-07 22:46 
GeneralCOM Functions Pin
sohcher10-Jan-07 22:42
membersohcher10-Jan-07 22:42 
QuestionGetSheets( ) funtion is returning NULL value, why? Pin
kausthubha12-Dec-05 0:03
memberkausthubha12-Dec-05 0:03 
GeneralSome solutions to add Excel2003(XP) support Pin
Roman Cherednik20-Aug-05 14:14
sussRoman Cherednik20-Aug-05 14:14 
GeneralMerging Excel Cells from C++ code Pin
UOzkan8-Jun-05 23:01
memberUOzkan8-Jun-05 23:01 
Hi Franz,

Thank you for your lovely code.
I need to merge 2 or more cells in excel sheet via C++ code.
Is there a way to do it?

Thank you.

AnswerRe: Merging Excel Cells from C++ code Pin
microspy7-Feb-06 1:04
membermicrospy7-Feb-06 1:04 
GeneralRe: Merging Excel Cells from C++ code Pin
UOzkan7-Feb-06 1:32
memberUOzkan7-Feb-06 1:32 
QuestionHow to import excel8.olb?? Pin
xiaosongxia12-May-04 12:25
memberxiaosongxia12-May-04 12:25 
AnswerRe: How to import excel8.olb?? Pin
Franz Maerzinger12-May-04 23:23
memberFranz Maerzinger12-May-04 23:23 
GeneralRe: How to import excel8.olb?? Pin
xiaosongxia14-May-04 16:46
memberxiaosongxia14-May-04 16:46 
GeneralRe: How to import excel8.olb?? Pin
Franz Maerzinger15-May-04 22:36
memberFranz Maerzinger15-May-04 22:36 
GeneralRe: How to import excel8.olb?? Pin
briantmali14-Dec-06 15:55
memberbriantmali14-Dec-06 15:55 
QuestionHow can I speed it up? Pin
Luthov23-Mar-04 13:08
memberLuthov23-Mar-04 13:08 
AnswerRe: How can I speed it up? Pin
Franz Maerzinger24-Mar-04 5:56
memberFranz Maerzinger24-Mar-04 5:56 
GeneralRe: How can I speed it up? Pin
Luthov24-Mar-04 7:24
memberLuthov24-Mar-04 7:24 
GeneralRe: How can I speed it up? Pin
ulf_ulfi8-Apr-05 3:31
memberulf_ulfi8-Apr-05 3:31 
GeneralPrint to image (BMP or JPEG) Pin
talsalmona7-Aug-03 13:31
membertalsalmona7-Aug-03 13:31 
QuestionHow to... Pin
Ridwan Kuswoyo28-Jul-03 18:14
memberRidwan Kuswoyo28-Jul-03 18:14 
AnswerRe: How to... Pin
Franz Maerzinger28-Jul-03 23:11
memberFranz Maerzinger28-Jul-03 23:11 
GeneralRe: How to... Pin
Ridwan Kuswoyo29-Jul-03 1:54
memberRidwan Kuswoyo29-Jul-03 1:54 
GeneralI can't find mso97.dll vbeext1.olb in my computer Pin
hsjwar29-Dec-02 2:17
memberhsjwar29-Dec-02 2:17 
GeneralRe: I can't find mso97.dll vbeext1.olb in my computer Pin
KarstenK9-Dec-02 2:55
memberKarstenK9-Dec-02 2:55 
GeneralRe: I can't find mso97.dll vbeext1.olb in my computer Pin
aircaslte21-Feb-04 16:49
memberaircaslte21-Feb-04 16:49 
GeneralOffice97 PrintOut Method Pin
Kostas Stefanou14-Nov-02 6:16
memberKostas Stefanou14-Nov-02 6:16 
GeneralRe: Office97 PrintOut Method Pin
Franz Maerzinger14-Nov-02 23:05
memberFranz Maerzinger14-Nov-02 23:05 
GeneralRe: Office97 PrintOut Method Pin
Kostas Stefanou15-Nov-02 10:32
memberKostas Stefanou15-Nov-02 10:32 
GeneralRe: Office97 PrintOut Method Pin
Franz Maerzinger17-Nov-02 20:33
memberFranz Maerzinger17-Nov-02 20:33 
GeneralRe: Office97 PrintOut Method Pin
Kostas Stefanou18-Nov-02 9:28
memberKostas Stefanou18-Nov-02 9:28 
QuestionOffeceXP? Pin
Anonymous22-Jul-02 17:47
sussAnonymous22-Jul-02 17:47 
AnswerRe: OffeceXP? Pin
Franz Maerzinger23-Jul-02 23:34
memberFranz Maerzinger23-Jul-02 23:34 
GeneralRe: OfficeXP? Pin
Alexpro2-Sep-02 3:26
memberAlexpro2-Sep-02 3:26 
GeneralRe: OfficeXP? Pin
Franz Märzinger2-Sep-02 3:52
sussFranz Märzinger2-Sep-02 3:52 
GeneralRe: OfficeXP? Pin
Alexpro2-Sep-02 4:03
memberAlexpro2-Sep-02 4:03 
GeneralRe: OfficeXP? Pin
Franz Märzinger2-Sep-02 6:40
sussFranz Märzinger2-Sep-02 6:40 
GeneralRe: OfficeXP? Pin
Tambosi6-Jan-04 2:21
memberTambosi6-Jan-04 2:21 
GeneralPrint to pdf-files Pin
Franz Maerzinger17-Jul-02 22:47
memberFranz Maerzinger17-Jul-02 22:47 
QuestionHow to hide excel menu title,toolbar,status bar? Pin
Anonymous8-Jun-02 22:05
memberAnonymous8-Jun-02 22:05 
Generalcool. big thanks! Pin
Anonymous23-May-02 5:59
memberAnonymous23-May-02 5:59 
GeneralPrinting to Excel Pin
Colin Dailly22-Apr-02 5:53
memberColin Dailly22-Apr-02 5:53 
GeneralRe: Printing to Excel Pin
Franz Maerzinger22-Apr-02 23:23
memberFranz Maerzinger22-Apr-02 23:23 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151126.1 | Last Updated 21 Apr 2002
Article Copyright 2002 by Franz Maerzinger
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid