zoukankan      html  css  js  c++  java
  • C++操作Word学习笔记(四)

    【当前博文转自http://blog.sina.com.cn/s/blog_4aaac71b01000brk.html

    【本文有打印相关操作】

    1、初始化COM库
    2、利用COM接口提供的函数,打开默认的模版文档。对Word进行读写等操作,下面代码包括写入文本,在表格中写入文本,实现控制页数,查找特定字符、打印等操作。
    3、小博开始常更新了,学了什么我就博上什么,欢迎大家光临。
     
    void CWordDlg::OnBnClickedButtonCreate()
    {
     // TODO: Add your control notification handler code here
     //初始化COM库
     AfxOleInit();
     
     //定义变量
     COleVariant varstrNull("");
     COleVariant varTrue(short(1),VT_BOOL);
     COleVariant varFalse(short(0),VT_BOOL);
     COleVariant vTure(SHORT(TRUE)), vFalse(SHORT(FALSE));
     COleVariant vE(_T(""));
     COleVariant v0(SHORT(0)), v1(SHORT(1)), v2(SHORT(2)), v5(SHORT(5)), v6(SHORT(6)), v12(SHORT(12)), v22(SHORT(22));
     CString strPath = "..\Perforation.tdt";
     //根据模版创建Word文档
     ExportMessage(1);
     CApplication app;
     app.CreateDispatch("Word.Application");
     app.put_Visible(TRUE);
     //word文档
     CDocuments docs = app.get_Documents();
     docs.AttachDispatch(app.get_Documents()); //将Documents类对象m_Docs和Idispatch接口关联起来;
     //打开文档
     docs.Open2000(COleVariant(strPath),varFalse,varFalse,varFalse,
      varstrNull,varstrNull,varFalse,varstrNull,
      varstrNull,varTrue,varTrue,varTrue);
     
     ExportMessage(2);
     CDocument0 doc0 = app.get_ActiveDocument();
     CSelection select = app.get_Selection();
     //写入文本
     select.TypeText("The First Table!");
     //选择表格
     CTables0 tabs = doc0.get_Tables();
     CTable0 tab1 = tabs.Item(1);
     CTable0 tab2 = tabs.Item(2);
     CCell0 cell = tab1.Cell(1,2);
     cell.Select();
     select.TypeText("12");
     cell = tab1.Cell(1,6);
     cell.Select();
     select.TypeText("16");
     cell = tab1.Cell(4,2);
     cell.Select();
     select.TypeText("42");
     //获得当前页信息
     COleVariant vAllPag(select.get_Information(4));
     int nAllPag = vAllPag.intVal;
     int nCurPag = nAllPag;
     
     CString strData;
     int i,j;
     i=1;
     while(nAllPag >= nCurPag)
     {
      for(j=1;j<=13;j++)
      {
       cell = tab2.Cell(i,j);
       cell.Select();
       strData.Format("%d",i);
       select.TypeText(strData);  //输入数据
       select.MoveRight(v1,v1,v0);  //右移
      }
     
      select.Collapse(v1);    //换行
      COleVariant vAllPagNew(select.get_Information(4));
      nCurPag = vAllPagNew.intVal;
      i++;
     }
     //查找END字符,删除行
     COleVariant vEnd(_T("END")),vT(SHORT(TRUE)), vF(SHORT(FALSE));
     
     CDocument0 Fnd(select.get_Find());  //Fnd和select相关联
     Fnd.Exe(vEnd, vF, vF, vF, vF, vF, vT, v1, vF, COleVariant(""), vT, vF, vF, vF, vF);
     select.MoveRight(v1,v1,v0);
     select.TypeBackspace();
     select.TypeBackspace();
     
     //释放对象
     cell.ReleaseDispatch();
     tab1.ReleaseDispatch();
     tab2.ReleaseDispatch();
     tabs.ReleaseDispatch();
     //保存word
     ExportMessage(3);
     CFile file;
     CString strSaveFile;
     BOOL bUse = TRUE;
     while(bUse)
     {
      CFileDialog fileDialog(FALSE);
      fileDialog.m_ofn.lpstrTitle = "保存Word文档";
      fileDialog.m_ofn.lpstrFilter = "Word Document(*.doc)*.docAll Files(*.*)*.*";
      fileDialog.m_ofn.lpstrDefExt = ".doc";
      if(IDOK == fileDialog.DoModal())
      {
       strSaveFile = fileDialog.GetPathName();
       if(file.Open(strSaveFile, CFile::modeWrite | CFile::modeCreate))
       {
        file.Close();
        bUse = FALSE;
        doc0.SaveAs(COleVariant(strSaveFile), v0, vF, vE, vF, vE, vF, vF, vF, vF, vF, vF, vF, vF, vF, vF);
       }
       else
       {
        bUse = TRUE;
        AfxMessageBox("文件正在编辑,无法进行存储!");
       }
      }
      else
      {
       bUse = FALSE;
       AfxMessageBox("取消存储!如要使用数据,请再次输出!");
      }
     }
     //打印
     ExportMessage(4);
     if(IsPrint())
     {
      ExportMessage(5);
      ShellExecute(NULL,"print",strSaveFile,"","",SW_HIDE);
     }
     select.ReleaseDispatch();
     doc0.ReleaseDispatch();
     docs.ReleaseDispatch();
     app.Quit(vFalse, vFalse, vFalse);
     app.ReleaseDispatch();
     ExportMessage(0);
    }

    void CWordDlg::ExportMessage(int nType)
    {
     CString strMessage;
     switch(nType)
     {
     case 0:
      strMessage = "就绪";
      break;
     case 1:
      strMessage = "创建Word文档...";
      break;
     case 2:
      strMessage = "写入数据...";
      break;
     case 3:
      strMessage = "保存Word文档...";
      break;
     case 4:
      strMessage = "正在检查当前打印机是否可用 ...";
      break;
     case 5:
      strMessage = "打印Word文档...";
      break;
     default:
      break;
     }
     GetDlgItem(IDC_MESSAGE)->SetWindowText(strMessage);
    }
    BOOL CWordDlg::IsPrint()
    {
     CApplication App;
     COleVariant vF(SHORT(FALSE));
     if(!App.CreateDispatch("Word.Application"))
     {
      AfxMessageBox("当前打印机安装有问题,无法打印!");
      return FALSE;
     }
     CString strPrt(App.get_ActivePrinter());
     strPrt = strPrt.Left(strPrt.ReverseFind('on')-1);
     strPrt.TrimRight();
     App.Quit( vF, vF, vF);
     HANDLE hPrt;
     if(!OpenPrinter(strPrt.GetBuffer(), &hPrt, NULL))
     {
      AfxMessageBox("当前打印机安装有问题,无法打印!");
      return FALSE;
     }
     return ClosePrinter(hPrt);
    }

  • 相关阅读:
    力扣(LeetCode)922. 按奇偶排序数组 II
    力扣(LeetCode)1002. 查找常用字符
    力扣(LeetCode)15. 三数之和
    Java == 和 equals 区别
    力扣(LeetCode)125. 验证回文串
    力扣(LeetCode) 905. 按奇偶排序数组
    力扣(LeetCode)832. 翻转图像
    力扣(LeetCode) 771. 宝石与石头
    Sticks
    荷马史诗
  • 原文地址:https://www.cnblogs.com/For-her/p/3499763.html
Copyright © 2011-2022 走看看