新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论高级C/C++编程、代码重构(Refactoring)、极限编程(XP)、泛型编程等话题
    [返回] 计算机科学论坛计算机技术与应用『 C/C++编程思想 』 → VC操作WORD[转帖] 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 4738 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: VC操作WORD[转帖] 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     一分之千 帅哥哟,离线,有人找我吗?射手座1984-11-30
      
      
      威望:1
      等级:研一(随老板参加了WWW大会还和Tim Berners-Lee合了影^_^)
      文章:632
      积分:4379
      门派:XML.ORG.CN
      注册:2006/12/31

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给一分之千发送一个短消息 把一分之千加入好友 查看一分之千的个人资料 搜索一分之千在『 C/C++编程思想 』的所有贴子 引用回复这个贴子 回复这个贴子 查看一分之千的博客楼主
    发贴心情 VC操作WORD[转帖]

    近日作一个小东西涉及到对汇总结果的打印,由于打印只在一台特定的机器进行所以考虑使用Word来实现,这样比较简单而且很专业。开始的时候这个东西没有这项功能后来我闲的无聊看看是否可以实现(以前从来没有做过这方面的东西),于是乎搞了一下也不是很难,一点心得大家给指点一二,使用的是Office2000,工程类型是MFC的对话框。先看看需要使用到的东东: 1.View->ClassWizard->Automation->Add Class From Library:定位到你安装Office的目录找到MSWord9.Olb 2.打开后把所有的类都选中倒入到你的工程(反正也是搞一回彻底一点:P) 3.在你的对话框头文件中加上#include"msword9.h" OK了准备工作搞定之后就可以玩儿转Word了,先来一个简单的
    void TestWord1()
    {
    _Application app;
    COleVariant vTrue((short)TRUE), vFalse((short)FALSE);
    app.CreateDispatch(_T("Word.Application"));
    app.SetVisible(FALSE);
    //Create New Doc
    Documents docs=app.GetDocuments();
    CComVariant tpl(_T("")),Visble,DocType(0),NewTemplate(false);
    docs.Add(&tpl,&NewTemplate,&DocType,&Visble);
    //Add Content:Text
    Selection sel=app.GetSelection();
    sel.TypeText(_T("\t\t\t\t\t第一次玩儿Word\r\n"));
    sel.TypeText(_T("\t\t\t\t\t\t\t\t----------先来一个简单的\r\n"));
    sel.ReleaseDispatch();
    docs.ReleaseDispatch();
    app.SetVisible(TRUE);
    app.ReleaseDispatch();
    }
    大家都应该使用过Word提供的表格功能很是专业,如果在作一些数据库开发之类的软件对汇总的数据进行打印是常见的功能,这个如果用Word来实现个人感觉不错,废话少说说练咱就练。
    void CWordTestDlg::TestWord2()
    {
    _Application app;
    COleVariant vTrue((short)TRUE), vFalse((short)FALSE);
    app.CreateDispatch(_T("Word.Application"));
    app.SetVisible(FALSE);
    //Create New Doc
    Documents docs=app.GetDocuments();
    CComVariant tpl(_T("")),Visble,DocType(0),NewTemplate(false);
    docs.Add(&tpl,&NewTemplate,&DocType,&Visble);
    //Add Content:Text
    Selection sel=app.GetSelection();
    sel.TypeText(_T("\t\t\t\t\t\t\t情况汇总\r\n"));
    sel.TypeText(_T("\t\t\t\t\t\t\t\t----------*******跨国公司\r\n"));
    COleDateTime dt=COleDateTime::GetCurrentTime();
    CString strDT=dt.Format("%Y-%m-%d");
    CString str("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t");
    str+=strDT;
    str+="\r\n";
    sel.TypeText(str);
    //Add Table
    _Document saveDoc=app.GetActiveDocument();
    Tables tables=saveDoc.GetTables();
    CComVariant defaultBehavior(1),AutoFitBehavior(1);
    tables.Add(sel.GetRange(),6,11,&defaultBehavior,&AutoFitBehavior);
    Table table=tables.Item(1);
    sel.TypeText(_T("Test1"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short
    (0)));

    sel.TypeText(_T("Test2"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test3"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test4"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short<(0)));
    sel.TypeText(_T("Test5"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test6"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test7"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test8"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test9"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test10"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test11"));  
    app.SetVisible(TRUE);
    table.ReleaseDispatch();
    tables.ReleaseDispatch();
    sel.ReleaseDispatch();
    docs.ReleaseDispatch();
    saveDoc.ReleaseDispatch();
    app.ReleaseDispatch();
    }
    上面说了如何制作一个简单的表格,在实际应用中会发现表格的种类很多其中对于单元格要求合并的情形很多,这样出来的表格比较专业让客户看起来也很是舒服,不喔喔了看看下面的代码就全明白了。
    void CWordTestDlg::TestWord3()
    {
    _Application app;
    COleVariant vTrue((short)TRUE), vFalse((short)FALSE);
    app.CreateDispatch(_T("Word.Application"));
    app.SetVisible(FALSE);
    //Create New Doc
    Documents docs=app.GetDocuments();
    CComVariant tpl(_T("")),Visble,DocType(0),NewTemplate(false);
    docs.Add(&tpl,&NewTemplate,&DocType,&Visble);
    //Add Content:Text
    Selection sel=app.GetSelection();
    sel.TypeText(_T("\t\t\t\t\t\t\t情况汇总\r\n"));
    sel.TypeText(_T("\t\t\t\t\t\t\t\t----------*******跨国公司\r\n"));
    COleDateTime dt=COleDateTime::GetCurrentTime();
    CString strDT=dt.Format("%Y-%m-%d");
    CString str("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t");
    str+=strDT;
    str+="\r\n";
    sel.TypeText(str);
    //Add Table
    _Document saveDoc=app.GetActiveDocument();
    Tables tables=saveDoc.GetTables();
    CComVariant defaultBehavior(1),AutoFitBehavior(1);
    tables.Add(sel.GetRange(),7,11,&defaultBehavior,&AutoFitBehavior);
    Table table=tables.Item(1);
    sel.TypeText(_T("Test1"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test2"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test3"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test4"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test5"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test6"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test7"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test8"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test9"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test10"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test11"));  
    for(int i=2;i<7;i+=2)
    {
      Cell c1=table.Cell(i,1);
     Cell c2=table.Cell(i+1,1);
     c1.Merge(c2);
     c1.ReleaseDispatch();
     c2.ReleaseDispatch();
    }
    app.SetVisible(TRUE);
    table.ReleaseDispatch();
    tables.ReleaseDispatch();
    sel.ReleaseDispatch();
    docs.ReleaseDispatch();
    saveDoc.ReleaseDispatch();
    app.ReleaseDispatch();
    app.SetVisible(TRUE);
    }

    下面说说如何填充单元格以及如何对合并后的单元格的内容让他居中显示这样能够看得舒服一点:)。代码如下所示:
    void CWordTestDlg::TestWord4()
    {
    _Application app;
    COleVariant vTrue((short)TRUE), vFalse((short)FALSE);
    app.CreateDispatch(_T("Word.Application"));
    app.SetVisible(FALSE);
    //Create New Doc
    Documents docs=app.GetDocuments();
    CComVariant tpl(_T("")),Visble,DocType(0),NewTemplate(false);
    docs.Add(&tpl,&NewTemplate,&DocType,&Visble);
    //Add Content:Text
    Selection sel=app.GetSelection();
    sel.TypeText(_T("\t\t\t\t\t\t\t情况汇总\r\n"));
    sel.TypeText(_T("\t\t\t\t\t\t\t\t----------*******跨国公司\r\n"));
    COleDateTime dt=COleDateTime::GetCurrentTime();
    CString strDT=dt.Format("%Y-%m-%d");
    COleDateTimeSpan span(1,0,0,0);
    CString strDT1=(dt-span).Format("%Y-%m-%d");
    CString strDate[]={strDT1,strDT};
    CString str("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t");
    str+=strDT;
    str+="\r\n";
    sel.TypeText(str);
    //Add Table
    _Document saveDoc=app.GetActiveDocument();
    Tables tables=saveDoc.GetTables();
    CComVariant defaultBehavior(1),AutoFitBehavior(1);
    tables.Add(sel.GetRange(),7,11,&defaultBehavior,&AutoFitBehavior);
    Table table=tables.Item(1);
    sel.TypeText(_T("Test1"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test2"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test3"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test4"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test5"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test6"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test7"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test8"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test9"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test10"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test11"));  
    int i=0,j=0;
    for(i=2;i<7;i+=2)
    {
     Cell c1=table.Cell(i,1);
     Cell c2=table.Cell(i+1,1);
     c1.Merge(c2);
     c1.ReleaseDispatch();
     c2.ReleaseDispatch();
    }
    Cell c;
    CString strName[]={"yingkou","zyq654321","iwaswzq"};
    for(j=0,i=0;i<3;++i,j+=2)
    {
     c=table.Cell(j+2,1);
     c.Select();
     Cells cs=sel.GetCells();
     cs.SetVerticalAlignment(1);
     sel.TypeText(strName[i]);  
     sel.MoveDown(COleVariant((short)5),COleVariant(short(1)),COleVariant(short(0)));
     cs.ReleaseDispatch();
     c.ReleaseDispatch();
    }
    c=table.Cell(7,1);
    c.Select();
    for(i=0,j=0;i<6;++i,++j)
    {
     if(i%2==0)
      j=0;
     sel.TypeText(strDate[j]);  
     sel.MoveUp(COleVariant((short)5),COleVariant(short(1)),COleVariant(short(0)));
    }
    c.ReleaseDispatch();
    app.SetVisible(TRUE);
    table.ReleaseDispatch();
    tables.ReleaseDispatch();
    sel.ReleaseDispatch();
    docs.ReleaseDispatch();
    saveDoc.ReleaseDispatch();
    app.SetVisible(TRUE);
    app.ReleaseDispatch();
    }


       收藏   分享  
    顶(0)
      




    ----------------------------------------------
    越学越无知

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/6/22 20:10:00
     
     GoogleAdSense射手座1984-11-30
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 C/C++编程思想 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/11/26 14:59:07

    本主题贴数1,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    62.500ms