VS2010写WORD(二)表格操作

只讲操作,原理未知。。。。。。

主要记录表格以的一下简单操作 

1.添加表格

2.设置列宽

3.设置边框属性

4.单元格的合并拆分

5.单元格的填充

6.动态添加一行

7.结束当前单元格的标记,调到文档末尾


进入表格

//选择从光标至文档结尾的内容 先跳出之前的内容

selection.EndKey(COleVariant((short)wdStory),COleVariant((short)wdMove));

CRange rng = selection.get_Range();
CTables0 tables = doc.get_Tables();
CTable0   tb = tables.Add(rng,2,2,vOpt,vOpt);
CColumns0 cols = tb.get_Columns();

//设置每列宽度
float wid = cols.get_Width();
CColumn col;
col.AttachDispatch(cols.Item(1),TRUE);

col.put_Width(50);//第一列设置为50

col.AttachDispatch(cols.Item(2),TRUE);
col.put_Width(wid + (wid - 50));//此处应注意 wid应该是获得的每列的宽度,表格总宽度是固定,为了保证表格总宽度,减掉的要加上

//设置表格外边框和内框属性

CBorders bds = tb.get_Borders();

bds.put_OutsideLineStyle(wdLineStyleSingle);
bds.put_OutsideColorIndex(wdGray50);
bds.put_InsideLineStyle(wdLineStyleSingle);
bds.put_InsideColorIndex(wdGray50);

//合并单元格

selection.MoveRight(COleVariant((short)1),COleVariant((short)2),COleVariant((short)wdExtend));
CCells cells = selection.get_Cells();
cells.put_VerticalAlignment(1);//文字居中
cells.Merge();

//插入图片 title
CString strAppPath = GetAppPath(); 
CString strPicture = strAppPath + _T("\\appico\\sms.png"); 
CnlineShapes nLineShapes = selection.get_InlineShapes();
CnlineShape nLineshape = nLineShapes.AddPicture(strPicture, vFalse, vTrue, vOpt);// 插入图片API
selection.TypeText(strFormat);
selection.put_Style(COleVariant((short)wdStyleHeading1));
CParagraph lastPara = selection.get_Paragraphs();
lastPara.put_Alignment(wdAlignParagraphCenter); 
selection.EndOf(COleVariant((short)wdParagraph), COleVariant((short)wdMove));  

//填充单元格  方法一

selection.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)wdMove));
selection.TypeText(_T("序号"));
selection.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)wdMove));
selection.TypeText(_T("短信信息"));

//动态添加一行

CRows rs;
COleVariant covOp((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
rs = tb.get_Rows();
rs.Add(covOp);
CRows rows=tb.get_Rows();
long nRows=rows.get_Count();

//拆分单元格
CCell cell = tb.Cell(nRows,2);
COleVariant lastrow((long)2);
COleVariant lastcol((long)1);
cell.Split(lastrow,lastcol);

//设置单元格背景色 和 填充单元格

cell = tb.Cell(nRows,1);
CShading s = cell.get_Shading();
s.put_BackgroundPatternColor(wdColorGray25);
CRange rng = cell.get_Range();
rng.put_Text(strFormat);

//结束当前表 EndKey调到文档末尾 结束当前table表的编辑
selection.EndKey(COleVariant((short)wdStory),COleVariant((short)wdMove));

//在添加第二个table表格前需要加一个段落 ,否则会报错( 原因未知  )
selection.TypeParagraph();
selection.EndOf(COleVariant((short)wdParagraph), COleVariant((short)wdMove));


//添加第二个表格 EndKey调到文档末尾 开始第二个table表的编辑

selection.EndKey(COleVariant((short)wdStory),COleVariant((short)wdMove));

。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值