摘要:使用msword.olb实现在word文件中插入,生成表格
例子部分代码:
_Application oWordApp;
Documents oWordDocs;
_Document oWordDoc;
Selection oWordSel;
Tables oWordTables;
Table oWordTable;
Range oWordRange;
InlineShapes oWordInlineShap;
COleVariant vTrue((short)TRUE),vFalse((short)FALSE),vOpt((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
COleVariant vUnit((short)12);
if (!oWordApp.CreateDispatch(_T("Word.Application")))
{
AfxMessageBox(_T("Word CreateDispatch Failed!"));
return ;
}
oWordApp.SetVisible(TRUE);
oWordApp.Activate();
oWordDocs = oWordApp.GetDocuments();
oWordDoc = oWordDocs.Add(vOpt,vOpt,vFalse,vOpt);
oWordDoc = oWordApp.GetActiveDocument();
oWordSel = oWordApp.GetSelection();
oWordRange= oWordSel.GetRange();
oWordTables = oWordDoc.GetTables();
oWordTables.Add(oWordRange,10,5,vTrue,vFalse);
oWordSel.SetText("序号");
oWordSel.MoveRight(vUnit,vOpt,vOpt);
oWordSel.SetText("姓名");
oWordSel.MoveRight(vUnit,vOpt,vOpt);
oWordSel.SetText("年龄");
oWordSel.MoveRight(vUnit,vOpt,vOpt);
oWordSel.SetText("住址");
oWordSel.MoveRight(vUnit,vOpt,vOpt);
oWordSel.SetText("备注");
oWordSel.MoveRight(vUnit,vOpt,vOpt);