获取所有工作表名,并将其显示到一个CListCtrl控件中
BOOL GetExcelSheetList(CString sExcelFile, CListCtrl &listMx)
{
_Worksheet m_wsExcelSingle;
Worksheets m_wsExcels;
_Workbook m_wbExcelSingle;
Workbooks m_wbExcels;
_Application m_appExcel;
if (!m_appExcel.CreateDispatch("Excel.Application",NULL))
{
MessageBox(NULL,"创建Excel服务失败!","提示",MB_OK|MB_ICONWARNING|MB_TOPMOST);
return FALSE;
}
m_appExcel.SetVisible(false);
m_wbExcels.AttachDispatch(m_appExcel.GetWorkbooks(),true);
m_wbExcelSingle.AttachDispatch(m_wbExcels.Add(_variant_t(sExcelFile)));
m_wsExcels.AttachDispatch(m_wbExcelSingle.GetWorksheets(),true);
for(int j=1;j<=m_wsExcels.GetCount();j++)
{
m_wsExcelSingle.AttachDispatch( m_wsExcels.GetItem( COleVariant( long( j ) ) ), TRUE );
CString sVal = m_wsExcelSingle.GetName();
int nItem = listMx.GetItemCount();
listMx.InsertItem(nItem,sVal);
}
//释放对象
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
m_wbExcelSingle.Close( covOptional, COleVariant( sExcelFile ), covOptional );
m_wbExcels.Close();
m_wsExcelSingle.ReleaseDispatch();
m_wsExcels.ReleaseDispatch();
m_wbExcelSingle.ReleaseDispatch();
m_wbExcels.ReleaseDispatch();
m_appExcel.ReleaseDispatch();
m_appExcel.Quit();
return TRUE;
}