VC6.0 MFC 我用colevariant类定义了一个变量result,想用AfxMessagebox()输出,括号里应该怎么写?另外,运行程序时总会打开excel,如何改进才能不用打开excel,而是直接通过对话框输出我想要的数据。新人求指导,程序如下
_Application app;
Workbooks books;
_Workbook book;
Worksheets sheets;
_Worksheet sheet;
Range range;
Font font;
Range cols;
LPDISPATCH lpDisp;
COleVariant vResult;
CString str = "";
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
//创建Excel 2003服务器(启动Excel)
if (!app.CreateDispatch("Excel.Application",NULL))
{
AfxMessageBox("Create Excel service failure!");
return;
}
// 设置为FALSE时,后面的app.Quit();注释要打开
// 否则EXCEL.EXE进程会一直存在,并且每操作一次就会多开一个进程
app.SetVisible(TRUE);
books.AttachDispatch(app.GetWorkbooks(),true);
lpDisp = books.Open("C:\\Test.xls",
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional);
ASSERT(lpDisp);
book.AttachDispatch(lpDisp);
// 得到Worksheets
sheets.AttachDispatch(book.GetWorksheets(),true);
// 得到Worksheet
sheet.AttachDispatch(sheets.GetItem(_variant_t((short)(1))));
// 得到全部Cells
range.AttachDispatch(sheet.GetCells(),true);
// 读取单元格的数据,第4行第1列
range.AttachDispatch(range.GetItem(_variant_t((long)(6)),_variant_t((long)(6))).pdispVal );
vResult =range.GetValue(covOptional);
range.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
app.ReleaseDispatch();
------解决思路----------------------
如果你的vResult 就是VT_BSTR字符串,可以AfxMessagebox(vResult .bstrVal);
或者:
CString strData;
switch(vResult .vt)
{
case VT_BSTR:
strData=V_BSTRT(&vResult ); break;//字符串
case VT_I4:
strData.Format(_T("%d"),V_I4(&vResult ));break;//整型
case VT_R4:
strData.Format(_T("%f"),(double)V_R4(&vResult ));break;//浮点数
//......
//......
}
AfxMessagebox(strData);
------解决思路----------------------
str.Format("%8.0f\n",dbl);
------解决思路----------------------
app.SetVisible(FALSE) //不可见
COleVariant::ChangeType(VT_BSTR) //转换到BSTR字符串