Click here to Skip to main content
12,633,764 members (27,870 online)
Click here to Skip to main content

Stats

208.7K views
3.4K downloads
54 bookmarked
Posted

Printing to Excel

, 20 Apr 2002
This article shows how to send data to Excel instead of a printer.
// TestExcelView.cpp : Implementierung der Klasse CTestExcelView
//

#include "stdafx.h"
#include "TestExcel.h"

#include "TestExcelDoc.h"
#include "TestExcelView.h"

#include "PrintExcel.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTestExcelView

IMPLEMENT_DYNCREATE(CTestExcelView, CView)

BEGIN_MESSAGE_MAP(CTestExcelView, CView)
	//{{AFX_MSG_MAP(CTestExcelView)
	ON_COMMAND(ID_MENU_EXCEL, OnMenuExcel)
	//}}AFX_MSG_MAP
	// Standard-Druckbefehle
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestExcelView Konstruktion/Destruktion

CTestExcelView::CTestExcelView()
{
	// ZU ERLEDIGEN: Hier Code zur Konstruktion einf�gen,

}

CTestExcelView::~CTestExcelView()
{
}

BOOL CTestExcelView::PreCreateWindow(CREATESTRUCT& cs)
{
	// ZU ERLEDIGEN: �ndern Sie hier die Fensterklasse oder das Erscheinungsbild, indem Sie
	//  CREATESTRUCT cs modifizieren.

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CTestExcelView Zeichnen

void CTestExcelView::OnDraw(CDC* pDC)
{
	CTestExcelDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// Todo

	
	CFont		*pFontOld = pDC->SelectObject(&(pDoc->m_fontNormal));
	COLORREF	rgbOld = pDC->SetBkColor (RGB(255, 255, 255));
	int			nField;


	for (nField=1; nField<=pDoc->m_nFieldsHeader; nField++) {
		// one is bold, one is normal
		if (nField % 2)
			pDC->SelectObject(&(pDoc->m_fontNormal));
		else
			pDC->SelectObject(&(pDoc->m_fontBold));

		pDC->ExtTextOut (pDoc->m_rectHeader[nField].left, 
						 pDoc->m_rectHeader[nField].top, 
						 ETO_CLIPPED, &(pDoc->m_rectHeader[nField]), 
						 pDoc->m_strHeader[nField], NULL);

	}
	

	for (nField=1; nField<=pDoc->m_nFieldsPosition; nField++) {
		// use different background colors for demo
		if ((nField % 3)==0) pDC->SetBkColor (RGB(255, 0, 0));
		if ((nField % 3)==1) pDC->SetBkColor (RGB(0, 255, 0));
		if ((nField % 3)==2) pDC->SetBkColor (RGB(192, 192, 192));
		pDC->ExtTextOut (0, 0, ETO_OPAQUE, &(pDoc->m_rectPosition[nField]), _T(""), 0, NULL);

		// use different text colors for demo
		if ((nField % 3)==0) pDC->SetTextColor (RGB(0, 0, 255));
		if ((nField % 3)==1) pDC->SetTextColor (RGB(192, 0, 192));
		if ((nField % 3)==2) pDC->SetTextColor (RGB(0, 0, 0));

		if (nField % 2)
			pDC->SelectObject(&(pDoc->m_fontNormal));
		else
			pDC->SelectObject(&(pDoc->m_fontBold));
		pDC->ExtTextOut (pDoc->m_rectPosition[nField].left, 
						 pDoc->m_rectPosition[nField].top, 
						 ETO_CLIPPED, &(pDoc->m_rectPosition[nField]), 
						 pDoc->m_strPosition[nField], NULL);

	}
	
	pDC->SetBkColor (rgbOld);
	pDC->SelectObject(pFontOld);
	

}

/////////////////////////////////////////////////////////////////////////////
// CTestExcelView Drucken

BOOL CTestExcelView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// Standardvorbereitung
	return DoPreparePrinting(pInfo);
}

void CTestExcelView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// ZU ERLEDIGEN: Zus�tzliche Initialisierung vor dem Drucken hier einf�gen
}

void CTestExcelView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// ZU ERLEDIGEN: Hier Bereinigungsarbeiten nach dem Drucken einf�gen
}

/////////////////////////////////////////////////////////////////////////////
// CTestExcelView Diagnose

#ifdef _DEBUG
void CTestExcelView::AssertValid() const
{
	CView::AssertValid();
}

void CTestExcelView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CTestExcelDoc* CTestExcelView::GetDocument() // Die endg�ltige (nicht zur Fehlersuche kompilierte) Version ist Inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTestExcelDoc)));
	return (CTestExcelDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTestExcelView Nachrichten-Handler

/////////////////////////////////////////////////////////////////////////////
// send data to excel
void CTestExcelView::OnMenuExcel() 
{
	// TODO: 
	CWaitCursor	waitCursor;	// 

	CPrintExcel	printingToExcel;

	COLORREF	rgb;
	LOGFONT		logFont;
	int			nFormat;
	CRect		rect;
	int			nField;

	CTestExcelDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);


	// Step one: calculate columns, use POSTION rects
	for (nField=1; nField<=pDoc->m_nFieldsPosition; nField++) {
		printingToExcel.CalcExcelCols (pDoc->m_rectPosition[nField]);
	}
	
	// Step two: print it
	for (nField=1; nField<=pDoc->m_nFieldsHeader; nField++) {
		// one is bold, one is normal
		if (nField % 2)
			pDoc->m_fontNormal.GetLogFont (&logFont);
		else
			pDoc->m_fontBold.GetLogFont (&logFont);
		rgb= RGB(0, 0, 0);	// black text

		// Set the font and the colortext		
		printingToExcel.PrintExcel (ID_EXCEL_TYPTEXTFONT, 
									&(pDoc->m_rectHeader[nField]),
									"", rgb, &logFont);

		// print the text normal
		nFormat = ID_FORMAT_LEFT;
		printingToExcel.PrintExcel (ID_EXCEL_TYPTEXT, 
									&(pDoc->m_rectHeader[nField]),
									pDoc->m_strHeader[nField], NULL, NULL, 
									nFormat); //, nOben);

	}
	
	// paint a frame
	rect.top = 0;
	rect.bottom = 30;
	rect.left = 10;
	rect.right = 380;

	printingToExcel.PrintExcel (ID_EXCEL_TYPLINESINGLETOP, &rect);
	printingToExcel.PrintExcel (ID_EXCEL_TYPLINESINGLELEFT, &rect);
	printingToExcel.PrintExcel (ID_EXCEL_TYPLINESINGLERIGHT, &rect);
	printingToExcel.PrintExcel (ID_EXCEL_TYPLINEDOUBLEBOTTOM, &rect);

	


	for (nField=1; nField<=pDoc->m_nFieldsPosition; nField++) {
		// use different background colors for demo
		if ((nField % 3)==0) rgb= RGB(255, 0, 0);
		if ((nField % 3)==1) rgb= RGB(0, 255, 0);
		if ((nField % 3)==2) rgb= RGB(192, 192, 192);
		printingToExcel.PrintExcel (ID_EXCEL_TYPBACKGROUND,
									&(pDoc->m_rectPosition[nField]),
									"", rgb);

		// use different text colors for demo
		if ((nField % 3)==0) rgb= RGB(0, 0, 255);
		if ((nField % 3)==1) rgb= RGB(192, 0, 192);
		if ((nField % 3)==2) rgb= RGB(0, 0, 0);

		if (nField % 2)
			pDoc->m_fontNormal.GetLogFont (&logFont);
		else
			pDoc->m_fontBold.GetLogFont (&logFont);

		// Set the font and the colortext		
		printingToExcel.PrintExcel (ID_EXCEL_TYPTEXTFONT, 
									&(pDoc->m_rectPosition[nField]),
									"", rgb, &logFont);


		// print the text centered
		nFormat = ID_FORMAT_CENTER;	// center it
		printingToExcel.PrintExcel (ID_EXCEL_TYPTEXT, 
									&(pDoc->m_rectPosition[nField]),
									pDoc->m_strPosition[nField], NULL, NULL, 
									nFormat); //, nOben);


	}
	
	// Set the name of the number sheet, use portrait, show excel
	printingToExcel.PrintExcel (ID_EXCEL_CLOSE, NULL, "Clientreport", 0, NULL, ID_FORMAT_PORTRAIT);	
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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

Share

About the Author

No Biography provided

You may also be interested in...

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