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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值