如何用ADO连接EXCEL数据?

basabin 2003-06-16 04:52:04
哪位大侠提供例子,3x。
...全文
109 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
newlili 2003-07-09
  • 打赏
  • 举报
回复
win32 console project.file readexcel.cpp
#include <stdio.h>
#include <windows.h>
#include <comdef.h>
#import "c:\program files\common files\system\ado\msado20.tlb" no_namespace rename ("EOF", "adoEOF")
HRESULT hr;
int main()
{
hr=CoInitialize(NULL);
_ConnectionPtr m_pConnection;
_CommandPtr m_commandptr;
_RecordsetPtr m_pUserSet;
try
{
//使用odbc的连接字符串
// _bstr_t conn("DRIVER={Microsoft Excel Driver (*.xls)};DriverId=790;DBQ=E:\\temp.xls;DefaultDir=e:\\;");
//使用ole db的连接字符串
_bstr_t conn("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=e:\\temp.xls;Extended Properties=""Excel 8.0;""" );
m_pConnection.CreateInstance (__uuidof(Connection));
m_pConnection->Open (conn,"","",-1);
_RecordsetPtr pRstSchema = NULL;
pRstSchema = m_pConnection->OpenSchema(adSchemaTables);
while(!(pRstSchema->adoEOF))
{
_bstr_t table_name = pRstSchema->Fields->GetItem("TABLE_NAME")->Value;
printf("Table Name: %s\n",(LPCSTR) table_name);
_bstr_t table_type = pRstSchema->Fields->GetItem("TABLE_TYPE")->Value;
printf("Table type: %s\n\n",(LPCSTR) table_type);
pRstSchema->MoveNext();
}

_bstr_t sqltext="SELECT * from [sheet1$] ";
m_commandptr.CreateInstance (__uuidof(Command));
m_pUserSet.CreateInstance (__uuidof(Recordset));
m_commandptr->ActiveConnection =m_pConnection ;
m_commandptr->CommandText =sqltext;
m_commandptr->CommandType =adCmdText;
m_pUserSet=m_commandptr->Execute (NULL,NULL,adCmdUnknown);
_variant_t filed[12];
_bstr_t filedname[12];
bool befirstrow=true;
while (!m_pUserSet->adoEOF )
{
long kk=m_pUserSet->GetFields ()->GetCount ();
for (long uu=0;uu<kk;uu++)
{
filedname[uu]=m_pUserSet->GetFields ()->GetItem ((_variant_t)uu)->GetName ();
filed[uu]=m_pUserSet->GetCollect (filedname[uu]);
printf("%s\t ",(char*)(_bstr_t)(filed[uu]));
}
printf("\r\n");
m_pUserSet->MoveNext ();
}
}
catch(_com_error * e)
{
MessageBox(NULL,"",e->ErrorMessage(),MB_OK);
}
catch(...)
{
long errorcount=m_pConnection->GetErrors ()->GetCount ();
char add[255];
strcpy(add,"");
for (short i=0;i<errorcount;i++)
{
strcat(add,m_pConnection->GetErrors ()->GetItem (_variant_t((short)i))->GetDescription ());
}
printf(add);
}
CoUninitialize();
return 0;
}
lihuihit 2003-06-27
  • 打赏
  • 举报
回复
高兴

终于找到
我需要的答案了
yjw1018(飘叶寻缘)
loucai 2003-06-23
  • 打赏
  • 举报
回复
给个连接字符串:
"Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Extended Properties='Excel 8.0;HDR=Yes';Data Source="+filename
yjw1018 2003-06-20
  • 打赏
  • 举报
回复
就像在Delphi中用ADO或BDE存取ACCESS、SQL数据库一样。
方法:在delphi中加入adoconnection控件,双击后选择jet 4.0 ole db,在连接页中
选择数据库的名字(.xls),再双击全部(all)页中的Extended Properties,在value中
填入Excel 8.0,OK!
在控件ADODataSet1的CommandText属性中选择了select * from Sheet1$后,别忘了在
Sheet1$的两边加上中括号[],否则出现“FROM子句语法错误”。或者用ADOQuery1控件:在
它的SQL属性中写入:select * from [Sheet1$]。
basabin 2003-06-19
  • 打赏
  • 举报
回复
可以连的,但是就是不能添加和删除记录,不知为什么?
mtdata 2003-06-19
  • 打赏
  • 举报
回复
在excel中通过宏编辑器来做
kevlee 2003-06-18
  • 打赏
  • 举报
回复
将EXCEL数据,导入SQL数据库再用ADO连接SQL数据库.
笨魚 2003-06-18
  • 打赏
  • 举报
回复
好象 ADO 不是用来做这个的吧!

2,209

社区成员

发帖
与我相关
我的任务
社区描述
其他数据库开发 其他数据库
社区管理员
  • 其他数据库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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