VC编程操作Word2010生成表格

VC编程操作Word2010生成表格

1. 右键单击工程节点,然后选择添加类,如下图:

https://img-blog.csdn.net/20160811174745436?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center

2. 添加TypeLib中的MFC类,如下图:

https://img-blog.csdn.net/20160811174843795?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center

3. 选择文件,打开MSWORD.OLB word2010的在Office14的文件夹下,如下图:

https://img-blog.csdn.net/20160811175031670?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center

4. 生成所有的接口类代码,如下图:

https://img-blog.csdn.net/20160811175129828?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center

5. 生成表格的代码如下:

void CMFCDlgDlg::OnBnClickedButton1()

{

       CApplication     oWordApp;

       CDocuments        oDocs;

       CDocument0       oDoc;

       CRange            range;

       CParagraph        paragraph;

       CParagraphs       paragraphs;

       CCell             cell;

       CCells            cells;

       CTable0            table;

       CTables0           tables;

       CBorder           border;

       CBorders          borders;

       CFont0           font;

       CPageSetup        pagesetup;

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

       LPDISPATCH   pDisp;

       LPUNKNOWN    pUnk;

       CLSID        clsid;

       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表格

       oWordApp.m_bAutoRelease=true;

       BeginWaitCursor();

       oDocs=oWordApp.get_Documents();

       COleVariant vOpt(DISP_E_PARAMNOTFOUND, VT_ERROR);

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

       pagesetup=oDoc.get_PageSetup();

       pagesetup.put_Orientation(1);  //横向

       paragraphs=oDoc.get_Paragraphs();

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

       range=paragraph.get_Range();

       //添加制作新表格

       tables=oDoc.get_Tables();

       int nBhCount=3;

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

       borders=table.get_Borders();

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

       range=table.get_Range();

       cells=range.get_Cells();

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

#define DOC_TABLE1_WIDTH 60

 

       cells.SetWidth(DOC_TABLE1_WIDTH,1);

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

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

       range=cell.get_Range();

       range.put_Bold(1);

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

       cell=table.Cell(2,1);

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

       range=cell.get_Range();

       range.put_Bold(1);

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

       cell=table.Cell(2,2);

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

       range=cell.get_Range();

       range.put_Bold(1);

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

       cell=table.Cell(2,3);

       range=cell.get_Range();

       range.put_Bold(1);

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

       for(int iBh=0;iBh

       {

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

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

              range=cell.get_Range();

              range.put_Bold(0);

              range.put_Text("hello1");

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

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

              range=cell.get_Range();

              range.put_Bold(0);

              range.put_Text("hello2");

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

              range=cell.get_Range();

              range.put_Bold(0);

              range.put_Text("hello3");   

       }

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

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

       range=cell.get_Range();

       range.put_Bold(1);

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

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

       {

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

              range=cell.get_Range();

              range.put_Bold(1);

              range.put_Text("hello4");

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

              range=cell.get_Range();

              range.put_Bold(0);

              range.put_Text("hello5");

       }

       //保存,清理

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

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

       CDocument0 oActiveDoc;

       oActiveDoc = oWordApp.get_ActiveDocument();

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

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

              vFalse, vFalse, vFalse, vFalse,

              vOpt,vOpt,vOpt,vOpt,vOpt);

       oWordApp.put_Visible(true);

       oWordApp.ReleaseDispatch(); //退出WORD

       EndWaitCursor();

}

效果图如下:

https://img-blog.csdn.net/20160811175334361?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center

你可能感兴趣的:(Visual,C++,软件开发)