CMyExcel::CMyExcel()
{
strFilePath = _T("");
}
CMyExcel::~CMyExcel()
{
COleVariant covFalse((short)FALSE);
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
MyRange.ReleaseDispatch();
MySheet.ReleaseDispatch();
MySheets.ReleaseDispatch();
MyBook.Close(covFalse,_variant_t(strFilePath),covOptional);
MyBook.ReleaseDispatch();
MyBooks.Close();
MyBooks.ReleaseDispatch();
MyApp.Quit();
MyApp.ReleaseDispatch();
CoUninitialize();
}
//打开Excel,但不会显示
BOOL CMyExcel::Open()
{
LPDISPATCH lpDisp = NULL;
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
CoInitialize(NULL);
if(!MyApp.CreateDispatch("Excel.Application",NULL))
{
AfxMessageBox(_T("EXCEL 初始化时出错!"),MB_OK | MB_ICONERROR);
return false;
}
MyBooks.AttachDispatch(MyApp.GetWorkbooks(),TRUE);
lpDisp = MyBooks.Add(covOptional);
MyBook.AttachDispatch(lpDisp,TRUE);
lpDisp = MyBook.GetWorksheets();
MySheets.AttachDispatch(lpDisp,TRUE);
return TRUE;
}
BOOL CMyExcel::Open(CString strFile)
{
LPDISPATCH lpDisp = NULL;
CoInitialize(NULL);
if(!MyApp.CreateDispatch("Excel.Application",NULL))
{
AfxMessageBox(_T("EXCEL 初始化时出错!"),MB_OK | MB_ICONERROR);
return false;
}
MyBooks.AttachDispatch(MyApp.GetWorkbooks(),TRUE);
lpDisp = MyBooks.Open(strFile,vtMissing,vtMissing,vtMissing,
vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing);
MyBook.AttachDispatch(lpDisp,TRUE);
lpDisp = MyBook.GetWorksheets();
MySheets.AttachDispatch(lpDisp,TRUE);
strFilePath = strFile;
return TRUE;
}
//打开Sheet
BOOL CMyExcel::OpenSheet(CString strSheet)
{
LPDISPATCH lpDisp = NULL;
long length;
length = MySheets.GetCount();
for(long i = 1;i <= length;i++)
{
lpDisp = MySheets.GetItem(_variant_t((long)i));
MySheet.AttachDispatch(lpDisp,TRUE);
if(MySheet.GetName() == strSheet)
{
lpDisp = MySheet.GetCells();
MyRange.AttachDispatch(lpDisp,TRUE);
return TRUE;
}
}
return FALSE;
}
//获得当前Sheet使用的行数
long CMyExcel::GetRows()
{
long lRowsNum;
//Range range = MyRange.GetEntireRow();
Range range,usedRange;
usedRange.AttachDispatch(MySheet.GetUsedRange());
range.AttachDispatch(usedRange.GetRows());
lRowsNum = range.GetCount();
//lRowsNum = range.GetCount();
range.ReleaseDispatch();
usedRange.ReleaseDispatch();
return lRowsNum;
}
//获得当前Sheet使用的列数
long CMyExcel::GetCols()
{
long lColNum;
Range range,usedRange;
usedRange.AttachDispatch(MySheet.GetUsedRange());
range.AttachDispatch(usedRange.GetColumns());
lColNum = range.GetCount();
range.ReleaseDispatch();
usedRange.ReleaseDispatch();
return lColNum;
}
//获得当前Sheet指定单元格内容
CString CMyExcel::GetItemText(long Row,long Col)
{
CString strValue = _T("");
Range range;
range.AttachDispatch(MyRange.GetItem(_variant_t(Row),_variant_t(Col)).pdispVal,TRUE);
_variant_t vtVal = range.GetValue2();
if(vtVal.vt == VT_EMPTY)
{
range.ReleaseDispatch();
return strValue;
}
vtVal.ChangeType(VT_BSTR);
strValue = vtVal.bstrVal;
range.ReleaseDispatch();
return strValue;
}
//设置当前Sheet指定单元格内容
BOOL CMyExcel::SetItemText(long Row,long Col,CString strText)
{
MyRange.SetItem(_variant_t(Row),_variant_t(Col),_variant_t(strText));
return TRUE;
}
//保存工作表
void CMyExcel::Save()
{
MyBook.Save();
}