// ole test3Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "ole test3.h"
#include "ole test3Dlg.h"
#include "excel.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// COletest3Dlg dialog
_Application app;

COletest3Dlg::COletest3Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(COletest3Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(COletest3Dlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void COletest3Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COletest3Dlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(COletest3Dlg, CDialog)
	//{{AFX_MSG_MAP(COletest3Dlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnOpenExcel)
	ON_BN_CLICKED(IDC_BUTTON3, OnReadData)
	ON_BN_CLICKED(IDC_BUTTON4, OnSaveExit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COletest3Dlg message handlers

BOOL COletest3Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void COletest3Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

HCURSOR COletest3Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void COletest3Dlg::OnButton1() 
{

	
if(!app.CreateDispatch("Excel.Application")) //запустить сервер
{
AfxMessageBox("Ошибка при старте Excel!");
return;
}
else
{
	app.SetSheetsInNewWorkbook(1); 
	app.SetVisible(TRUE); //и сделать его видимым
	pWorkbooks=app.GetWorkbooks();
	pWorkbooks.Add(COleVariant((short)1));
	
	long chan;
	CString it = "regi", gn = "1352", cl = "regedit";
	chan = app.DDEInitiate("progman","progman");
	app.DDEExecute(chan,"[CreateGroup("+gn+")]");
	app.DDEExecute(chan,"[AddItem("+cl+","+it+",,)]");

	app.DDETerminate(chan);	
}
}


void COletest3Dlg::OnOpenExcel() 
{
	// TODO: Add your control notification handler code here
CString str;
m_worksheets=app.GetWorksheets();
m_worksheet=m_worksheets.GetItem(COleVariant((short)1));
pRange=m_worksheet.GetRange(COleVariant("A1"),COleVariant("C2"));
GetDlgItem(IDC_EDIT1)->GetWindowText(str);
pRange.SetValue2(COleVariant(str));

}

void COletest3Dlg::OnReadData() 
{
	pRange = m_worksheet.GetRange(COleVariant("A1"),COleVariant("A1"));
	GetDlgItem(IDC_STATIC)->SetWindowText((CString ) pRange.GetValue2().bstrVal);
}


void COletest3Dlg::OnSaveExit() 
{
	// TODO: Add your control notification handler code here
	CString c;
	c = "c:\\Test.xls";
	m_worksheet.SaveAs(c,COleVariant((short) 1 ),COleVariant(""),COleVariant(""),COleVariant((short)0),COleVariant((short)0),COleVariant((short)0),COleVariant(""),COleVariant(""),COleVariant(""));
	app.Quit();
;
	
}
Соседние файлы в папке 2.1