VC 6.0动态生成Word表格实例

 (1)首先,向你的项目中添加word 2003类型库:方法是在MFC ClassWizard中点击按钮Add Class,选择From a type library,在弹出的浏览对话框中,浏览找到office 2003安装路径下的msword.olb文件,例如:"C:\Program Files\Microsoft Office\OFFICE11\MSWORD.OLB",然后选择以下类,这些类都是后面实现生成word表格要用到的:

_Application,Documents,_Document,Range, Paragraph,Paragraphs,Cell,Cells,Table,Tables,Border,Borders ,_Font ,PageSetup

(2)在使用到这些类型的类的头文件或者cpp文件的开头,包含以下头文件:#include "msword.h"

(3) 创建如下代码

//1.定义对象

_Application     oWordApp; 

Documents        oDocs;

_Document        oDoc;

Range            range;

Paragraph        paragraph;

Paragraphs       paragraphs;

Cell             cell;

Cells            cells;

Table            table;

Tables           tables;

Border           border;

Borders          borders;

_Font            font;

PageSetup        pagesetup;

 

//2.创建和连接word对象,初始化连接,判断当前系统是否有word进程,然后然后创建或连接对象

LPDISPATCH   pDisp; 

LPUNKNOWN    pUnk; 

CLSID        clsid; 

CoInitialize(NULL); 

CLSIDFromProgID(L"Word.Application",&clsid); 

if(GetActiveObject(clsid,NULL,&pUnk)==S_OK) 

pUnk-> QueryInterface(IID_IDispatch,(void **)&pDisp); 

oWordApp.AttachDispatch(pDisp); 

}  

else 

if(!oWordApp.CreateDispatch("Word.Application"))

{

AfxMessageBox("创建Word服务失败!"); 

}

}

 

 

//初始化文档,生成WORD表格

oDocs.ReleaseDispatch();

oWordApp.m_bAutoRelease=true;

BeginWaitCursor();

oDocs=oWordApp.GetDocuments();

COleVariant vOpt(DISP_E_PARAMNOTFOUND, VT_ERROR);

oDoc=oDocs.Add(vOpt,vOpt,vOpt,vOpt);//加新文档

pagesetup=oDoc.GetPageSetup();

pagesetup.SetOrientation(1);//横向

paragraphs=oDoc.GetParagraphs(); //生成第一个表

paragraph=paragraphs.GetLast();//以当前段落最后一行为表起始位置

range=paragraph.GetRange();

//添加制作新表格

tables=oDoc.GetTables();

 

int nBhCount=3;

table=tables.Add(range,5+nBhCount,6,vOpt,vOpt);

borders=table.GetBorders();

borders.SetEnable(1);//显示表框

range=table.GetRange();

cells=range.GetCells();

cells.SetVerticalAlignment(1);//文字居中

 

#define DOC_TABLE1_WIDTH 60

 

cells.SetWidth(DOC_TABLE1_WIDTH); 

cell=table.Cell(1,1);//合并第一行的三列

cell.Merge(table.Cell(1,6));

range=cell.GetRange();

range.SetBold(1);

range.SetText("                                         病害统计信息");//居中

cell=table.Cell(2,1);

cell.Merge(table.Cell(2,2));

range=cell.GetRange();

range.SetBold(1);

range.SetText("组成材料");

cell=table.Cell(2,2);

cell.Merge(table.Cell(2,4));

range=cell.GetRange();

range.SetBold(1);

range.SetText("病害种类");

cell=table.Cell(2,3);

range=cell.GetRange();

range.SetBold(1);

range.SetText("病害总数");

for(int iBh=0;iBh<nBhCount;iBh++)

{

cell=table.Cell(2+iBh+1,1);

cell.Merge(table.Cell(2+iBh+1,2));

range=cell.GetRange();

range.SetBold(0);

range.SetText("hello1");

cell=table.Cell(2+iBh+1,2);

cell.Merge(table.Cell(2+iBh+1,4));

range=cell.GetRange();

range.SetBold(0);

range.SetText("hello2");

cell=table.Cell(2+iBh+1,3);

range=cell.GetRange();

range.SetBold(0);

range.SetText("hello3");    

}

cell=table.Cell(3+nBhCount,1);//合并7列

cell.Merge(table.Cell(3+nBhCount,6));

range=cell.GetRange();

range.SetBold(1);

range.SetText("                                       等级统计信息");

for(int iCol=1;iCol<=6;iCol++)

{

cell=table.Cell(3+nBhCount+1,iCol);

range=cell.GetRange();

range.SetBold(1);

range.SetText("hello4");

cell=table.Cell(3+nBhCount+2,iCol);

range=cell.GetRange();

range.SetBold(0);

range.SetText("hello5"); 

}

 

//保存,清理

CString   strSave="D:\\test.doc";

    COleVariant vTrue((short)TRUE), vFalse((short)FALSE);

_Document oActiveDoc; 

oActiveDoc = oWordApp.GetActiveDocument(); 

oActiveDoc.SaveAs(COleVariant(strSave),COleVariant((short)0), vFalse,

COleVariant(""), vTrue,COleVariant(""),vFalse, 

vFalse, vFalse, vFalse, vFalse,

vOpt,vOpt,vOpt,vOpt,vOpt); 

oWordApp.SetVisible(true);

oDocs.ReleaseDispatch();    //断开关联;

oWordApp.ReleaseDispatch(); //退出WORD 

EndWaitCursor();

(5)生成表格如下:

                                         病害统计信息

组成材料

病害种类

病害总数

hello1

hello2

hello3

hello1

hello2

hello3

hello1

hello2

hello3

                                       等级统计信息

hello4

hello4

hello4

hello4

hello4

hello4

hello5

hello5

hello5

hello5

hello5

hello5

posted @ 2013-01-12 23:12  Roarsun  阅读(2144)  评论(0编辑  收藏  举报