加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

使用VC++2008操作word2003生成报表

(2010-07-18 14:41:13)
标签:

vs2008

vc

mfc

word

报表

分类: 编程心得

  使用VC编程来操纵Office。你可以实现诸如:Word文件打印、传送数据到Word文档、发送E-MAIL、自动产生表格、Excel数据统计、圆饼图,直方图显示、自动报表生成、播放幻灯、doc,txt,HTML,rtf文件转换、中文简繁体转换、拼音或笔画排序......只要是Office能够实现的功能,都可以在你写的程序中调用。

http://www.vckbase.com/document/image/paragraph.gif 一、概念
  Microsoft 的 Office 产品中,都提供了OLE Automation 自动化程序的接口。如果你使用VB,VBA 和 Script 脚本调用 Office 功能的话,其实比使用 VC 调用要简单的多。比如在 WORD 中,调出菜单“工具(T)宏(M)录制新宏(R)”,这时候它开始记录你在 WORD 中任何菜单和键盘的操作,把你的操作过程保存起来,以便再次重复调用。而保存这些操作的记录,其实就是使用了 VBA 程序(Visual Basic for Application)。而我们下面要实现的功能,也同样要参考 VBA 的方法。

http://www.vckbase.com/document/image/paragraph.gif 二、结构层次
  为了更有逻辑,更有层次地操作 Office,Microsoft 把应用(Application)按逻辑功能划分为如下的树形结构

Application(WORD 为例,只列出一部分)
  Documents(所有的文档)
        Document(一个文档)
            ......
  Templates(所有模板)
        Template(一个模板)
            ......
  Windows(所有窗口)
        Window
        Selection
        View
  Selection(编辑对象)
        Font
        Style
        Range
        ......
  ......

只有了解了逻辑层次,我们才能正确的操纵 Office。举例来讲,如果给出一个VBScript语句是:
      application.ActiveDocument.SaveAs "c:abc.doc"
那么,我们就知道了,这个操作的过程是:第一步,取得Application;第二步,从Application中取得ActiveDocument;第三步,调用 Document 的函数 SaveAs,参数是一个字符串型的文件名。

http://www.vckbase.com/document/image/paragraph.gif 三、基本步骤
(1)创建(或打开已有的)一个 MFC 的程序工程。
(2)执行 项目-->Add Class-->From a type Library--> 在 Office 目录中,找到你想使用的类型库。现使用的是 Office2003,其Word 的类型库文件,保存在 c:\Program Files\Microsoft Office\Office11\MSWORD.OLB。

(3)选择类型库文件后,在弹出的对话窗中继续选择要添加的类。我们现在添加——Application,Documents,Document,Range,Borders, Font,ParagraphFormat,Selection,Table,Tables,Columns,Cell。这时候会在项目中添加N多类。CApplication类对应Application对象,获取WordApplication对象,可以控制word的显示,例如显示打印预览,状态栏是否可见等CDocument0类对应Document对象,打开或者创建一个word文件时,Document对象就产生了。CDocuments类对应Documents对象这是一个Document对象的集合。操作多个word时需要。CRange类对应Range对象,该对象用于操作word中的一块区域,Range对象代表word中一块连续的区域,有起始位置和结束位, VC中用COleVarian对象的intVal属性表示起始位和结束位。put_Text向该区域中写入文字。CTables0类对应表格对象,还有Column,Row,Cell对象。

(4)初始化COM。方法一,找到App的InitInstance()函数,在其中添加 AfxOleInit()函数的调用;方法二,在需要调用COM功能的地方 CoInitialize(NULL),调用完毕后 CoUninitialize()。
(5)在你需要调用 Office 功能函数的 cpp 文件中
     #include <atlbase.h>  // 为了方便操作 VARIANT 类型变量,使用 CComVariant 模板类
    #include "CApplication.h"

    #include "CDocuments.h"
    #include "CDocument0.h"
    #include "CRange.h"
    #include "CBorders.h"
    #include "CFont0.h"
    #include "CParagraphFormat.h"
    #include "CSelection.h"
    #include "CTable0.h"
    #include "CTables0.h"
    #include "CColumns0.h"
    #include "CCell.h"

  // 具体的头文件名,是由装载类型库的文件名决定的。

在每个新加的office头文件中,即上边这些头文件中("CApplication.h","CDocuments.h",,"CDocument0.h", "CRange.h", "CBorders.h", "CFont0.h", "CParagraphFormat.h","CSelection.h", "CTable0.h", "CTables0.h", "CColumns0.h","CCell.h")将"c:\Program Files\Microsoft Office\Office11\MSWORD.OLB"  no_namespace 替换为

#import "C:\\Program Files\\Microsoft Office\\OFFICE11\\MSWORD.OLB" no_namespace     raw_interfaces_only \
rename("FindText","_FindText")       \
rename("Rectangle","_Rectangle")   \
rename("ExitWindows","_ExitWindows")

(6)好了,现在开始写程序吧。另外要说明的是,步骤2和3,其实也可以使用 #import 方式引入类型库。

新建一个对话框程序,建一按钮

添加如下代码
 CString strField[8],strTop;
strField[0]="商品编号";
strField[1]="商品名称";
strField[2]="现有数量";
strField[3]="价    格";
strField[4]="采购数量";
strField[5]="采购价格";
strField[6]="供应商";
strField[7]="计划采购时间";

CString Title="计划采购信息";

CWaitCursor wait;
CApplication wordApp;
CDocument0 wordDoc;
CDocuments wordDocs;
if(!wordApp.CreateDispatch("word.Application"))
{
   AfxMessageBox("Failed!");
   return;
}
LPDISPATCH pDocs=wordApp.get_Documents();
wordDocs.AttachDispatch(pDocs);
VARIANT varUnit;
VARIANT varOptional;
VariantInit(&varUnit);
varUnit.vt=VT_I4;
varUnit.lVal=5;
//
VariantInit(&varOptional);
varOptional.vt=VT_ERROR;
varOptional.scode=DISP_E_PARAMNOTFOUND;
//
LPDISPATCH pDoc=wordDocs.Add(&varOptional,&varOptional,&varOptional,&varOptional);
wordDoc.AttachDispatch(pDoc);

long nRows,nCols;
nRows=8;
nCols=8;
//
CRange wordRange;
wordRange=wordDoc.Range(&varOptional,&varOptional);
//
CSelection wordSelec=wordApp.get_Selection();
CFont0 oFont;
CParagraphFormat wordFormat;

//标题
wordSelec.HomeKey(&varUnit,&varOptional);

oFont=wordSelec.get_Font();
oFont.put_Size(12);
oFont.put_Name("宋体");
wordFormat=wordSelec.get_ParagraphFormat();
wordFormat.put_Alignment(1);
wordSelec.InsertAfter(LPCSTR(Title));
wordSelec.InsertParagraphAfter();

oFont.put_Size(16);
oFont.put_Name("黑体");
wordFormat=wordSelec.get_ParagraphFormat();
wordFormat.put_Alignment(1);
wordSelec.InsertAfter(LPCSTR(strTop));
wordSelec.InsertParagraphAfter();

wordSelec.EndKey(&varUnit,&varOptional);

//设置表格
wordRange=wordSelec.get_Range();
oFont=wordSelec.get_Font();
oFont.put_Size(10);
oFont.put_Name("宋体");
wordFormat=wordSelec.get_ParagraphFormat();
wordFormat.put_Alignment(1);

VARIANT varTable;
VariantInit(&varTable);
varUnit.vt=VT_I4;
varUnit.lVal=0;
//
CTables0 wordTables=wordSelec.get_Tables();
CTable0 wordTable=wordTables.Add(wordRange,nRows,nCols,&varOptional,&varTable);
//
CBorders tblBorders;
tblBorders=wordTable.get_Borders();
tblBorders.put_InsideLineStyle(1);
tblBorders.put_OutsideLineStyle(7);
//
CColumns0 tblColumns;
tblColumns=wordTable.get_Columns();
tblColumns.get_PreferredWidthType();
//
int i,j,n;
char buf[50];
CString strValue;
CCell tblCell;

for(i=1;i<=nRows;i++)
{
   for(j=1;j<=nCols;j++)
   {
    if(i==1)
    {
     tblCell=wordTable.Cell(i,j);
     wordRange=tblCell.get_Range();
     oFont=wordRange.get_Font();
     oFont.put_Name("黑体");
     n=j-1;
     strValue=strField[n];
     wordRange.InsertAfter(strValue);
    }
    else
    {
     tblCell=wordTable.Cell(i,j);
     wordRange=tblCell.get_Range();
     n=j-1;
     if(n==0 || n==1 || n==6)
     {
         strValue="016";
     }
     if(n==2 || n==3 || n==4 ||n==5)
    
      strValue="2345";
     }
     if(n==7)
     {
      strValue="7";
     }
     //strValue.TrimRight();
     wordRange.InsertAfter(strValue);
    }
   }

}
wordApp.put_Visible(true);
wordDoc.Activate();

试试看行不行,我也是新手,咱们可以交流交流。

计划采购信息

 

商品编号

商品名称

现有数量

价   

采购数量

采购价格

供应商

计划采购时间

016

016

2345

2345

2345

2345

016

7

016

016

2345

2345

2345

2345

016

7

016

016

2345

2345

2345

2345

016

7

016

016

2345

2345

2345

2345

016

7

016

016

2345

2345

2345

2345

016

7

016

016

2345

2345

2345

2345

016

7

016

016

2345

2345

2345

2345

016

7

 

 大概是这样的,我也是刚学。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有