VC操作Word系列(四)

本文介绍使用VC进行Word文档操作的方法,包括添加文本、表格及单元格填充等,并演示如何实现合并单元格及内容居中显示等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

随笔之:VC操作Word系列()

上一回讲了如何合并单元格,这次来说说如何填充单元格以及如何对合并后的单元格的内容让他居中显示这样能够看得舒服一点:)。代码如下所示:

void CWordTestDlg::TestWord4()

{

_Application app;

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

app.CreateDispatch(_T("Word.Application"));

app.SetVisible(FALSE);

//Create New Doc

Documents docs=app.GetDocuments();

CComVariant tpl(_T("")),Visble,DocType(0),NewTemplate(false);

docs.Add(&tpl,&NewTemplate,&DocType,&Visble);

//Add Content:Text

Selection sel=app.GetSelection();

sel.TypeText(_T("/t/t/t/t/t/t/t情况汇总/r/n"));

sel.TypeText(_T("/t/t/t/t/t/t/t/t----------*******跨国公司/r/n"));

COleDateTime dt=COleDateTime::GetCurrentTime();

CString strDT=dt.Format("%Y-%m-%d");

COleDateTimeSpan span(1,0,0,0);

CString strDT1=(dt-span).Format("%Y-%m-%d");

CString strDate[]={strDT1,strDT};

CString str("/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t");

str+=strDT;

str+="/r/n";

sel.TypeText(str);

//Add Table

_Document saveDoc=app.GetActiveDocument();

Tables tables=saveDoc.GetTables();

CComVariant defaultBehavior(1),AutoFitBehavior(1);

tables.Add(sel.GetRange(),7,11,&defaultBehavior,&AutoFitBehavior);

Table table=tables.Item(1);

sel.TypeText(_T("Test1"));

sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));

sel.TypeText(_T("Test2"));

sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));

sel.TypeText(_T("Test3"));

sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));

sel.TypeText(_T("Test4"));

sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));

sel.TypeText(_T("Test5"));

sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));

sel.TypeText(_T("Test6"));

sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));

sel.TypeText(_T("Test7"));

sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));

sel.TypeText(_T("Test8"));

sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));

sel.TypeText(_T("Test9"));

sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));

sel.TypeText(_T("Test10"));

sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));

sel.TypeText(_T("Test11")); 

int i=0,j=0;

for(i=2;i<7;i+=2)

{

        Cell c1=table.Cell(i,1);

        Cell c2=table.Cell(i+1,1);

        c1.Merge(c2);

        c1.ReleaseDispatch();

        c2.ReleaseDispatch();

}

Cell c;

CString strName[]={"yingkou","zyq654321","iwaswzq"};

for(j=0,i=0;i<3;++i,j+=2)

{

        c=table.Cell(j+2,1);

        c.Select();

        Cells cs=sel.GetCells();

        cs.SetVerticalAlignment(1);

        sel.TypeText(strName[i]); 

        sel.MoveDown(COleVariant((short)5),COleVariant(short(1)),COleVariant(short(0)));

        cs.ReleaseDispatch();

        c.ReleaseDispatch();

}

c=table.Cell(7,1);

c.Select();

for(i=0,j=0;i<6;++i,++j)

{

        if(i%2==0)

               j=0;

        sel.TypeText(strDate[j]); 

        sel.MoveUp(COleVariant((short)5),COleVariant(short(1)),COleVariant(short(0)));

}

c.ReleaseDispatch();

app.SetVisible(TRUE);

table.ReleaseDispatch();

tables.ReleaseDispatch();

sel.ReleaseDispatch();

docs.ReleaseDispatch();

saveDoc.ReleaseDispatch();

app.SetVisible(TRUE);

app.ReleaseDispatch();

}

运行一下看看版面比以前好多了吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值