excel 类的使用
try
{
CString strFileName = _T("d:\\test.xls");
_Application ExcelApp;
Workbooks wbsBooks; //练习簿
_Workbook wbBook;
Worksheets wsssheets; //工作表
_Worksheet wsssheet;
Range rgRge;
_variant_t vExcelFilePath = strFileName;
long lSheetsCount = 0;
if (!ExcelApp.CreateDispatch("Excel.Application", NULL))
{
printf("Create Excel service failed!\n");
return false;
}
wbsBooks.AttachDispatch(ExcelApp.GetWorkbooks(), true);
wbBook.AttachDispatch(wbsBooks.Add(vExcelFilePath));
//得到Worksheets
wsssheets.AttachDispatch(wbBook.GetWorksheets(),true);
//得到Sheet的总数
lSheetsCount = wsssheets.GetCount();
//这里省略对文档的编辑处理......
wbBook.SetSaved(TRUE);//关键是这句
wbBook.Close(COleVariant("false"), vExcelFilePath, COleVariant("false"));
ExcelApp.Quit();
//释放对象
wbBook.ReleaseDispatch();
wsssheets.ReleaseDispatch();
wbsBooks.ReleaseDispatch();
ExcelApp.ReleaseDispatch();
}
catch(_com_error &e)
{
printf("Error: %s\n", (LPCSTR)e.Description());
return false;
}
catch (...)
{
printf("Unknown Error\n");
return false;
}
return true;
从excel 中获取的日期字串转化成 CString 40969.000000 ==== 2012/3/1
varVal = m_objRange.GetValue2();
// 日期得到的是一串数字
if (varVal.bstrVal == NULL)
{
COleDateTime date(varVal.dblVal);
SYSTEMTIME systime;
VariantTimeToSystemTime(date, &systime);
CTime cTime(systime);
strText = cTime.Format(_T("%Y/%m/%d"));
}
else
strText = varVal.bstrVal;