MFC制作word文档

MFC制作word文档

2023年7月17日发(作者:)

MFC/VC++调用word进行报表制作

使用word进行数据报告的制作可谓非常方便,word具有非常强大的编辑、排版功能。使用word能够制作出内容丰富、样式精美的报告。我们在工作中当然会有报表报告的需求,如果能够在MFC/VC++里面能够调用word进行word格式报告的自动生成,岂不是一件非常惬意的事情。

我在工作当中需要对大量的数据或者各种测试数据进行统计分析,最终的统计分析结果总归要汇总为一份报告,不管是内部使用也好还是外部提供给客户也好,一份内容翔实、格式精美的报告自然必不可少。我对MFC/VC++也不是高手,只是业余爱好自己动动手做些东西。自然,低于VC操作word可谓完全没有经验,在网络上面也查找了很多资料,都是些只言片语,不能真正的领略通过VC使用word的方法。于是自己摸索了几个礼拜,可谓耗时甚长,劳心劳力啊。MS的东西,封装的也太严实了,对于函数部分只有些许的简单介绍,往往看的云里雾里。没有实践还是不行啊!体会自己的经历,虽然辛苦但也有收获。不想其他朋友再继续走我走过的路,浪费时间、精力,故成文以共享。

废话少说,进入正题吧。

第一步,当然我们需要首先导入word需要的库。通过ClassWizard->Add

Class选择From a Type Library…定位需要的库文件导入。本例应该使用C:/Program Files/Microsoft Office/OFFICE11/,在这里尽可选择所有的类导入,反正用的时候都有了,呵呵。完成这一步,我们得到两个文件msword.h和,那就加入你的工程吧。

说明:如果需要在VC++/MFC开发程序操作word/excel等office元素,那么需要对必要的类型库进行导入.下面是office系列的类型库参考,导入类型库时候请选择正确的类型库进行导入.

应用程序

Microsoft Access 97

Microsoft Jet Database 3.5

Microsoft Binder 97

Microsoft Excel 97

类型库

Microsoft Graph 97

Microsoft Office 97

Microsoft Outlook 97

Microsoft PowerPoint 97

Microsoft Word 97

Microsoft Access 2000

Microsoft Jet Database 3.51

Microsoft Binder 2000

Microsoft Excel 2000

Microsoft Graph 2000

Microsoft Office 2000

Microsoft Outlook 2000

Microsoft PowerPoint 2000

Microsoft Word 2000

Microsoft Access 2002

Microsoft Excel 2002

Microsoft Graph 2002

Microsoft Office 2002

Microsoft Outlook 2002

Microsoft PowerPoint 2002

Microsoft Word 2002

Microsoft Office Access 2003

Microsoft Office Excel 2003

Microsoft Office Graph 2003

Microsoft Office 2003

Microsoft Office Outlook 2003

Microsoft Office PowerPoint 2003

Microsoft Office Word 2003

注意:这些类型库的默认位置是:

Office 版本

Office 97

C:/Program Files/Microsoft Office/Office

Office

2000

Office XP

C:/Program Files/Microsoft Office/Office10

Office

2003

C:/Program Files/Microsoft Office/Office11

C:/Program Files/Microsoft Office/Office

路径

和 的默认位置是C:/Program Files/Common Files/Microsoft Shared/Dao。

第二步,进入具体使用实践阶段。激动人心的时刻到来了,呵呵。不过别忘记把你的H文件#Include进来。下面是我的一个例子,从我具体的工程当中抽取出来供各位参考。有些地方我会简要解释。还等什么,赶紧试验一下代码吧。修改一下就是你的报告了,开心吧。

BOOLCMDIDemoApp::OnMakeReport()

{

_ApplicationwordApp;

DocumentswordDocs; _DocumentwordDoc;

SelectionwordSelection;

RangewordRange;

TableswordTables;

TablewordTable;

CellwordCell;

CellswordCells;

//Paragraphs wordParagraphs;

//Paragraph wordParagraph;

_FontwordFont;

ShadingwordShading;

//

intnRow=0;

//

COleVariantvTrue((short)TRUE),

vFalse((short)FALSE),

vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

CComVariantdefaultBehavior(1),AutoFitBehavior(0);

//创建word application实例

if(!Dispatch(_T("ation")))

{

AfxMessageBox("Word CreateDispatch Failed!");

returnFALSE;

}

ible(TRUE);//make visible

te();//激活

wordDocs=uments();//get documents object

//

CStringstrDocTemplate;

CStringstrPath;

charszFullPath[_MAX_PATH];

::GetModuleFileName(NULL,szFullPath,_MAX_PATH);

("%s",szFullPath);

intnPos=eFind('//');

strDocTemplate=(nPos);

strPath=strDocTemplate;

TRACE1("%s/n",strDocTemplate);

// strDocTemplate+="//Template//";

//

wordDoc=(COleVariant(strDocTemplate),&vOpt,&vOpt,&vOpt,&vOpt,

//

&vOpt,&vOpt,&vOpt,&vOpt,&vOpt,&vOpt,&vTrue,&vOpt,&vOpt,&vOpt,&vOpt);

CComVarianttpl(_T("")),Visble,DocType(0),NewTemplate(false); wordDoc=(&tpl,&NewTemplate,&DocType,&Visble);

wordSelection=ection();

//Dispatch(les());

wordTables=les();

//wordParagraphs = agraphs();

//wordParagraph=t();

//wordRange=ge();

//(COleVariant((short)6),COleVariant((short)0));

//move insertion point to the end of the story

/*

wdUnits:

wdCharacter=1

wdWord=2

wdSentence=3

wdParagraph=4

wdSection=8

wdStory=6

wdCell=12

wdColumn=9

wdRow=10

wdTable=15

wdMovementType:

wdMove=0

wdExtend=1

*/

//move to end of story

(COleVariant((short)6),COleVariant((short)0));

//1.1 RxLev Full

xt("1. 统计报告");

y(&CComVariant(5),&CComVariant(1));

//Format the line with selection

wordFont= t();

d(9999998);//wdToggle

(&CComVariant(5),&CComVariant(0));

ragraph();

xt("(1.1). 分段统计");

ragraph();

d(9999998);//wdToggle

wordRange=ge();

//add table

//nRow=m_nRange1+1;

wordTable=(wordRange,5/*row*/,4/*column*/,&defaultBehavior,&AutoFitBehavior);

wordRange=ge();

//d(COleVariant((short)15),COleVariant((short)1));

//();

//move end of table

//(COleVariant((short)15),COleVariant((short)0));

//insert rows

//RowsBelow(COleVariant((short)5));

//选择第一个单元,进而选择第一行进行格式化

wordCell=(1,1);

();

//select the row with current selection

(&CComVariant(10),&CComVariant(1));

//Format the row with selection

//wordFont = t();

d(9999998);//wdToggle

wordCells=ls();

wordShading= ding();

ture(0);

kgroundPatternColor(14737632);//15987699 14737632

adColorBlue

egroundPatternColor(-16777216);//-16777216

wdColorAutomatic

//move to end of table

//(COleVariant((short)15),COleVariant((short)0));

//wordParagraph=t();

//wordRange=ge();

//d(COleVariant((short)4),COleVariant((short)1));

//t("Test");

//wordSelection=ection();

//d(COleVariant((short)6),COleVariant((short)1));

wordCell=(1,1);

();

xt("统计项目");

ght(&CComVariant(12),&CComVariant(1),&CComVariant(0));

xt("采样");

ght(&CComVariant(12),&CComVariant(1),&CComVariant(0));

xt("百分比");

ght(&CComVariant(12),&CComVariant(1),&CComVariant(0));

xt("累计百分比");

//

//

(COleVariant((short)6),COleVariant((short)0));

ragraph();

xt("(1.2). 分段统计");

y(&CComVariant(5),&CComVariant(1));

//Format the line with selection

wordFont= t();

d(9999998);//wdToggle

(&CComVariant(5),&CComVariant(0));

ragraph();

d(9999998);//wdToggle

wordRange=ge();

//add table

//nRow=m_nRange1+1;

wordTable=(wordRange,5/*row*/,4/*column*/,&defaultBehavior,&AutoFitBehavior);

wordRange=ge();

//选择第一个单元,进而选择第一行进行格式化

wordCell=(1,1);

();

//select the row with current selection

(&CComVariant(10),&CComVariant(1));

//Format the row with selection

wordFont= t();

d(9999998);

wordCells=ls();

wordShading= ding();

ture(0);

kgroundPatternColor(14737632);//15987699

egroundPatternColor(-16777216);

wordCell=(1,1);

();

xt("Range");

ght(&CComVariant(12),&CComVariant(1),&CComVariant(0));

xt("Samples");

ght(&CComVariant(12),&CComVariant(1),&CComVariant(0)); xt("Percentage");

ght(&CComVariant(12),&CComVariant(1),&CComVariant(0));

xt("Calculation");

//

//Save document as report

SYSTEMTIMEsysTime;

GetLocalTime(&sysTime);

CStringstrReport;

("%d年-%d月-%d日 %d-%d-%d 报告",,,

,,e,d);

strReport=strPath+"//"+strReport;

(COleVariant(strReport),COleVariant((short)0),vOpt,vOpt,vOpt,vOpt,vOpt,vOpt

,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt);

//Release com

eDispatch();

eDispatch();

eDispatch();

eDispatch();

eDispatch();

eDispatch();

eDispatch();

kSmartTags();

eDispatch();

eDispatch();

returnTRUE;

}

注释如下:

1.

使用之前比忘记先进行COM环境的初始化,最好放在BOOLCMDIDemoApp::InitInstance()里吧。

//Initialize COM context

if(FAILED(::CoInitialize(NULL)))

{

AfxMessageBox("Initialize COM context failed!");

returnFALSE;

} 2.

注意几个函数的用法:

a)

(COleVariant((short)6),COleVariant((short)0));

b)

(&CComVariant(10),&CComVariant(1));

c)

y(&CComVariant(5),&CComVariant(1));

d)

wordTable=(wordRange,5/*row*/,4/*column*/,&defaultBehavior,&AutoFitBehavior);

这几个函数可以说是我收获最大的地方了,EndOf/EndKey/HomeKey实现了当前焦点的任意移动,比如到一个文档的末尾或者行尾、表格的操作位置等。举例来说:EndOf (VARIANT* Unit, VARIANT* Extend) 其第二个参数Extend决定了你是单纯的移动光标还是扩展移动选中内容。通过VBA参考,你可以知道wdMove=0 wdExtend=1,这样就可以决定是移动还是扩展选择内容了。

当然还有其他很多操作可以摸索,大可以先用word的宏录制,然后编写自己的VC代码,不过确实是个繁琐的过程,很多VBA常量需要查找具体的value替换。

通过预先的模版,你可以实现打开一个word模版,然后添加你自己的内容,灵活应用。然后保存为自己的报告。请参阅我的例子自己实现吧。Enjoy

it 

由于时间关系,我还没有摸索通如何在word报告中插入图表(chart/bar/pie)以及如何插入图片jpg等,这样就可以做出图文并茂的报告了。如果你有经验,欢迎交流共享。

2007-12-6

samuelzhao@

分享到:

查看评论

8楼 wj6910502 2012-03-11 12:07发表 [回复] [引用][举报]

上一篇:MFC LoadFrame()创建视图、框架参考

下一篇:使用ATL的W2A和A2W宏

很好~~~~感谢楼主

7楼 xtzhx2008 2008-12-20 21:28发表 [回复] [引用][举报]

不错!

6楼 过客 2008-03-07 11:01发表 [回复] [引用][举报]

挺好!尤其有我需要的变量值。

5楼 teleinfor 2007-12-09 17:12发表 [回复] [引用][举报]

其实这里用到了CComVariant/COleVariant/VARIANT/_variant_t,具体使用哪种,可以根据自己的喜好选择.COM接口传递的参数对于这几个数据类型非常常用.您可以自己摸索学习它们.总的来说CComVariant比较好用

4楼 teleinfor 2007-12-09 17:04发表 [回复] [引用][举报]

回楼上的tx,可能是因为你的相关的CComVariant头文件没有包含.

#include <atlcomcli.h>

Re: teleinfor 2007-12-10 09:52发表 [回复] [引用][举报]

或者你直接使用这个头文件包含也行<br />#include &lt;atlbase.h&gt;<br

/>

3楼 swpusmq2005 2007-12-08 16:32发表 [回复] [引用][举报]

按照上面的方法,但是最后CComVariant 这个类好象没有!

能说说是什么原因吗??

2楼 teleinfor 2007-12-07 13:48发表 [回复] [引用][举报]

谢谢!

不知道你说的“列”,指的是什么?对于表格的列如果想进行操作,也是容易的,只要我们懂得了EndOf()/EndKey()/HomeKey()几个函数的用法,具体移动wdMove或者wdExtend你的选择区域都可以灵活实现,变化不同的wdUnit就行了。

wdColumn=9

wdRow=10

wdTable=15

这几个都是根表格相关的单位,列只要指定Extend参数为wdColumn就ok了,当然需要转换为value=9

时间仓促,我没有详细整理优化代码以及排版。以后会慢慢的总结VC/MFC对office Excel/Word的操作步骤。

不知道哪位大侠对操作word图表Char Object或者通过excel、word结合实现图表控制有经验?请指教。

1楼 dengxidengxi 2007-12-07 10:02发表 [回复] [引用][举报]

伟大的工作!

希望能够有一些列的探索~

这些咚咚非常实用的。

发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1689608888a270296.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信