VC 開啟 Excel 檔案後,excel.exe程序無法退出
阿新 • • 發佈:2019-01-04
測試程式碼:
vc6.0
office2000
以下程式碼需要MFC的支援,而且工程中還要包含EXCEL2000的定義檔案:EXCEL9.H,EXCEL9.CPP
_Application ExcelApp;
Workbooks wbsMyBooks;
_Workbook wbMyBook;
Worksheets wssMysheets;
_Worksheet wsMysheet;
Range range;
LPDISPATCH lpDisp;
//建立Excel 2000伺服器(啟動Excel)
COleVariant vResult;
COleVariant covTrue((short)TRUE);
COleVariant covFalse((short)FALSE);
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
CoInitialize(NULL);
if (!ExcelApp.CreateDispatch("Excel.Application",NULL))
{
AfxMessageBox("建立Excel服務失敗!" );
exit(1);
}
ExcelApp.SetVisible(TRUE); //使Excel可見
ExcelApp.SetUserControl(TRUE); //允許其它使用者控制Excel
//開啟c:\\aaa.xls
wbsMyBooks.AttachDispatch(ExcelApp.GetWorkbooks());
lpDisp = wbsMyBooks.Open("C:\\aaa.xls",
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional );
//得到Workbook
wbMyBook.AttachDispatch(lpDisp);
//得到Worksheets
wssMysheets.AttachDispatch(wbMyBook.GetWorksheets());
//得到當前活躍sheet
//如果有單元格正處於編輯狀態中,此操作不能返回,會一直等待
lpDisp=wbMyBook.GetActiveSheet();
wsMysheet.AttachDispatch(lpDisp);
//*****
//讀取已經使用區域的資訊,包括已經使用的行數、列數、起始行、起始列
Range usedRange;
usedRange.AttachDispatch(wsMysheet.GetUsedRange());
range.AttachDispatch(usedRange.GetRows());
long iRowNum=range.GetCount(); //已經使用的行數
range.AttachDispatch(usedRange.GetColumns());
long iColNum=range.GetCount(); //已經使用的列數
long iStartRow=usedRange.GetRow(); //已使用區域的起始行,從1開始
long iStartCol=usedRange.GetColumn(); //已使用區域的起始列,從1開始
for (long i=iStartRow; i<iRowNum+1; i++)
{
for (long j=iStartCol; j<iColNum+1; j++)
{
range.AttachDispatch(wsMysheet.GetCells());
range.AttachDispatch(range.GetItem (COleVariant((long)i),COleVariant((long)j)).pdispVal );
vResult =range.GetValue();
CString str;
if(vResult.vt == VT_BSTR) //字串
{
str=vResult.bstrVal;
}
else if (vResult.vt==VT_R8) //8位元組的數字
{
str.Format("%f",vResult.dblVal);
}
else if(vResult.vt==VT_DATE) //時間格式
{
SYSTEMTIME st;
VariantTimeToSystemTime(vResult.date, &st);
}
else if(vResult.vt==VT_EMPTY) //單元格空的
{
str="";
}
if (str.Find("44") != -1)
{
AfxMessageBox(str);
}
}
}
/*****
//讀取第一個單元格的值
range.AttachDispatch(wsMysheet.GetCells());
range.AttachDispatch(range.GetItem (COleVariant((long)1),COleVariant((long)1)).pdispVal );
vResult =range.GetValue();
CString str;
if(vResult.vt == VT_BSTR) //字串
{
str=vResult.bstrVal;
}
else if (vResult.vt==VT_R8) //8位元組的數字
{
str.Format("%f",vResult.dblVal);
}
else if(vResult.vt==VT_DATE) //時間格式
{
SYSTEMTIME st;
VariantTimeToSystemTime(vResult.date, &st);
}
else if(vResult.vt==VT_EMPTY) //單元格空的
{
str="";
}
AfxMessageBox(str);
*/
/*
wbsMyBooks.AttachDispatch(ExcelApp.GetWorkbooks(),true);
wbMyBook.AttachDispatch(wbsMyBooks.Add(_variant_t("c:\\aaa.xls")));
//MessageBox("gg1");
//得到Worksheets
wssMysheets.AttachDispatch(wbMyBook.GetWorksheets(),true);
//得到sheet1
wsMysheet.AttachDispatch(wssMysheets.GetItem(_variant_t("bend")),true);
//得到全部Cells,此時,rgMyRge是cells的集合
range.AttachDispatch(wsMysheet.GetCells(),true);
_variant_t vStr;
BSTR bStr;
vStr=range.GetItem(_variant_t((long)3),_variant_t((long)3));
bStr = (_bstr_t)vStr;
//CString outstr=(LPCTSTR)(LPSTR)bStr;
CString out2="";
for(int i=0;i<sizeof(bStr);i++)
{
out2+=(CString)(char)*(bStr+i);
}
AfxMessageBox(out2);
*/
//關閉execl
ExcelApp.SetUserControl(FALSE);
wbMyBook.Close (covOptional,covOptional,covOptional);
wbsMyBooks.Close();
ExcelApp.Quit();
//釋放物件
range.ReleaseDispatch();
wsMysheet.ReleaseDispatch();
wssMysheets.ReleaseDispatch();
wbMyBook.ReleaseDispatch();
wbsMyBooks.ReleaseDispatch();
ExcelApp.ReleaseDispatch();
}
Quit之前
SetUserControl(FALSE);