我看了网上的一些资料
好像读取Excel数据和Access数据查不多
我谢了如下代码:
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pResultRecordset;
try
{
CString DBPathName=_T("G:\\灰色预测测试\\负荷预测历史数据.xls");
CString strWay="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+DBPathName+";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1;""" ;
HRESULT hr=m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection->Open((LPCSTR)strWay,"","",adModeUnknown);
}
catch(_com_error &e)
{
MessageBox(e.Description());
}
CString m_strSQL="select * from [历史数据$]";
try
{
m_pResultRecordset.CreateInstance(__uuidof(Recordset));
m_pResultRecordset->Open(m_strSQL.AllocSysString(),
m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
}
catch(_com_error &e)
{
MessageBox(e.Description());
}
但是在测试的时候出来一个对话框,里面啥内容都没有。
这应该算是没连接成功吧?到底该怎么做呢?
好像读取Excel数据和Access数据查不多
我谢了如下代码:
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pResultRecordset;
try
{
CString DBPathName=_T("G:\\灰色预测测试\\负荷预测历史数据.xls");
CString strWay="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+DBPathName+";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1;""" ;
HRESULT hr=m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection->Open((LPCSTR)strWay,"","",adModeUnknown);
}
catch(_com_error &e)
{
MessageBox(e.Description());
}
CString m_strSQL="select * from [历史数据$]";
try
{
m_pResultRecordset.CreateInstance(__uuidof(Recordset));
m_pResultRecordset->Open(m_strSQL.AllocSysString(),
m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
}
catch(_com_error &e)
{
MessageBox(e.Description());
}
但是在测试的时候出来一个对话框,里面啥内容都没有。
这应该算是没连接成功吧?到底该怎么做呢?
_RecordsetPtr m_pResultRecordset;
CString DBPathName=_T("G:\\灰色预测测试\\ExcelAccess\\ExcelAccessSrc\\Test.xls");
CString strWay="Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq="+BPathName;
HRESULT hr=m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection->Open((LPCSTR)strWay,"","",adModeUnknown);
CString m_strSQL="SELECT * FROM [TestSheet$]";
m_pResultRecordset.CreateInstance(__uuidof(Recordset));
m_pResultRecordset->Open(m_strSQL.AllocSysString(),
m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
//调试时,显示记录集并不为空
if(m_pResultRecordset->adoEOF)
{
TRACE("记录集为空");
return;
}
//调试时,lCount=-1,好像表示记录数不确定
long lCount=m_pResultRecordset->GetRecordCount();try
{
m_pResultRecordset->MoveFirst();
CString strData;
//执行后,strData仍旧为空
strData=m_pResultRecordset->GetCollect("姓名").bstrVal;
}
//并没有捕捉到一点错误
catch(_com_error &e)
{
TRACE(e.Description());
}
顺便把Excel的数据说一下:
姓名 年龄
Test Test
徐景周 27
徐志慧 23
郭徽 28
Test Test
朱小鹏 26
Test Test
还希望各位大侠多多帮忙指点一下。
执行这一句之前,先定义一个:
_variant_t var;
var = _pResultRecordset->GetCollect("姓名");
然后在调试状态下,查看var的类型,我觉得很有可能是你的"姓名"列取出来不是字符串(bstrVal)
确实是VT_BSTR类型,用你们所说的方法也能读出数据
看来是CString类型和BSTR类型不能直接转换
用
_bstr_t bt;
bt=m_pResultRecordset->GetCollect("姓名").bstrVal;
就可以了
不过还有一个问题:
现在我把数据变成2005年1月//编辑框中的是2005-1-1,单元格格式为yyyy"年"m"月";@
1998//编辑框中为1998.2356475895,单元格格式为数值,小数位数为0此时我无论是用
_variant_t va;
va=m_pResultRecordset->GetCollect("2005年1月");
还是
va=m_pResultRecordset->GetCollect("2005-1-1");
调试时的错误都是说:
在指定名称或项目的集合中未能找到指定项目
请问这个该如何解决呢?
GetCollect("FieldName")!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
可能我说的有点不清楚,现在重新申明一下:
Excel中的数据及数据格式如下:姓名 2005年1月 //编辑框中的是2005-1-1,单元格格式为yyyy"年"m"月";@
test 1998 //编辑框中为1998.2356475895,单元格格式为数值,小数位数为0
excel 1997 //编辑框中为1997.8956246525,单元格格式为数值,小数位数为0
.... ....获取列名方法如下:
//正确读取列名“姓名”(1)
_bstr_t bstrName=m_pResultRecordset->Fields->Item[(long)0]->Name;
//不能正确读取,为“F2”(1),我想获得的是为“2005年1月”或“2005-1-1”
_bstr_t bstrName=m_pResultRecordset->Fields->Item[(long)1]->Name;
当然,我也不希望用Excel,我对这种形式的数据库也不熟悉,但是客户给我的历史数据就是采用这种格式,我总不能叫客户改文件格式来适应我吧
至于 chjc2003(至尊拖鞋) 所说的,也有道理,但是我查了一下资料,Excel还是可以当作数据库来用的,它和其他的数据库是不同,所以才可能出现日期型的列名无法读取的问题但是,不知道有没有方法在ADO里把Excel第一行的数据的格式全都转化成“常规”或“文本”型呢?这样不也一样可以读取吗?