如何在MFC对话框中嵌入Excel表格

如何在MFC对话框中嵌入Excel表格,第1张

1. 打开 Excel 工作表,选定要嵌入的对象。 ...

2. 单击常用工具栏中的〔复制〕按钮,将选中的对象复制到“剪贴板”中。 ...

3. 回到正在编辑的 Word 文档中,定位插入点,再单击“编辑”菜单中的“选择性粘 贴...

4. 在d出的“选择性粘贴”对话框中,选择“粘贴”单选框,表示将对象嵌入 Word 文档。

使用应用程序向导新建一个名为“Embed_Excel”的 MFC 应用程序向导 (EXE) 项目。选择“单文档”作为要创建的应用程序类型,并选择“容器”作为要包括的复合文档支持类型。接受所有其他默认设置。将生成下面几个类:Application:Embed_Excel.h 和 Embed_Excel.cpp 中的 CEmbed_ExcelAppFrame:MainFrm.h 和 MainFrm.cpp 中的 CMainFrameDocument:Embed_ExcelDoc.h 和 Embed_ExcelDoc.cpp 中的 CEmbed_ExcelDocView:Embed_ExcelView.h 和 Embed_ExcelView.cpp 中的 CEmbed_ExcelViewContainer Item:CntrItem.h 和 CntrItem.cpp 中的 CEmbed_ExcelCntrItem在视图菜单上,单击类向导。单击自动化选项卡,单击添加类,并选择从类型库。找到 Microsoft Excel 类型库,然后将类型库中的所有类都添加到您的项目中。对于 Excel 97,类型库位于 Excel8.olb。对于 Excel 2000,类型库位于 Excel9.olb;对于 Excel 2002,类型库位于 Excel.exe。将下面一行代码添加到 CntrItem.h:LPDISPATCH GetIDispatch()然后将 GetIDispatch 方法添加到 CntrItem.cpp:Sample Code ----------- /******************************************************************* * This method returns the IDispatch* for the application linked to * this container. ********************************************************************/ LPDISPATCH CEmbed_ExcelCntrItem::GetIDispatch() { //The this and m_lpObject pointers must be valid for this function //to work correctly. The m_lpObject is the IUnknown pointer to // this object. ASSERT_VALID(this)ASSERT(m_lpObject != NULL)LPUNKNOWN lpUnk = m_lpObject//The embedded application must be running in order for the rest //of the function to work. Run()//QI for the IOleLink interface of m_lpObject. LPOLELINK lpOleLink = NULLif (m_lpObject->QueryInterface(IID_IOleLink, (LPVOID FAR*)&lpOleLink) == NOERROR) { ASSERT(lpOleLink != NULL)lpUnk = NULL//Retrieve the IUnknown interface to the linked application. if (lpOleLink->GetBoundSource(&lpUnk) != NOERROR) { TRACE0("Warning: Link is not connected!\n")lpOleLink->Release()return NULL} ASSERT(lpUnk != NULL)} //QI for the IDispatch interface of the linked application. LPDISPATCH lpDispatch = NULLif (lpUnk->QueryInterface(IID_IDispatch, (LPVOID FAR*)&lpDispatch) !=NOERROR) { TRACE0("Warning: does not support IDispatch!\n")return NULL} //After assuring ourselves it is valid, return the IDispatch //interface to the caller. ASSERT(lpDispatch != NULL)return lpDispatch} 将下面一行代码添加到 Embed_ExcelView.h:void EmbedAutomateExcel()然后将 EmbedAutomateExcel 方法添加到 Embed_ExcelView.cpp:Sample Code ----------- /******************************************************************** * This method encapsulates the process of embedding an Excel * Worksheet in a View object and automating that worksheet to add * some text to cell A1. ********************************************************************/ void CEmbed_ExcelView::EmbedAutomateExcel() { //Change the cursor so the user knows something exciting is going //on. BeginWaitCursor()CEmbed_ExcelCntrItem* pItem = NULLTRY { //Get the document associated with this view, and be sure it's //valid. CEmbed_ExcelDoc* pDoc = GetDocument()ASSERT_VALID(pDoc)//Create a new item associated with this document, and be sure //it's valid. pItem = new CEmbed_ExcelCntrItem(pDoc)ASSERT_VALID(pItem)// Get Class ID for Excel sheet. // This is used in creation. CLSID clsidif(FAILED(::CLSIDFromProgID(L"Excel.sheet",&clsid))) //Any exception will do. We just need to break out of the //TRY statement. AfxThrowMemoryException()// Create the Excel embedded item. if(!pItem->CreateNewItem(clsid)) //Any exception will do. We just need to break out of the //TRY statement. AfxThrowMemoryException()//Make sure the new CContainerItem is valid. ASSERT_VALID(pItem)// Launch the server to edit the item. pItem->DoVerb(OLEIVERB_SHOW, this)// As an arbitrary user interface design, this sets the // selection to the last item inserted. m_pSelection = pItem// set selection to last inserted item pDoc->UpdateAllViews(NULL)//Query for the dispatch pointer for the embedded object. In //this case, this is the Excel worksheet. LPDISPATCH lpDisplpDisp = pItem->GetIDispatch()//Add text in cell A1 of the embedded Excel sheet _Workbook wbWorksheets wsSet_Worksheet wsRange range_Application app//set _Workbook wb to use lpDisp, the IDispatch* of the //actual workbook. wb.AttachDispatch(lpDisp)//Then get the worksheet's application. app = wb.GetApplication()//Then get the first worksheet in the workbook wsSet = wb.GetWorksheets()ws = wsSet.GetItem(COleVariant((short)1))//From there, get a Range object corresponding to cell A1. range = ws.GetRange(COleVariant("A1"), COleVariant("A1"))//Fill A1 with the string "Hello, World!" range.SetValue(COleVariant("Hello, World!"))//NOTE: If you are automating Excel 2002, the Range.SetValue method has an //additional optional parameter specifying the data type. Because the //parameter is optional, existing code will still work correctly, but new //code should use the new convention. The call for Excel2002 should look //like the following: //range.SetValue( ColeVariant( (long)DISP_E_PARAMNOTFOUND, VT_ERROR ), // COleVariant("Hello, World!"))} //Here, we need to do clean up if something went wrong. CATCH(CException, e) { if (pItem != NULL) { ASSERT_VALID(pItem)pItem->Delete()} AfxMessageBox(IDP_FAILED_TO_CREATE)} END_CATCH //Set the cursor back to normal so the user knows exciting stuff //is no longer happening. EndWaitCursor()} 将下面一行代码添加到 Embed_ExcelView.h:#include "excel8.h" 注意:如果要自动化 Excel 2000,头文件应为“excel9.h”。如果要自动化 Excel 2002,头文件应为“excel.h”。看一下 View 类的 OnInsertObject() 方法。您会注意到一件非常有趣的事:这个方法与我们刚刚编写的方法惊人地相似。事实上,我们编写的代码只是 OnInsertObject() 的一个特例,它允许用户从可用的 OLE 对象列表中选择插入应用程序的对象。由于我们的目的只是自动化 Excel 工作表,因此覆盖了这一行为。在我们的应用程序中,从 InsertObject() 内部删除了所有代码,并代之以对 EmbedAutomateExcel() 的调用。编译并运行应用程序。在编辑菜单上,单击插入新对象。结果:Microsoft Excel 工作表被嵌入到 View 对象中;另外,通过自动化,在单元格 A1 的内容中填入了“Hello, World!”

一定要给分哦。

Microsoft Foundation Classes (MFC), Microsoft Visual C++, 32-bit Editions,

version 6.0 以上

Microsoft OLE 库

本文档适用于:

Microsoft Foundation Classes (MFC), Microsoft Visual C++, 32-bit Editions,

version 6.0 以上

Microsoft OLE 库

第一步:建立一个自动化工程

启动VC 6.0,打开新建对话框,新建一个MFC AppWizard(exe)工程,这里工程明设置为TestExcel。

进入MFC 应用程序向导,选择 基本对话框,直接点击完成,工程文件结构如下图:

打开MFC ClassWizard窗口(查看—>建立类向导),选择Automation,单击Add Class按钮,选择From a type

library...,d出文件选择对话框,之后定位到Microsoft Office的安装目录(通常为C:\Program Files\Microsoft

Office\Office),选择EXCEL9.OLB注意,确定后,d出Confirm Classes窗口,选择列表中的所有类,单击OK按钮。

注意 文件名EXCEL9.OLB,因安装的Office版本不同而有所差异,EXCEL9.OLB对应的是Microsoft Office

2000,微软命名方式为Excel+数字的形式,Office版本越高,数字越大。

返回编辑器,查看工程文件,可发现多了EXCEL9.H及EXCEL9.CPP两个文件。

打开stdafx.h头文件确保包含如下头文件:

#include <afxdisp.h>#include "excel9.h"

打开TestExcel.cpp文件,修改CTestExcelApp::InitInstance(),加入如下代码:

BOOL CTestExcelApp::InitInstance() {

if( !AfxOleInit() ){

AfxMessageBox("初始化Ole出错!")

return FALSE

}

AfxEnableControlContainer()

......

......

return FALSE

}

为保证编译时不产生重复定义错误,打开excel9.h文件,在文件开始位置加入如下代码:

#if !defined _HEAD_FILE_EXCEL9_ #define _HEAD_FILE_EXCEL9_

#pragma once

相应的,在文件末尾加入:

#endif

到此,OLE自动化工程建立完成。按下F7,看是否能通过编译。

第二步: *** 作EXCEL文件

选择ResourceView工作区,打开IDD_TESTEXCEL_DIALOG,在对话框中添加一个按钮控件Button1,双击它,生成一个Button1

Click事件的处理函数:

void CTestExcelDlg::OnButton1()

{

// TODO: Add your control notification handler code here

}

在OnButton1()函数中,添加代码:

void CTestExcelDlg::OnButton1()

{

// TODO: Add your control notification handler code here

_Application app

Workbooks books

_Workbook book

Worksheets sheets

_Worksheet sheet

Range range

Font font

Range cols

COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR)

if( !app.CreateDispatch("Excel.Application") ){

this->MessageBox("无法创建Excel应用!")

return

}

books=app.GetWorkbooks()

book=books.Add(covOptional)

sheets=book.GetSheets()

sheet=sheets.GetItem(COleVariant((short)1))

range=sheet.GetRange(COleVariant("A1"),COleVariant("A1"))

range.SetValue(COleVariant("HELLO EXCEL!"))

font=range.GetFont()

font.SetBold(COleVariant((short)TRUE))

range=sheet.GetRange(COleVariant("A2"),COleVariant("A2"))

range.SetFormula(COleVariant("=RAND()*100000"))

range.SetNumberFormat(COleVariant("$0.00"))

cols=range.GetEntireColumn()

cols.AutoFit()

app.SetVisible(TRUE)

app.SetUserControl(TRUE)

}

按下Ctrl+F5,执行程序。按下Button1,将会d出Excel窗口:

到此,即完成了我们的示例程序,下面将对程序进行详细的说明,如果大家有过使用Visual Basic *** 作Excel程序的经验,则应该能看懂下面程序:

void CTestExcelDlg::OnButton1() {

// TODO: Add your control notification handler code here

/*

先创建一个_Application类,用_Application来创建一个Excel应用程序接口。

Excel接口类中层次如下所示:

-_Application

-Workbooks 工作薄集合

-_Workbook 工作薄

-Worksheets 工作表集合

-_Worksheet 工作表

-Range单元格区域

故要 *** 作表,必须先逐步获取Workbooks—>Workbook —>Worksheets —>Worksheet —>Range

*/

_Application app

Workbooks books

_Workbook book

Worksheets sheets

_Worksheet sheet

Range range//Excel中针对单元格的 *** 作都应先获取其对应的Range对象

Font font

Range cols

/*

COleVariant类为VARIANT数据类型的包装,在自动化程序中,通常都使用VARIANT数据类型进行参数传递。故下列程序中,函数参数都是通过COleVariant类来转换了的。

*/

//covOptional 可选参数的VARIANT类型

COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR)

if( !app.CreateDispatch("Excel.Application") ){

this->MessageBox("无法创建Excel应用!")

return

}

//获取工作薄集合

books=app.GetWorkbooks()

//添加一个工作薄

book=books.Add(covOptional)

//获取工作表集合

sheets=book.GetSheets()

//获取第一个工作表

sheet=sheets.GetItem(COleVariant((short)1))

//选择工作表中A1:A1单元格区域

range=sheet.GetRange(COleVariant("A1"),COleVariant("A1"))

//设置A1=HELLO EXCEL!"

range.SetValue(COleVariant("HELLO EXCEL!"))

//调整格式,设置粗体

font=range.GetFont()

font.SetBold(COleVariant((short)TRUE))

//选择A2单元格,插入一个公式"=RAND()*100000",并设置A2数字格式为货币形式

range=sheet.GetRange(COleVariant("A2"),COleVariant("A2"))

range.SetFormula(COleVariant("=RAND()*100000"))

range.SetNumberFormat(COleVariant("$0.00"))

//选择A:A列,设置宽度为自动适应

cols=range.GetEntireColumn()

cols.AutoFit()

//显示Excel表格,并设置状态为用户可控制

app.SetVisible(TRUE)

app.SetUserControl(TRUE)

如何自动保存我们生成的Excel文件?通过Workbook对象的SaveAs方法即可实现:

book.SaveAs(COleVariant("C:\\a.xls"),covOptional,

covOptional,covOptional,

covOptional,covOptional,(long)0,covOptional,covOptional,covOptional,

covOptional)

上面的程序只能实现新建Excel文件的功能,但大多数情况我们需要的是导入excel中的数据,即要打开现存的xls文件,那么可用如下代码:

_Application appWorkbooks books

_Workbook book

Worksheets sheets

_Worksheet sheet

LPDISPATCH lpDisp//接口指针

COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR)

if( !app.CreateDispatch("Excel.Application") ){

this->MessageBox("无法创建Excel应用!")

return

}

books=app.GetWorkbooks()

lpDisp = books.Open("C:\\a.xls",covOptional,covOptional,

covOptional, covOptional, covOptional, covOptional,

covOptional, covOptional, covOptional, covOptional,

covOptional, covOptional, covOptional, covOptional

)

book.AttachDispatch( lpDisp )

sheets=book.GetSheets()

sheet=sheets.GetItem(COleVariant((short)1))

range=sheet.GetRange(COleVariant("A1"),COleVariant("A1"))

COleVariant rValue

rValue=COleVariant(range.GetValue())

rValue.ChangeType(VT_BSTR)

this->MessageBox(CString(rValue.bstrVal))

book.SetSaved(TRUE)

app.Quit()

运行上面程序,单击Button1按钮,会显示出C:\a.xls文件中A1单元格中的内容。注意看以下代码,实现了Variant数据类型转换为CString类,这个只是一个示例,转换较为简单。

COleVariant rValue

rValue=COleVariant(range.GetValue())

rValue.ChangeType(VT_BSTR)

this->MessageBox(CString(rValue.bstrVal))


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11902870.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-19
下一篇2023-05-19

发表评论

登录后才能评论

评论列表(0条)

    保存