请问用ADO能不能查询Excel表格

zz421 2003-08-21 04:10:57
我要开发一个绘图程序,其中的点的数据用Excel表格存储比较方便,单不只能不能查询,怎样得到其中的值,用与绘图,请各位大师给予指点
...全文
64 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
康斯坦汀 2003-08-22
  • 打赏
  • 举报
回复
为什么要用excel, ADO可以访问excel, 但是要带上很多dll,
看你的需求,完全用不着excel,

找本MFC的书,看看“类的序列化”的相关内容,使用序列化功能,存在文件里,
读写都很容易。完全满足你的要求。

例子:MFC中自带的绘图程序例子中都有这样的内容。比如经典的"SCRIBBLE"

ZHENG017 2003-08-22
  • 打赏
  • 举报
回复
#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();
}
//比方说如果table_name为test$的话,那么sqltext中的表名应该为[test$],就是在表名前加上"[]"
//就是这个"[]"浪费了我很多时间
_bstr_t sqltext="SELECT * from [test$] ";
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 ",(LPCSTR)(_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;
}

4,011

社区成员

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

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