请教ADO读取excel2007所有内容

阴凉 2013-08-09 02:42:06
小弟初来乍到,最近学习excel方面的知识,这几天用C++使用ADO接口读取excel2007的内容,弄了半天只会读取一列的内容,但如果一个excel有列的话,我就得使用GetCollect()函数从F1读取到F50,这个不科学啊?再说我又不知道具体有多少列,想知道这个其中有什么逻辑,或者用什么函数能获得列数。在此虚心请教。代码如下,用的VS2010的win32控制台程序,多字节字符集,读excel2007:

#include "stdafx.h"
#include <atlstr.h>
#include <iostream>
using namespace std;



int _tmain(int argc, _TCHAR* argv[])
{
::CoInitialize(NULL);
_ConnectionPtr m_pConnection;
HRESULT hr=m_pConnection.CreateInstance(__uuidof(Connection));
if(!SUCCEEDED(hr))
{
cout<<"不成功"<<endl;
::CoUninitialize();
cin.get();
return 0;
}


try
{
m_pConnection->Open("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=database.xlsx;Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\"","","",adModeUnknown);
}
catch(_com_error e) //捕捉异常
{
CString strError;
strError.Format( "连接数据库发生异常! \r \n错误信息:%s", e.ErrorMessage( ) );
cout<<strError; //显示错误信息
::CoUninitialize();
cin.get();
return 0;
}


_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance(__uuidof(Recordset));
CString strSQL=_T("select* from [Sheet1$]");
try
{
m_pRecordset->Open(_variant_t(strSQL),m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
}
catch(_com_error e) //捕捉异常
{
CString strError;
strError.Format( "连接数据库发生异常! \r \n错误信息:%s", e.ErrorMessage( ) );
cout<<strError; //显示错误信息
::CoUninitialize();
cin.get();
return 0;
}



while(!m_pRecordset->adoEOF)
{
//m_pRecordset->MoveFirst();
_variant_t temp =m_pRecordset->GetCollect("F1");
if(temp.vt != VT_NULL)
{
CString strl=(CString)temp;
_variant_t temp_t =m_pRecordset->GetCollect("F2");
if(temp_t.vt != VT_NULL)
{
cout<<strl<<" ";
}
else
{
cout<<strl<<endl;
}
}
_variant_t temp_t =m_pRecordset->GetCollect("F2");
if(temp_t.vt != VT_NULL)
{
CString strl=(CString)temp_t;
_variant_t temp_th =m_pRecordset->GetCollect("F3");
if(temp_th.vt != VT_NULL)
{
cout<<strl<<" ";
}
else
{
cout<<strl<<endl;
}
}
_variant_t temp_th =m_pRecordset->GetCollect("F3");
if(temp_th.vt != VT_NULL)
{
CString strl=(CString)temp_th;
cout<<strl<<endl;
}
m_pRecordset->MoveNext();

}

m_pConnection->Close();
m_pConnection= NULL;
::CoUninitialize();
cin.get();
return 0;
}
...全文
118 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
cjzzmdn 2013-08-09
  • 打赏
  • 举报
回复
还一个不一样的地方,我直接打开表了 参数5:adcmdtable
m_pRecordset->Open("[dsds$]",m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
			adOpenKeyset,//adOpenDynamic,adOpenKeyset
			adLockReadOnly,
			adCmdTable);
cjzzmdn 2013-08-09
  • 打赏
  • 举报
回复
这样就读取了字段名,
FieldsPtr fldLoop = NULL; 
		_variant_t Index;
		Index.vt = VT_I2;
		vector<pair<DataTypeEnum,string> > m_tableinfo;
	
			fldLoop = m_pRecordset->GetFields();
			int ncnt = (int)fldLoop->GetCount();
			for (int i = 0; i < ncnt; i++)
			{
				Index.iVal=i; 
				//这里一定要一个variant?
				string fldname = fldLoop->GetItem(Index)->GetName();
				DataTypeEnum fldtype = fldLoop->GetItem(Index)->Type;
				m_tableinfo.push_back(make_pair(fldtype,fldname));
			}
我的连接串是这样的
adoinfo = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=\"";
	adoinfo += Types[m_sExtname] + ";HDR=YES;\";Data Source=" ;
	adoinfo += m_sFileName + ";";
注意hdr=yes 没有这个字段名就是f1,f2这样的
max_min_ 2013-08-09
  • 打赏
  • 举报
回复
之前也有人发过贴了,貌似这边的没多少人会ADO的东西 你换个模块问问吧!帅哥!

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧