MFC操作EXCEL封裝類

MFC操作EXCEL簡單的功能封裝,方便工作使用。

#pragma once
#include "excel9.h"
enum eAlignmentStatus
{
Alignment_Left = -4131,
Alignment_Middle = -4108,
Alignment_Right = -4152,
Alignment_Top = -4160,
Alignment_Bottom = -4107,
};
class CExcelOperate
{
public:
CExcelOperate();
~CExcelOperate();


public:
BOOL OpenExcelTemplate(CString strFilePathAndName);
void CloseExcelTemplate();


void SaveExcel(CString strFilePathAndName);
void PrintPreview();


void SetItemValue(int nRowIndex,int nColIndex,int nItemValue);
void SetItemValue(int nRowIndex,int nColIndex,long lItemValue);
void SetItemValue(int nRowIndex,int nColIndex,CString strItemValue);


void MergeItem(CString strStartItemName,CString strEndItemName);
void SetItemHeight(int nRowIndex,int nHeight);
void SetColumnWidth(CString strColIndex,int nWidth);


void SetItemTextSize(CString strStartItemName,CString strEndItemName,int nSize);
void SetItemTextBlod(CString strStartItemName,CString strEndItemName,BOOL bBlodStatus);




void SetHorizontalAlignment(CString strStartItemName,CString strEndItemName,eAlignmentStatus eStatus);
void SetVerticalAlignment(CString strStartItemName,CString strEndItemName,eAlignmentStatus eStatus); 


void SetBorderAround(CString strStartItemName,CString strEndItemName);
protected:
_Application m_hApplication;
Workbooks m_hWorkbooks;
_Workbook m_hWorkBook;
Worksheets m_hWorkSheets;
_Worksheet m_hWorkSheet;


Range m_hRange;


BOOL m_bOpenFileStatus;
};


#include "stdafx.h"
#include "ExcelOperate.h"
CExcelOperate::CExcelOperate()
{
m_bOpenFileStatus = FALSE;
}
CExcelOperate::~CExcelOperate()
{
CloseExcelTemplate();
}
void CExcelOperate::CloseExcelTemplate()
{
if (TRUE == m_bOpenFileStatus)
{
m_hWorkbooks.Close();


m_hRange.ReleaseDispatch();
m_hWorkSheet.ReleaseDispatch();
m_hWorkSheets.ReleaseDispatch();
m_hWorkBook.ReleaseDispatch();
m_hWorkbooks.ReleaseDispatch();


m_hApplication.ReleaseDispatch();
m_hApplication.Quit();


m_bOpenFileStatus = FALSE;

}
BOOL CExcelOperate::OpenExcelTemplate(CString strFilePathAndName)
{
if (TRUE == m_bOpenFileStatus)
{
CloseExcelTemplate();
}


if (m_hApplication.CreateDispatch("Excel.Application",NULL) == FALSE)
{
AfxMessageBox("創建Excel服務失敗");
return m_bOpenFileStatus;
}


m_hApplication.SetVisible(FALSE);


m_hWorkbooks.AttachDispatch(m_hApplication.GetWorkbooks(),TRUE);


COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
LPDISPATCH lpDisp;
lpDisp = m_hWorkbooks.Open(strFilePathAndName,covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,covOptional);
m_hWorkBook.AttachDispatch(lpDisp);


m_hWorkSheets.AttachDispatch(m_hWorkBook.GetWorksheets(),true);


m_hWorkSheet.AttachDispatch(m_hWorkSheets.GetItem(_variant_t("sheet1")),true);


CString str1;
m_hWorkSheet.AttachDispatch(m_hWorkSheets.GetItem(_variant_t("sheet1")),true);
str1 = "第1頁";
m_hWorkSheet.SetName(str1);


m_hWorkSheet.AttachDispatch(m_hWorkSheets.GetItem(_variant_t("第1頁")),true);    // 一個投影 


m_hRange.AttachDispatch(m_hWorkSheet.GetCells(),true);


m_bOpenFileStatus = TRUE;
return m_bOpenFileStatus;
}
void CExcelOperate::SaveExcel(CString strFilePathAndName)
{
if (m_bOpenFileStatus == TRUE)
{
m_hWorkSheet.SaveAs(strFilePathAndName,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing);
}
}
void CExcelOperate::PrintPreview()
{
if (TRUE == m_bOpenFileStatus)
{
m_hWorkBook.SetSaved(TRUE);
m_hApplication.SetVisible(true);
m_hWorkBook.PrintPreview(_variant_t(true));
}
}
void CExcelOperate::SetItemValue(int nRowIndex, int nColIndex, CString strItemValue)
{
m_hRange.SetItem(_variant_t((long)nRowIndex),_variant_t((long)nColIndex),_variant_t(strItemValue));
}
void CExcelOperate::SetItemValue(int nRowIndex,int nColIndex,int nItemValue)
{
m_hRange.SetItem(_variant_t((long)nRowIndex),_variant_t((long)nColIndex),_variant_t(nItemValue));
}
void CExcelOperate::SetItemValue(int nRowIndex, int nColIndex, long lItemValue)
{
m_hRange.SetItem(_variant_t((long)nRowIndex),_variant_t((long)nColIndex),_variant_t(lItemValue));
}
void CExcelOperate::MergeItem(CString strStartItemName, CString strEndItemName)
{
Range hRange;
hRange.AttachDispatch(m_hWorkSheet.GetRange(_variant_t(strStartItemName),_variant_t(strEndItemName)),TRUE);
hRange.Merge(_variant_t((long)0)); 
hRange.ReleaseDispatch();
}
void CExcelOperate::SetItemHeight(int nRowIndex,int nHeight)
{
Range hRange;
CString strMsgInfo;
strMsgInfo.Format("A%d",nRowIndex);
hRange.AttachDispatch(m_hWorkSheet.GetRange(_variant_t(strMsgInfo),_variant_t(strMsgInfo)),TRUE);
hRange.SetRowHeight(COleVariant(short(nHeight)));
hRange.ReleaseDispatch();
hRange.AttachDispatch(m_hWorkSheet.GetCells(),true);
}
void CExcelOperate::SetColumnWidth(CString strColIndex, int nWidth)
{
strColIndex += "0";
Range hRange;
hRange.AttachDispatch(m_hWorkSheet.GetRange(_variant_t(strColIndex),_variant_t(strColIndex)),TRUE);
hRange.SetColumnWidth(COleVariant(short(nWidth)));
hRange.ReleaseDispatch();
hRange.AttachDispatch(m_hWorkSheet.GetCells(),true);
}
void CExcelOperate::SetItemTextSize(CString strStartItemName,CString strEndItemName,int nSize)
{
Range hRange;
hRange.AttachDispatch(m_hWorkSheet.GetRange(_variant_t(strStartItemName),_variant_t(strEndItemName)),TRUE);
CFontXLS hFontXLS = hRange.GetFont();
hFontXLS.put_Size(COleVariant(short(nSize)));
hRange.ReleaseDispatch();
}
void CExcelOperate::SetItemTextBlod(CString strStartItemName, CString strEndItemName, BOOL bBlodStatus)
{
Range hRange;
hRange.AttachDispatch(m_hWorkSheet.GetRange(_variant_t(strStartItemName),_variant_t(strEndItemName)),TRUE);
CFontXLS hFontXLS = hRange.GetFont();
hFontXLS.put_Bold(COleVariant((short)TRUE));
hRange.ReleaseDispatch();
}
void CExcelOperate::SetVerticalAlignment(CString strStartItemName, CString strEndItemName, eAlignmentStatus eStatus)
{
Range hRange;
hRange.AttachDispatch(m_hWorkSheet.GetRange(_variant_t(strStartItemName),_variant_t(strEndItemName)),TRUE);
hRange.SetVerticalAlignment(_variant_t((long)eStatus));
hRange.ReleaseDispatch();
}
void CExcelOperate::SetHorizontalAlignment(CString strStartItemName, CString strEndItemName, eAlignmentStatus eStatus)
{
Range hRange;
hRange.AttachDispatch(m_hWorkSheet.GetRange(_variant_t(strStartItemName),_variant_t(strEndItemName)),TRUE);
hRange.SetHorizontalAlignment(_variant_t((long)eStatus));
hRange.ReleaseDispatch();
}
void CExcelOperate::SetBorderAround(CString strStartItemName, CString strEndItemName)
{
Range hRange;
hRange.AttachDispatch(m_hWorkSheet.GetRange(_variant_t(strStartItemName),_variant_t(strEndItemName)),TRUE);
hRange.BorderAround(_variant_t((long)1),_variant_t((long)2),_variant_t((long)-4105),vtMissing);
hRange.ReleaseDispatch();
}

源代碼下載地址:

http://download.csdn.net/detail/zhenxiaohui/6715141

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章