描述:
rt
但是Excel里内容是不确定的,就是说不知道它里面有几行几列。
我的代码如下
void CTestExcel2Dlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
CoInitialize(NULL);
_ConnectionPtr pCon_ex(__uuidof(Connection));
CString ConnectionString;
CString m_strAppPath=_T("");
CString excel_path=_T("");
CString c />
CString FilePathName;
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"XML Files(*.xls)|*.xls||",NULL,0);///TRUE为OPEN对话框,FALSE为SAVE AS对话框
//dlg.m_ofn.lpstrInitialDir=_T("d:\\"); //这里就设置了对话框的默认目录d盘
if(dlg.DoModal()==IDOK)
FilePathName=dlg.GetPathName();
else
return;
//程序所在目录路径
m_strAppPath = FilePathName.Left( FilePathName.ReverseFind( '\\' ) );
//打开excel
/*"HDR=Yes;" 表示工作表的第一行是表头,没有数据。 "HDR=No;"与之相反。
"IMEX=1;"告诉驱动程序始终将"intermixed"数据类型(numbers, dates, strings等等)作为文本型读取。
注意:该选项可能引起Excel工作表写权限的修改。如果想写入数据,创建新表等必须使其为0*/
ConnectionString = _T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=");
ConnectionString += FilePathName; //excel file name
ConnectionString += _T(";Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\"");
BSTR resultsString = ConnectionString.AllocSysString();
//pCon_ex.CreateInstance(__uuidof(Connection));
resultsString = ConnectionString.AllocSysString();
pCon_ex->Open(resultsString,"","",adConnectUnspecified);
CString strSQL=_T("");
_RecordsetPtr pRst(__uuidof(Recordset)); //数据集
//strSQL="select * from [sheet1$A1:C2] ";
strSQL="select * from [sheet1$]";//[道路$]为sheet的名称
pRst=pCon_ex->Execute((_bstr_t)strSQL,NULL,adCmdText); //指定的城市
long temp1 =pRst->GetMaxRecords();
pRst->MoveFirst(); //只读取第一行
while(!pRst->adoEOF)
{
int i=0;
_variant_t t = _variant_t(long(i));
CString result;
result = (LPCSTR)_bstr_t(pRst->GetCollect(t));//以列序号的方式来读取字段内容 0based
MessageBox(result);
; }
_variant_t temp=pRst->GetFilter();
//long num=pRst->GetPageSize();
long num=2;
_variant_t count=pRst->GetRows(num,vtMissing,vtMissing);
//str=(count);
//str=(char *)_bstr_t(count);
//MessageBox(str);
//count.n
/*i++;*/
//result = (LPCSTR)_bstr_t(pRecordset->GetCollect("人口"));//以字段名的方式来读字段内容
pRst->MoveNext();
}
pRst->Close();
pCon_ex->Close();
pRst.Release();
pCon_ex.Release();
CoUninitialize();
}
求教高手