|
void CMaintestDlg::OnBtnRt()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if(!objApp.CreateDispatch("Excel.Application"))
{
AfxMessageBox("创建Excel服务失败!");
return;
}
COleVariant vResult;
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
objBooks.AttachDispatch(objApp.GetWorkbooks(),true);
objBook.AttachDispatch(objBooks.Open("e:\\x.xls",
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional));
//得到Worksheets
objSheets.AttachDispatch(objBook.GetWorksheets(),true);
//得到sheet1
objSheet.AttachDispatch(objSheets.GetItem(_variant_t("Sheet1")),true);
//得到全部Cells,此时,objSheet是cells的集合
bjRange.AttachDispatch(objSheet.GetCells(),true);
VARIANT vs=objRange.GetItem(_variant_t((long)1),_variant_t((long2));
m_rt1=vs.dblVal;//m_rt1是对话框类的变量,doule型
UpdateData(FALSE);
objRange.ReleaseDispatch();
objSheet.ReleaseDispatch();
objSheets.ReleaseDispatch();
objBook.ReleaseDispatch();
objBooks.ReleaseDispatch();
objApp.ReleaseDispatch();
运行后,m_tr1显示的不是e:\\x.xls文件里sheet1里第二行第二格的那个值。
本来是303.2,结果显示3.75064,再点击下按钮,显示的数又变了。
很奇怪,请高手赐教。 |
|