用VC对Office进行操作的介绍已经不少了,但是从来没有把word,office/9.shtml' target='_blank' class='article'>excel,powerPoint进进全面的介绍的。
用例子来说明吧,首先创建一个MFC AppWizard(EXE)工程,然后通过在VIEW菜单中,选ClassWizard,
选Automation选项卡,选Add Class,选择From a TypeLibrary, 选中Microsoft Office 2000 类型库:Excel9.olb,MSPPT9.OLB,MSWORD9.OLB(在Microsoft Office\Office目录下) 会将类型库中的所有类添加到你的工程中。
然后写一个类来操作Office吧!
ObtGuiGcomOfficePrinter.h
#if !defined(AFX_OBTGUIGCOMOFFICEPRINTER_H__03A0C2D8_DFC8_4B51_8ADB_994B86BACB82__INCLUDED_)
#define AFX_OBTGUIGCOMOFFICEPRINTER_H__03A0C2D8_DFC8_4B51_8ADB_994B86BACB82__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "comdef.h"
#include "ObtGuiGcomMsWord9.h"
#include "ObtGuiGcomMsPpt9.h"
#include "ObtGuiGcomExcel9.h"
class AFX_EXT_CLASS ObtGuiGcomOfficePrinter
{
public:
ObtGuiGcomOfficePrinter();
virtual ~ObtGuiGcomOfficePrinter();
//Operator
public:
BOOL WordPrinterToJcf(LPCTSTR lpszFileName,LPCTSTR lpszActivePrinter);
BOOL ExcelPrinterToJcf(LPCTSTR lpszFileName,LPCTSTR lpszActivePrinter);
BOOL PowerPointPrinterToJcf(LPCTSTR lpszFileName,LPCTSTR lpszActivePrinter);
};
#include "stdafx.h"
#include "ObtGuiGcomOfficePrinter.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
ObtGuiGcomOfficePrinter::ObtGuiGcomOfficePrinter()
{
}
ObtGuiGcomOfficePrinter::~ObtGuiGcomOfficePrinter()
{
}
BOOL ObtGuiGcomOfficePrinter::PowerPointPrinterToJcf(LPCTSTR lpszFileName,LPCTSTR lpszActivePrinter)
{
_PptApplication m_powerpointApp;
Presentations m_powerpointPres;
_Presentation m_powerpointPre;
m_powerpointPres.ReleaseDispatch();
m_powerpointPre.ReleaseDispatch();
if(!m_powerpointApp.CreateDispatch("PowerPoint.Application"))
{
AfxMessageBox("创建PowerPoint服务失败!");
return FALSE;
}
m_powerpointApp.m_bAutoRelease=true;
m_powerpointApp.SetVisible(TRUE);//对于PowerPoint必须设置为TRUE
m_powerpointPres.AttachDispatch(m_powerpointApp.GetPresentations());
m_powerpointPres.Open(lpszFileName,-1,-1,-1);
m_powerpointPre.AttachDispatch(m_powerpointApp.GetActivePresentation(),TRUE);
m_powerpointPre.PrintOut(-1,-1,"",long(1),-1);
m_powerpointApp.Quit();
m_powerpointPre.ReleaseDispatch();
m_powerpointPres.ReleaseDispatch();
m_powerpointApp.ReleaseDispatch();
return TRUE;
}
BOOL ObtGuiGcomOfficePrinter::ExcelPrinterToJcf(LPCTSTR lpszFileName,LPCTSTR lpszActivePrinter)
{
_ExcelApplication m_excelApp;//定义Excel提供的应用程序对象
Workbooks m_excelBooks;
_Workbook m_excelBook;
m_excelBooks.ReleaseDispatch();
m_excelBook.ReleaseDispatch();
m_excelApp.m_bAutoRelease=true;
//创建Excel 2000服务器(启动Excel)
if (!m_excelApp.CreateDispatch("Excel.Application"))
{
AfxMessageBox("创建Excel服务失败!");
return FALSE;
}
m_excelApp.SetVisible(FALSE); //设置为隐藏
//利用模板文件建立新文档
m_excelBooks.AttachDispatch(m_excelApp.GetWorkbooks(),true);
m_excelBook.AttachDispatch(m_excelBooks.Add(_variant_t(lpszFileName)));
//m_excelApp.SetActivePrinter(lpszActivePrinter); //设置当前打印机
COleVariant covTrue((short)TRUE), covFalse((short)FALSE), covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);//定义打印机并打印
m_excelBook.PrintOut(covOptional,covOptional,COleVariant(long(1)),covFalse,covOptional,covOptional,covOptional,covOptional);
m_excelApp.Quit();//退出
m_excelBook.ReleaseDispatch();
m_excelBooks.ReleaseDispatch();
m_excelApp.ReleaseDispatch();
return TRUE;
}
BOOL ObtGuiGcomOfficePrinter::WordPrinterToJcf(LPCTSTR lpszFileName,LPCTSTR lpszActivePrinter)
{
_WordApplication m_wordApp;//定义Word提供的应用程序对象
Documents m_wordDocs;//定义Word提供的文档对象
_Document m_wordDoc; //当前的的文档对象
m_wordDocs.ReleaseDispatch();
m_wordDoc.ReleaseDispatch();
m_wordApp.m_bAutoRelease=true;
if(!m_wordApp.CreateDispatch("Word.Application")) //创建Word应用服务
{
AfxMessageBox("创建Word应用服务失败!");
return FALSE;
}
m_wordApp.SetVisible(FALSE); //设置为隐藏
//下面是打开文件定义VARIANT变量;
COleVariant varFilePath(lpszFileName);
COleVariant varstrNull("");
COleVariant varZero((short)0);
COleVariant varTrue(short(1),VT_BOOL);
COleVariant varFalse(short(0),VT_BOOL);
m_wordDocs.AttachDispatch(m_wordApp.GetDocuments());//将Documents类对象m_Docs和Idispatch接口关联起来;
m_wordDocs.Open(varFilePath,varFalse,varFalse,varFalse,varstrNull,varstrNull,
varFalse,varstrNull,varstrNull,varTrue,varTrue,varTrue);
m_wordDoc.AttachDispatch(m_wordApp.GetActiveDocument()); //得到当前激活的Document对象
m_wordApp.SetActivePrinter(lpszActivePrinter); //设置当前打印机
COleVariant covTrue((short)TRUE), covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); //定义打印属性
m_wordDoc.PrintOut(covFalse,
covOptional,
covOptional,
covOptional,
covOptional,
covOptional,
covOptional,
COleVariant((long)1),
covOptional,
covOptional,
covOptional,
covOptional,
covOptional,
covOptional,
covOptional,
covOptional,
covOptional,
covOptional);
m_wordApp.Quit(covOptional,covOptional,covOptional);//退出
m_wordDoc.ReleaseDispatch(); //断开关联;
m_wordDocs.ReleaseDispatch();
m_wordApp.ReleaseDispatch();
return TRUE;
}
以上是用Office打开相应的文档进行打印的操作,如果你需要进行相应的其他操作,
你可以用Office里面的宏进行录制然后转化为相应的代码。
例2:
给企业开发的项目中,客户经常要求将数据汇总显示并打印。站在程序员的立场上,简单数据的汇总打印可以采用VB自带报表,对复杂的数据用水晶报表或第三方打印控件。在企业中,汇总数据另一个目的是为了便于交流和共享资源,报表的格式通常固定,内容可以自己调整,他们对 Excel 和 Word 情有独钟。
刚完成的项目中,客户要求将汇总的数据以三种方式进行显示和打印:AutoCad、Excel 和 Word 文档中。前两种方式网上资源很多,对 Word 文档我找到例子不是很多。由于需要和 AutoCad 开发程序结合,不能将 Word 文档以 OLE 的方式 嵌入到程序中,文档只能在内存中形成,这将占用大量的CPU资源,另开一个线程进行处理,线程间以发送消息的方式进行通信。
首先在VC引入Word, 对格式相同的地方一定使用模版的方式进行处理,这可以大大的提高运行速度,Word中的模版为*.dot。加载模版的代码为:
sDocs=sApp.GetDocuments();
sDocs.AttachDispatch(sApp.GetDocuments(),true);
COleVariant vFalse((long)0),vTrue((long)1);
sDoc.AttachDispatch(sDocs.Add(COleVariant(_variant_t(strFileName)), vFalse,vFalse, vTrue));
需要注意的是,如果Word中使用到表格,VBA录制的全是Selection对象的处理方法,在VC中使用Selection对象的MoveUp和MoveDown函数时会出现不确定现象(如果你知道为什么的话请告诉我)。如果你需要绘制表格,不要用程序来动态形成,绘制表格的代码是很繁琐的,而且在页眉中绘制单元格是很难控制的。可以先在模版中绘出你要的表格格式,然后用程序复制单元格或者直接使用模版中的单元格。由于我做的项目表格的行数不确定,我采用的是复制单元格的方式(速度比较慢,谁有更好的方法?)。
剩下的处理流程,就是你想做什么操作,先在Word中录制一段宏,然后查看其VBA代码,并将其转化到Vc程序中。这就看你Word使用的熟练程度了,例如如何把不同页的页眉页脚设置不同,如何得到当前页的页数以及总的文档的页数,具体可以参考程序。
VC中也可以像Excel中调用模版中存在的宏,代码如下:
VARIANT vtMissing ;vtMissing.vt =VT_ERROR;
vtMissing.scode =DISP_E_PARAMNOTFOUND;
sApp.Run("AddCell",&vtMissing,&vtMissing,&vtMissing,
&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing,
&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing,
&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing,
&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing);
例3:
#include
#include
#pragma warning (disable:4146 4192 4786 4081)
#define USE_OFFICEXP 1
#ifdef USE_OFFICEXP
#import "C:\Program Files\Common Files\Microsoft Shared\Office10\MSO.DLL"
#import "c:\\Program Files\\Common Files\Microsoft Shared\\VBA\VBA6\VBE6EXT.olb"
#import "c:\Program Files\Microsoft Office\Office10\MSWORD.olb" rename("ExitWindows","ExitWindowsEx")
#import "c:\Program Files\Microsoft Office\Office10\office/9.shtml' target='_blank' class='article'>excel.exe" rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") \
rename("DocumentProperties", "DocumentPropertiesXL") exclude("IFont","IPicture")
#else
#import "c:\Program Files\Microsoft Office\Office\MSO9.dll"
#import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB" no_namespace
#import "c:\Program Files\Microsoft Office\Office\MSWORD9.OLB" rename("ExitWindows","_ExitWindows")
#import "c:\Program Files\Microsoft Office\Office\excel9.olb" rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") \
rename("DocumentProperties", "DocumentPropertiesXL") exclude("IFont","IPicture")
#endif
int main(int argc, char* argv[])
{
::CoInitialize(NULL);
CLSID clsid;
#if 1
CLSIDFromProgID(L"Word.Application", &clsid);
Word::_ApplicationPtr pApp;
//pApp.GetActiveObject (clsid);
pApp.CreateInstance (clsid,NULL);
pApp ->PutVisible (VARIANT_TRUE);
_variant_t bstrFileName = "C:\\Project\\Îĵµ\\ftp•þÎñÉè¼Æ.doc";
pApp ->GetDocuments() ->Open (&bstrFileName);
//pApp ->GetDocuments() ->Add();
Word::_DocumentPtr pWordDocument =pApp-> GetActiveDocument();
Word::SelectionPtr pSelection = pApp ->GetSelection();
Word::ParagraphsPtr pParagraphs = pWordDocument->GetParagraphs ();
long lParaCnt = pParagraphs ->GetCount ();
Word::ParagraphPtr pTemp;
Word::RangePtr pRange;
for (long iLoop =1;iLoop
pTemp = pParagraphs->Item (iLoop);
pRange = pTemp ->GetRange ();
printf("%s\n",(char*)pRange ->GetText());
printf("%s\n",(char*)(_bstr_t)pTemp->GetStyle ());
}
//pApp ->GetDocuments() ->Close ();
pApp ->Quit ();
#endif
#if 0
_variant_t varItem((long)1);
CLSIDFromProgID(L"Excel.Application", &clsid);
Excel::_ApplicationPtr pApp;
pApp .CreateInstance (clsid,NULL);
pApp ->PutVisible (0,VARIANT_TRUE);
Excel::_WorkbookPtr pWorkbook = pApp ->GetWorkbooks () ->Add ();
Excel::_WorksheetPtr pSheet = pWorkbook ->GetWorksheets () ->GetItem (varItem);
Excel::RangePtr pRange = pSheet ->GetUsedRange(0);
int iColCnt = pRange ->GetColumns() ->GetCount();
printf("used columns is %d\n",iColCnt);
int iRowCnt = pRange ->GetRows() ->GetCount();
printf("used rows is %d\n",iRowCnt);
pRange = pSheet ->GetRange(OLESTR("A1"));
pRange ->Value2 = OLESTR("zheng017");
pSheet ->GetRows ()-
Excel::InteriorPtr pInterior= pRange ->GetInterior ();
varItem.intVal = 0xff0000;
pInterior ->PutColor (varItem);
pSheet ->PrintPreview ();
#endif
::CoUninitialize();
return 0;
}