VC6.0利用ADO读取excel
运行环境:VC6.0
特点:简单易懂,适合初学者。直接拷来用也容易。
简单实例:读取,修改,插入,遍历,以及报错记录。
几种功能都有注释,一看就懂。
注意:程序运行时,保证读取的excel不被打开,打开时会占用文件,导致无法读取。
利用ADO读取,不需要任何插件,也不需要配置数据源,直接就能读取excel
代码片段和文件信息
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 90773 2008-04-14 20:00 VC6.0ado读excelDebugmsado15.tlh
文件 76691 2008-04-14 20:00 VC6.0ado读excelDebugmsado15.tli
文件 204892 2018-03-23 08:55 VC6.0ado读excelDebug est.exe
文件 266500 2018-03-23 08:55 VC6.0ado读excelDebug est.ilk
文件 113068 2018-03-23 08:55 VC6.0ado读excelDebug est.obj
文件 5780692 2018-03-23 08:50 VC6.0ado读excelDebug est.pch
文件 738304 2018-03-23 08:55 VC6.0ado读excelDebug est.pdb
文件 295936 2018-03-23 08:55 VC6.0ado读excelDebugvc60.idb
文件 184320 2018-03-23 08:55 VC6.0ado读excelDebugvc60.pdb
文件 90639 2008-04-14 20:00 VC6.0ado读excelmsado15.tlh
文件 76624 2008-04-14 20:00 VC6.0ado读excelmsado15.tli
文件 3239 2018-03-23 08:56 VC6.0ado读excel est.cpp
文件 3377 2015-02-13 14:59 VC6.0ado读excel est.dsp
文件 533 2015-02-13 14:59 VC6.0ado读excel est.dsw
文件 295936 2018-03-23 08:56 VC6.0ado读excel est.ncb
文件 48640 2018-03-23 08:56 VC6.0ado读excel est.opt
文件 1286 2018-03-23 08:55 VC6.0ado读excel est.plg
文件 20480 2018-03-23 08:55 VC6.0ado读excel est.xls
目录 0 2018-03-23 08:55 VC6.0ado读excelDebug
目录 0 2018-03-23 08:56 VC6.0ado读excel
----------- --------- ---------- ----- ----
8291930 20
#include “stdio.h“
#include
//#include
#include
#include
#import “C:Program Filescommon filessystemadomsado15.dll“ no_namespace rename(“EOF““EndOfFields“)
int errlog(_bstr_t task_bstr_t err)
{
FILE *fp; /*定义文件指针*/
fp=fopen(task+“_err.txt“ “w+“); /*建立一个文字文件只写*/
fputs(err fp);/*向所建文件写入一串字符*/
fclose(fp);
return 0;
}//出错记录功能
int main(int argcchar* argv[])
{char PlanVersion[40];
_bstr_t Querytask;
::CoInitialize(NULL);
_bstr_t capacity;
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
WCHAR* szServerName = L“Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.xls;Extended Properties=“Excel 8.0;HDR=Yes;IMEX=0““;
HRESULT hr = m_pConnection.CreateInstance(__uuidof(Connection));
hr = m_pConnection->Open((_bstr_t)szServerName““““0);
hr = m_pRecordset.CreateInstance(__uuidof(Recordset));
Query=“select name from [Sheet1$] where id=1“;//读取
try{
hr = m_pRecordset->Open(Query(IDispatch *)m_pConnectionadOpenDynamicadLockOptimisticadCmdText);
if(!m_pRecordset->EndOfFields)
{
capacity=m_pRecordset->GetCollect(“name“);
}if ( m_pRecordset->State )m_pRecordset->Close();
}
catch(const _com_error e)//获取错误信息以防错误
{
printf(“读取excel失败咧
“);
}
{
char *strTmp = (char*)capacity;
printf(“读取到字符串-->%s
“strTmp);
}
Query=“update [Sheet1$] set name=‘set‘ where id=1“;//修改
try{
hr = m_pRecordset->Open(Query(IDispatch *)m_pConnectionadOpenDynamicadLockOptimisticadCmdText);
}
catch(const _com_error e)//获取错误信息以防错误
{
printf(“修改excel失败
“);
}
Query=“insert into [Sheet1$] (idname) values (‘3‘‘adsfwe‘)“;//插入
try{
hr = m_pRecordset->Open(Query(IDispatch *)m_pConnectionadOpenDynamicadLockOptimisticadCmdText);
}
catch(const _com_error e)//获取错误信息以防错误
{
printf(“插入excel失败
“);
}
Query=“select name from [Sheet1$] “;//遍历
try{
hr = m_pRecordset->Open(Query(IDispatch *)m_pConnectionadOpenDynamicadLockOptimisticadCmdText);
// if(!m_pRecordset->EndOfFields)
{ while(!m_pRecordset->EndOfFields)
{
capacity=m_pRecordset->GetCollect(“name“);
{
char *strTmp = (char*)capacity;
printf(“遍历读取到的字符串-->%s
“strTmp);
}
m_pRecordset->MoveNext();
}
}if ( m_pRecordset->State )m_pRecordset->Close();
}
catch(const _com_error e)//获取错误信息以防错误
{
printf(“读取excel失败咧
“);
}
////这段是生成指定格式的时间所用
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( “%4d-%02d-%02d
“1900+timeinfo->tm_year1+timeinfo->tm_montimeinfo->tm_mday);
////指定格式时间
/////遍历工作簿
m_pRecordset = m_pConnection->OpenSchema(adSchemaTables);
while(!m_pRecordset->EndOfFields)
{
capacity = (_bstr_t)(m_pRecordset->GetFields()->GetItem(“TABLE_NAME“)->Value);
{
char *strTmp = (char*)capacity;
printf(“工作簿-->%s
“strTmp);
}
m_pRecordset->MoveNext();
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 90773 2008-04-14 20:00 VC6.0ado读excelDebugmsado15.tlh
文件 76691 2008-04-14 20:00 VC6.0ado读excelDebugmsado15.tli
文件 204892 2018-03-23 08:55 VC6.0ado读excelDebug est.exe
文件 266500 2018-03-23 08:55 VC6.0ado读excelDebug est.ilk
文件 113068 2018-03-23 08:55 VC6.0ado读excelDebug est.obj
文件 5780692 2018-03-23 08:50 VC6.0ado读excelDebug est.pch
文件 738304 2018-03-23 08:55 VC6.0ado读excelDebug est.pdb
文件 295936 2018-03-23 08:55 VC6.0ado读excelDebugvc60.idb
文件 184320 2018-03-23 08:55 VC6.0ado读excelDebugvc60.pdb
文件 90639 2008-04-14 20:00 VC6.0ado读excelmsado15.tlh
文件 76624 2008-04-14 20:00 VC6.0ado读excelmsado15.tli
文件 3239 2018-03-23 08:56 VC6.0ado读excel est.cpp
文件 3377 2015-02-13 14:59 VC6.0ado读excel est.dsp
文件 533 2015-02-13 14:59 VC6.0ado读excel est.dsw
文件 295936 2018-03-23 08:56 VC6.0ado读excel est.ncb
文件 48640 2018-03-23 08:56 VC6.0ado读excel est.opt
文件 1286 2018-03-23 08:55 VC6.0ado读excel est.plg
文件 20480 2018-03-23 08:55 VC6.0ado读excel est.xls
目录 0 2018-03-23 08:55 VC6.0ado读excelDebug
目录 0 2018-03-23 08:56 VC6.0ado读excel
----------- --------- ---------- ----- ----
8291930 20
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。
评论列表(条)