MFC 简单封装word, office操作。

来源:互联网 发布:拜占庭共识算法 编辑:程序博客网 时间:2024/05/16 04:51

1.环境VS6.0,office 2003 word, excel Old(类型库文件)

2.测试机器,office 2007,windows xp.

使用vs6导入的类,word和excel会导致工程重定义的错误。这里用C++名字空间来解决;

修改如下:

//msword.h

namespace word{

....原来的代码

}

//msword.cpp

...

#ifdef _DEBUG

...

#endif

namespace word

{

...原来的代码

}

excel类似的方法

namespace excel{

...

}


word, excel简单封装类,写的简单,目前能想到的功能不多。

//office_mgr.h

#include "excel9.h"
#include "msword.h"

class excel_mgr{
public:
excel_mgr();
~excel_mgr();


bool createExcelApp();

bool createWorkBooks();
bool createWorkBook();


bool openWorkSheet(const CString& filename);


bool saveWorkSheet();
bool saveWorkSheetAs(const CString& filename);


bool setCellText(int row, int column, const CString& text);
bool setCellText(const CString& s, const CString& text);


bool closeWorkSheet();
bool closeExcelApp();


bool showExcel();
bool hideExcel();

protected:
CString indexToString( int row, int col );
private:
excel::_Applicationm_excel_app;
excel::Worksheetsm_worksheets;
excel::_Worksheetm_worksheet;
excel::Workbooksm_workbooks;
excel::_Workbookm_workbook;

};




class word_mgr
{
public:
word_mgr();
~word_mgr();


bool showWord();
bool hideWord();

//report about word operator
bool reportFromTemplate(CString templatefilename, bool showword = true);

bool createWordApp();
bool createDocuments();
bool createDocument();

bool openDocument(const CString& filename);
bool saveDocumentAs(const CString& filename);
bool closeWordApp();
bool closeDocument();


bool writeText(const CString& text);
bool typeBookMarkText(const CString& bookmarkname, const  CString& value);
bool typeBookMarkImage(const  CString& bookmarkname, const  CString &imgfilename);



private:
word::_Applicationm_word;
word::Documentsm_wordDocs;
word::_Documentm_wordDoc;
word::Selectionm_wordSel;
word::Range m_wordRange;
word::Bookmarksm_doc_bookmarks;

};

//office_mgr. cpp

#include "stdafx.h"
#include "office_mgr.h"
#include <atlbase.h>


excel_mgr::excel_mgr()
{


}
excel_mgr::~excel_mgr()
{


}
bool excel_mgr::createExcelApp()
{
COleException pe;
if(m_excel_app.m_lpDispatch)
{
m_excel_app.ReleaseDispatch();
}
if(!m_excel_app.CreateDispatch(_T("Excel.Application"), NULL))
{
pe.ReportError();
return false;
}
return true;
}
bool excel_mgr::createWorkBooks()
{
if(!m_excel_app.m_lpDispatch)
{
if(!createExcelApp())
return false;
}
if(m_workbooks.m_lpDispatch)
{
m_workbooks.ReleaseDispatch();
}
m_workbooks.AttachDispatch(m_excel_app.GetWorkbooks());
if(!m_workbooks.m_lpDispatch)
{
return false;
}

return true;
}

CString excel_mgr::indexToString( int row, int col )   
{   
    CString strResult;  
    if( col > 26 )   
    {   
        strResult.Format(_T("%c%c%d"),'A' + (col-1)/26-1,'A' + (col-1)%26,row);  
    }   
    else   
    {   
        strResult.Format(_T("%c%d"), 'A' + (col-1)%26,row);  
    }   
    return strResult;  
}   
bool excel_mgr::createWorkBook()
{
CComVariant Template(_T(""));
if(!m_workbooks.m_lpDispatch)
{
return false;
}
m_workbook.AttachDispatch(m_workbooks.Add(Template));
m_worksheets = m_excel_app.GetWorksheets();
m_worksheet = m_excel_app.GetActiveSheet();
if(!m_worksheet.m_lpDispatch || !m_worksheets.m_lpDispatch || !m_workbook.m_lpDispatch)
return false;
return true;
}
bool excel_mgr::saveWorkSheetAs(const CString& filename)
{
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
if(!m_workbook.m_lpDispatch)
{
return false;
}
m_workbook.SaveAs(
COleVariant(filename),
covOptional,  
covOptional,
covOptional,  
covOptional,
covOptional,
(long)0,  
covOptional,
covOptional,
covOptional,  
covOptional
); 
return true;
}

bool excel_mgr::showExcel()
{
m_excel_app.SetVisible(TRUE);
return true;
}
bool excel_mgr::hideExcel()
{
m_excel_app.SetVisible(FALSE);
return true;
}


bool excel_mgr::closeWorkSheet()
{
m_workbook.SetSaved(TRUE);
return true;
}
bool excel_mgr::closeExcelApp()
{
COleVariant vTrue((short)TRUE),      
vFalse((short)FALSE),  
vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);  
    m_excel_app.Quit();   
    if(m_worksheet.m_lpDispatch)  
        m_worksheet.ReleaseDispatch();  
    if(m_worksheets.m_lpDispatch)  
        m_worksheets.ReleaseDispatch();   
    if(m_workbooks.m_lpDispatch)  
        m_workbooks.ReleaseDispatch();  
    if(m_workbook.m_lpDispatch)  
        m_workbook.ReleaseDispatch();
if(m_excel_app.m_lpDispatch)
m_excel_app.ReleaseDispatch();
return true;
}
bool excel_mgr::openWorkSheet(const CString& filename)
{
return true;
}

bool excel_mgr::setCellText(int row, int column, const CString& text)
{
if(!m_worksheet.m_lpDispatch)
{
return false;
}

excel::Range range =m_worksheet.GetRange(COleVariant(indexToString(row,column)),COleVariant(indexToString(row,column)));  
range.SetValue(COleVariant(text));
range.ReleaseDispatch();
return true;
}
bool excel_mgr::setCellText(const CString& s, const CString& text)
{
if(!m_worksheet.m_lpDispatch)
{
return false;
}
excel::Range rang = m_worksheet.GetRange(COleVariant(s), COleVariant(s));
rang.SetValue(COleVariant(text));
rang.ReleaseDispatch();
return true;
}

//测试程序

excel_mgr excel1;
word_mgr word1;

excel1.createExcelApp();
excel1.createWorkBooks();
excel1.createWorkBook();
excel1.showExcel();
for(int row = 2; row < 10; row++)
{
for(int col = 2; col < 10; ++col)
{
CString str;
str.Format("%d-%d", row, col);
excel1.setCellText(row, col, str);
}
}
excel1.saveWorkSheetAs("C:\\lv.xlsx");
excel1.closeWorkSheet();


word1.createWordApp();
word1.createDocuments();
word1.createDocument();
word1.showWord();
word1.saveDocumentAs("C:\\lv.docx");
Sleep(3000);
word1.closeWordApp();

0 0