使用VC++2008操作word2003生成报表
标签:
vs2008vcmfcword报表 |
分类: 编程心得 |
使用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(所有的文档)
Templates(所有模板)
Windows(所有窗口)
Selection(编辑对象)
......
只有了解了逻辑层次,我们才能正确的操纵 Office。举例来讲,如果给出一个VBScript语句是:
那么,我们就知道了,这个操作的过程是:第一步,取得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,
(4)初始化COM。方法一,找到App的InitInstance()函数,在其中添加
AfxOleInit()函数的调用;方法二,在需要调用COM功能的地方 CoInitialize(NULL),调用完毕后
CoUninitialize()。
(5)在你需要调用 Office 功能函数的 cpp 文件中
在每个新加的office头文件中,即上边这些头文件中("CApplication.h","CDocuments.h",,"CDocument0.h",
"CRange.h",
#import "C:\\Program Files\\Microsoft
Office\\OFFICE11\\MSWORD.OLB"
no_namespace
rename("FindText","_FindText")
rename("Rectangle","_Rectangle")
rename("ExitWindows","_ExitWindows")
(6)好了,现在开始写程序吧。另外要说明的是,步骤2和3,其实也可以使用 #import 方式引入类型库。
新建一个对话框程序,建一按钮
添加如下代码
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"))
{
}
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++)
{
}
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 |

加载中…