📄 exceltestdlg.cpp
字号:
// ExcelTestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ExcelTest.h"
#include "ExcelTestDlg.h"
#include "Excel.h"
#include "comdef.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CExcelTestDlg dialog
CExcelTestDlg::CExcelTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CExcelTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CExcelTestDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CExcelTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CExcelTestDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CExcelTestDlg, CDialog)
//{{AFX_MSG_MAP(CExcelTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CREATEXCEL, OnCreatexcel)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CExcelTestDlg message handlers
BOOL CExcelTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
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
}
void CExcelTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 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 CExcelTestDlg::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();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CExcelTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CExcelTestDlg::OnCreatexcel()
{
_Application m_excelApp;//定义Excel提供的应用程序对象
Workbooks m_excelBooks;
_Workbook m_excelBook;
Sheets m_Sheets;
_Worksheet m_WorkSheet;
Range m_Range;
Interior m_Inter;
CoInitialize(NULL); //初始化Com库,必须调用
m_excelApp.m_bAutoRelease=true;
if (!m_excelApp.CreateDispatch("Excel.Application")) //创建Excel服务器
{
AfxMessageBox("创建Excel服务失败!");
return;
}
m_excelApp.SetVisible(FALSE); //设置为隐藏
//利用模板文件建立新文档
COleVariant varFormat((short)-4143);
COleVariant varCenter((short)-4108);
COleVariant varIndex((short)1);
COleVariant varFalse((short)0,VT_BOOL);
COleVariant varColorIndex((short)6);
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);//
COleVariant varRange1("A1",VT_BSTR);
COleVariant varRange2("D1",VT_BSTR);
COleVariant varText("TextTest",VT_BSTR);
m_excelBooks.AttachDispatch(m_excelApp.GetWorkbooks(),true);
m_excelBook.AttachDispatch(m_excelBooks.Add(_variant_t(""))); //
m_Sheets=m_excelBook.GetSheets();
m_WorkSheet=m_Sheets.GetItem(varIndex);
m_WorkSheet.SetName("MySheet");
m_Range=m_excelApp.GetRange(varRange1,varRange2);
m_Range.Select();
m_Range.SetHorizontalAlignment(varCenter);
m_Range.SetVerticalAlignment(varCenter);
m_Range.SetWrapText(varFalse);
m_Range.SetOrientation(varFalse);
m_Range.SetAddIndent(varFalse);
m_Range.SetIndentLevel(varFalse);
m_Range.SetShrinkToFit(varFalse);
m_Range.SetReadingOrder(-5002);
m_Range.SetMergeCells(varFalse);
m_Range.Merge(covOptional);
m_Range=m_excelApp.GetActiveCell();
m_Range.SetFormulaR1C1(varText);
m_Inter=m_Range.GetInterior();
m_Inter.SetColorIndex(varColorIndex);
m_Inter.SetPattern(varIndex);
m_excelBook.SaveAs(_variant_t("c:\\ExcelTest.xls"),varFormat,covOptional,
covOptional,
covOptional,
covOptional,
0,
covOptional,
covOptional,
covOptional,
covOptional,
covOptional);
m_excelApp.Quit();//退出
m_Inter.ReleaseDispatch();
m_Range.ReleaseDispatch();
m_excelBooks.ReleaseDispatch();
m_excelBook.ReleaseDispatch();
m_excelApp.ReleaseDispatch();
::CoUninitialize();
AfxMessageBox("c:\\ExcelTest.xls生成完毕");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -