//打开Excel
CoInitialize(NULL);
ConnectionString = _T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=");
ConnectionString += strExcelName; //excel file name
ConnectionString += _T(";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=0\"");
BSTR resultsString = ConnectionString.AllocSysString();
pCon_ex.CreateInstance(__uuidof(Connection));
//resultsString = ConnectionString.AllocSysString();
HRESULT _hr = pCon_ex->Open(resultsString,"","",adModeUnknown);
if(FAILED(_hr))
{
AfxMessageBox(_T("打开excel失败"));
return FAIL;
}
//读数据
strSQL.Format(_T("select * from [sheet1$] where 出勤 is not null"));
pRst=pCon_ex->Execute((_bstr_t)strSQL,NULL,adCmdText);
sql.Format(_T("select count(*) as geshu from [sheet1$] where 出勤 is not null "));
Rs1=pCon_ex->Execute((_bstr_t)sql,NULL,adCmdText);
_variant_t vCount=Rs1->GetCollect("geshu");
LONG nNum=vCount.lVal; //符合条件的记录个数
if (nNum < 1)
{//未搜索到有效数据
return FAIL;
}
代码如上,我试着读Excel文件(总共有200多行数据,第一行是title)。然后我发现,当2-9行中出勤列有数据的时候,nNum的值正确;但是当2-9行中出勤列都没值的话(第10行出勤有值),得到的nNum就是0了。
感觉就像是,只有当2-9行有数据的时候,才能读出出勤不为空的行(哪怕只是第9行和最后一行有数据,也能正确读出2行);但只要2-9行出勤都是空的话,无论后边有多少条数据,最终nNum的值都是0。
希望高手指点,谢谢。
------解决方案--------------------------------------------------------
IMEX=1