• MFC vc++ 写 word 文件,使用MSWORD.OLB


    写一个doc文件

    先写一句话

    画一个2列5行的表格,第一列数据加粗

    再写一句话

    再画一个5列4行的表格,第一行数据加粗,第5列数据有颜色

    参考

    VC程序嵌入Word或者WPS

    MFC下使用C++操作Word文档

    1.从MSWORD.OLB导入要使用的类

    项目->类向导->添加类(向下的小三角)类型库中的MFC类->文件->选择MSWORD.OLB

    MSWORD.OLB的位置与安装office的版本和安装位置有关

    可以从这里参考查查自己电脑MSWORD.OLB文件的位置 https://www.mypcrun.com/file-info-page/msword-olb/

    我电脑是office2016 位置是 C:Program Files (x86)Microsoft Office ootOffice16MSWORD.OLB

    选择自己需要的类,导入到工程中

    2.导入的所有的.h文件开头第一句都有一行代码,把这个注释掉,就能编译了

    #import "C:\Program Files (x86)\Microsoft Office\root\Office16\MSWORD.OLB" no_namespace

    3.include 这些.h文件,我这里用到这些

    #include "CApplication.h"
    #include "CDocument0.h"
    #include "CDocuments.h"
    #include "CBorders.h"
    #include "CFont0.h"
    #include "CParagraphs.h"
    #include "CSelection.h"
    #include "CTable0.h"
    #include "CTables0.h"
    #include "CCell.h"

    写一个例子

      1 void CMFCApplication1Dlg::OnBnClickedOk()
      2 {
      3     // TODO: 在此添加控件通知处理程序代码
      4     //1.开启word
      5     CApplication word_app;
      6     if (!word_app.CreateDispatch(_T("Word.Application"), NULL))
      7     //if (!word_app.CreateDispatch(_T("KWPS.Application"), NULL))
      8     {
      9         AfxMessageBox(_T("本机没有安装word产品!"));
     10         return;
     11     }
     12     word_app.put_Visible(FALSE);//设置word可见
     13 
     14                                 //2.新建文档
     15     COleVariant vTrue((short)TRUE), vFalse((short)FALSE), VOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
     16     CDocuments docs;
     17     CDocument0 doc;
     18     docs = word_app.get_Documents();
     19     doc = docs.Add(new CComVariant(_T("")), new CComVariant(false), new CComVariant(0), new CComVariant());
     20 
     21     //3.开始向word写入数据
     22     CSelection sel = word_app.get_Selection();
     23 
     24     CParagraphs wordParagraphs = sel.get_ParagraphFormat();
     25     wordParagraphs.put_Alignment(0);//0 左 1剧中 2右
     26 
     27     CFont0 font = sel.get_Font();
     28     font.put_Bold(1);
     29     font.put_Size(22);
     30     font.put_Name(_T("宋体"));//设置字体属性
     31     sel.TypeText(TEXT("hello MSWORD.OLB
    
    "));
     32 
     33     CTables0 tables = doc.get_Tables();
     34     CTable0 table1 = tables.Add(sel.get_Range(), 5, 2, new CComVariant(), new CComVariant());//创建5行2列的表格
     35     CBorders borders1 = table1.get_Borders();
     36     borders1.put_InsideLineStyle(1);
     37     borders1.put_OutsideLineStyle(1);//设置表格边框,默认没有边框
     38 
     39     //设置单元格宽度
     40     CCell CellTemp;
     41     for (int i = 0; i < 5; i++)
     42     {
     43         CellTemp = table1.Cell(i + 1, 1);
     44         CellTemp.put_Width(100);//宽度
     45     }
     46     for (int i = 0; i < 5; i++)
     47     {
     48         CellTemp = table1.Cell(i + 1, 2);
     49         CellTemp.put_Width(315);
     50     }
     51     //写单元格内容
     52     CellTemp = table1.Cell(1, 1);
     53     CellTemp.Select();
     54     wordParagraphs.put_Alignment(1);//设置文字居中
     55     CFont0 fontTemp = sel.get_Font();
     56     fontTemp.put_Bold(1);
     57     fontTemp.put_Size(10);
     58     sel.TypeText(_T("1111"));
     59     sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0)));
     60     wordParagraphs.put_Alignment(1);//设置文字居中
     61     fontTemp.put_Bold(1);
     62     fontTemp.put_Size(10);
     63     sel.TypeText(_T("2222"));
     64     sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0)));
     65     wordParagraphs.put_Alignment(1);//设置文字居中
     66     fontTemp.put_Bold(1);
     67     fontTemp.put_Size(10);
     68     sel.TypeText(_T("3333"));
     69     sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0)));
     70     wordParagraphs.put_Alignment(1);//设置文字居中
     71     fontTemp.put_Bold(1);
     72     fontTemp.put_Size(10);
     73     sel.TypeText(_T("4444"));
     74     sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0)));
     75     wordParagraphs.put_Alignment(1);//设置文字居中
     76     fontTemp.put_Bold(1);
     77     fontTemp.put_Size(10);
     78     sel.TypeText(_T("5555"));
     79     sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0)));
     80 
     81     CellTemp = table1.Cell(1, 2);
     82     CellTemp.Select();
     83     wordParagraphs.put_Alignment(0);//设置文字居中
     84     fontTemp = sel.get_Font();
     85     fontTemp.put_Bold(0);
     86     fontTemp.put_Size(10);
     87     sel.TypeText(_T("一一一"));
     88     sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0)));
     89     wordParagraphs.put_Alignment(0);//设置文字居中
     90     fontTemp.put_Bold(0);
     91     fontTemp.put_Size(10);
     92     sel.TypeText(_T("二二二"));
     93     sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0)));
     94     wordParagraphs.put_Alignment(0);//设置文字居中
     95     fontTemp.put_Bold(0);
     96     fontTemp.put_Size(10);
     97     sel.TypeText(_T("三三三"));
     98     sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0)));
     99     wordParagraphs.put_Alignment(0);//设置文字居中
    100     fontTemp.put_Bold(0);
    101     fontTemp.put_Size(10);
    102     sel.TypeText(_T("一一
    二二
    三三"));
    103     sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0)));
    104     wordParagraphs.put_Alignment(0);//设置文字居中
    105     fontTemp.put_Bold(0);
    106     fontTemp.put_Size(10);
    107     sel.TypeText(_T("五五五"));
    108     sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0)));
    109 
    110     sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0)));
    111     fontTemp.put_Size(10);
    112     sel.TypeText(TEXT("不知道写啥就hello world吧
    "));
    113     //sel.TypeParagraph();    // 新起一段
    114 
    115     CTable0 table2 = tables.Add(sel.get_Range(), 4, 5, new CComVariant(), new CComVariant());//创建4行2列的表格
    116     CBorders borders2 = table2.get_Borders();
    117     borders2.put_InsideLineStyle(1);
    118     borders2.put_OutsideLineStyle(1);//设置表格边框,默认没有边框
    119 
    120     //设置单元格宽度
    121     for (int i = 0; i < 4; i++)
    122     {
    123         for (int j = 0; j < 2; j++)
    124         {
    125             CellTemp = table2.Cell(i + 1, j + 1);
    126             if (j == 0)
    127             {
    128                 CellTemp.put_Width(140);
    129             }
    130             else if (j == 1)
    131             {
    132                 CellTemp.put_Width(80);
    133             }
    134             else if (j == 2)
    135             {
    136                 CellTemp.put_Width(70);
    137             }
    138             else if (j == 3)
    139             {
    140                 CellTemp.put_Width(60);
    141             }
    142             else if (j == 4)
    143             {
    144                 CellTemp.put_Width(60);
    145             }
    146         }
    147     }
    148     //第一行标题
    149     CellTemp = table2.Cell(1, 1);
    150     CellTemp.Select();
    151     CString strTitleTemp;
    152     strTitleTemp = _T("AAA");
    153     wordParagraphs.put_Alignment(0);//设置文字居左
    154     fontTemp.put_Bold(1);
    155     fontTemp.put_Size(10);
    156     sel.TypeText(strTitleTemp);
    157     CellTemp = table2.Cell(1, 2);
    158     CellTemp.Select();
    159     strTitleTemp = _T("BBB");
    160     sel.TypeText(strTitleTemp);
    161     CellTemp = table2.Cell(1, 3);
    162     CellTemp.Select();
    163     strTitleTemp = _T("CCC");
    164     sel.TypeText(strTitleTemp);
    165     CellTemp = table2.Cell(1, 4);
    166     CellTemp.Select();
    167     strTitleTemp = _T("DDD");
    168     sel.TypeText(strTitleTemp);
    169     CellTemp = table2.Cell(1, 5);
    170     CellTemp.Select();
    171     strTitleTemp = _T("EEE");
    172     sel.TypeText(strTitleTemp);
    173 
    174     //第一列数据
    175     CString strTemp;
    176     CellTemp = table2.Cell(2, 1);
    177     CellTemp.Select();
    178     for (int i = 0; i < 4 - 1; i++)
    179     {
    180         wordParagraphs.put_Alignment(0);//设置文字居左
    181         fontTemp.put_Bold(0);
    182         fontTemp.put_Size(10);
    183         strTemp.Format(_T("第一列%d"), i+1);
    184         sel.TypeText(strTemp);
    185         sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0)));
    186     }
    187     //第二列数据
    188     CellTemp = table2.Cell(2, 2);
    189     CellTemp.Select();
    190     for (int i = 0; i < 4 - 1; i++)
    191     {
    192         wordParagraphs.put_Alignment(0);//设置文字居左
    193         fontTemp.put_Bold(0);
    194         fontTemp.put_Size(10);
    195         strTemp.Format(_T("第二列%d"), i + 1);
    196         sel.TypeText(strTemp);
    197         sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0)));
    198     }
    199     //第三列数据
    200     CellTemp = table2.Cell(2, 3);
    201     CellTemp.Select();
    202     for (int i = 0; i < 4 - 1; i++)
    203     {
    204         wordParagraphs.put_Alignment(0);//设置文字居左
    205         fontTemp.put_Bold(0);
    206         fontTemp.put_Size(10);
    207         strTemp.Format(_T("第三列%d"), i + 1);
    208         sel.TypeText(strTemp);
    209         sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0)));
    210     }
    211     //第四列数据
    212     CellTemp = table2.Cell(2, 4);
    213     CellTemp.Select();
    214     for (int i = 0; i < 4 - 1; i++)
    215     {
    216         wordParagraphs.put_Alignment(0);//设置文字居左
    217         fontTemp.put_Bold(0);
    218         fontTemp.put_Size(10);
    219         strTemp.Format(_T("第四列%d"), i + 1);
    220         sel.TypeText(strTemp);
    221         sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0)));
    222     }
    223     //第五列数据
    224     CellTemp = table2.Cell(2, 5);
    225     CellTemp.Select();
    226     for (int i = 0; i < 4 - 1; i++)
    227     {
    228         wordParagraphs.put_Alignment(0);//设置文字居左
    229         fontTemp.put_Bold(0);
    230         fontTemp.put_Size(10);
    231         strTemp.Format(_T("第五列%d"), i + 1);
    232         if (i%2)
    233         {
    234             fontTemp.put_Color(65280);
    235         }
    236         else
    237         {
    238             fontTemp.put_Color(255);
    239         }
    240         sel.TypeText(strTemp);
    241         sel.MoveDown(COleVariant((short)5), COleVariant(short(1)), COleVariant(short(0)));
    242     }
    243 
    244     //4.保存并退出
    245     doc.SaveAs(COleVariant(_T("E:\123.doc")), VOpt, VOpt, VOpt, VOpt, VOpt, VOpt, VOpt, VOpt,
    246         VOpt, VOpt, VOpt, VOpt, VOpt, VOpt, VOpt);
    247     doc.Close(vFalse, VOpt, VOpt);
    248     word_app.Quit(VOpt, VOpt, VOpt);
    249 }

    写完成的doc文件内容如图

  • 相关阅读:
    ios 初吻
    Oracle 的sql*plus编辑器真够简陋
    人月神话:软件界面交互和易用性改进总结
    IIS管理
    【C#】自定义新建一个DataTable(3列),循环3维矩形数组往其填充数据
    window.open完美替代window.showModalDialog
    Sql Server之ORDER BY不规则排序.如:中文月份排序
    JS设计模式之单体模式(Singleton)
    关于第三方库引用的问题
    [JSTL] 使用本地化资源文件的方法
  • 原文地址:https://www.cnblogs.com/ckrgd/p/14438260.html
Copyright © 2020-2023  润新知