运用VC或Java对Office进行编程操作 - etre的专栏 - CSDN博客

来源:百度文库 编辑:神马文学网 时间:2024/04/29 23:22:36
运用VC或Java对Office进行编程操作 收藏
      用VC对Office进行操作的介绍已经不少了,但是从来没有把word,excel,powerPoint进进全面的介绍的。    由于工作的需要,我需要对在自己的软件中对word,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里面的宏进行录制然后转化为相应的代码。对于在Java里需要时行Office操作的,你必须用JNI调用或运用Java-Com的相应包 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/etre/archive/2003/07/28/17562.aspx