AttachDispatch
1.函数说明
使用此函数需加入EXCEL的组件库,且引入CRange.h,并获得work和sheet后方可使用。
2.函数原型
CRange::void AttachDispatch(LPDISPATCH lpDispatch,BOOL bAutorelease =1)
lpDispatch:锁定的目标指针。
bAutorelease:是否自动释放。
3.函数使用
CApplication app;
CWorkbooks books;
CWorkbook book;
CWorksheets sheets;
CWorksheet sheet;
CRange range;
CRange iCell;
LPDISPATCH lpDisp;
COleVariant vResult;
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
if (CoInitialize(NULL)!=0)
{
AfxMessageBox(_T("初始化COM支持库失败!"));
return 0;
}
if(!app.CreateDispatch(_T("Excel.Application")))
{
AfxMessageBox(_T("无法启动Excel服务器!"));
return 0;
}
books.AttachDispatch(app.get_Workbooks());
lpDisp = books.Open(_T("C:\\test.xls"),covOptional, covOptional, covOptional , covOptional, covOptional,covOptional, covOptional, covOptional, covOptional, covOptional,covOptional, covOptional, covOptional,covOptional);
//得到Workbook
book.AttachDispatch(lpDisp);
//得到Worksheets
sheets.AttachDispatch(book.get_Worksheets());
//得到当前活跃sheet
//如果有单元格正处于编辑状态中,此操作不能返回,会一直等待
lpDisp=book.get_ActiveSheet();
sheet.AttachDispatch(lpDisp);
//读取第一个单元格的值
range.AttachDispatch(sheet.get_Cells());
range.AttachDispatch(range.get_Item (COleVariant((long)1),COleVariant((long)1)).pdispVal );
/*COleVariant*/ vResult =range.get_Value2();
CString str;
if(vResult.vt == VT_BSTR) //字符串
{
str=vResult.bstrVal;
}
else if (vResult.vt==VT_R8) //8字节的数字
{
str.Format(_T("%f"),vResult.dblVal);
}
/*else if(vResult.vt==VT_DATE) /格式
{
SYSTEMTIME st;
VariantTimeToSystemTime(&vResult.date, &st);
}
else if(vResult.vt==VT_EMPTY) //单元格空的
{
str="";
}*/
books.Close();
app.Quit(); // 退出
//释放对象
range.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
app.ReleaseDispatch();
CWorkbooks books;
CWorkbook book;
CWorksheets sheets;
CWorksheet sheet;
CRange range;
CRange iCell;
LPDISPATCH lpDisp;
COleVariant vResult;
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
if (CoInitialize(NULL)!=0)
{
AfxMessageBox(_T("初始化COM支持库失败!"));
return 0;
}
if(!app.CreateDispatch(_T("Excel.Application")))
{
AfxMessageBox(_T("无法启动Excel服务器!"));
return 0;
}
books.AttachDispatch(app.get_Workbooks());
lpDisp = books.Open(_T("C:\\test.xls"),covOptional, covOptional, covOptional , covOptional, covOptional,covOptional, covOptional, covOptional, covOptional, covOptional,covOptional, covOptional, covOptional,covOptional);
//得到Workbook
book.AttachDispatch(lpDisp);
//得到Worksheets
sheets.AttachDispatch(book.get_Worksheets());
//得到当前活跃sheet
//如果有单元格正处于编辑状态中,此操作不能返回,会一直等待
lpDisp=book.get_ActiveSheet();
sheet.AttachDispatch(lpDisp);
//读取第一个单元格的值
range.AttachDispatch(sheet.get_Cells());
range.AttachDispatch(range.get_Item (COleVariant((long)1),COleVariant((long)1)).pdispVal );
/*COleVariant*/ vResult =range.get_Value2();
CString str;
if(vResult.vt == VT_BSTR) //字符串
{
str=vResult.bstrVal;
}
else if (vResult.vt==VT_R8) //8字节的数字
{
str.Format(_T("%f"),vResult.dblVal);
}
/*else if(vResult.vt==VT_DATE) /格式
{
SYSTEMTIME st;
VariantTimeToSystemTime(&vResult.date, &st);
}
else if(vResult.vt==VT_EMPTY) //单元格空的
{
str="";
}*/
books.Close();
app.Quit(); // 退出
//释放对象
range.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
app.ReleaseDispatch();
4.函数返回
无返回值