資源簡介
運行環境:VC6.0
特點:簡單易懂,適合初學者。直接拷來用也容易。
簡單實例:讀取,修改,插入,遍歷,以及報錯記錄。
幾種功能都有注釋,一看就懂。
注意:程序運行時,保證讀取的excel不被打開,打開時會占用文件,導致無法讀取。
利用ADO讀取,不需要任何插件,也不需要配置數據源,直接就能讀取excel

代碼片段和文件信息
#include?“stdio.h“
#include?
//#include?
#include?
#include?
?
?
#import?“C:\Program?Files\common?files\system\ado\msado15.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失敗咧\n“);
}?
{
???char?*strTmp?=?(char*)capacity;?
printf(“讀取到字符串-->%s\n“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失敗\n“);
}?
Query=“insert?into?[Sheet1$]?(idname)?values?(‘3‘‘adsfwe‘)“;//插入
try{
hr?=?m_pRecordset->Open(Query(IDispatch?*)m_pConnectionadOpenDynamicadLockOptimisticadCmdText);
}
catch(const?_com_error?e)//獲取錯誤信息以防錯誤
{
?????printf(“插入excel失敗\n“);
}?
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\n“strTmp);
}
??m_pRecordset->MoveNext();
?}
}if?(?m_pRecordset->State?)m_pRecordset->Close();
}
catch(const?_com_error?e)//獲取錯誤信息以防錯誤
{
?????printf(“讀取excel失敗咧\n“);
}?
////這段是生成指定格式的時間所用
time_t?rawtime;??
struct?tm?*?timeinfo;??
?time?(?&rawtime?);??
timeinfo?=?localtime?(?&rawtime?);??
?printf?(?“%4d-%02d-%02d\n“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\n“strTmp);
}
????m_pRecordset->MoveNext();?
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????90773??2008-04-14?20:00??VC6.0ado讀excel\Debug\msado15.tlh
?????文件??????76691??2008-04-14?20:00??VC6.0ado讀excel\Debug\msado15.tli
?????文件?????204892??2018-03-23?08:55??VC6.0ado讀excel\Debug\test.exe
?????文件?????266500??2018-03-23?08:55??VC6.0ado讀excel\Debug\test.ilk
?????文件?????113068??2018-03-23?08:55??VC6.0ado讀excel\Debug\test.obj
?????文件????5780692??2018-03-23?08:50??VC6.0ado讀excel\Debug\test.pch
?????文件?????738304??2018-03-23?08:55??VC6.0ado讀excel\Debug\test.pdb
?????文件?????295936??2018-03-23?08:55??VC6.0ado讀excel\Debug\vc60.idb
?????文件?????184320??2018-03-23?08:55??VC6.0ado讀excel\Debug\vc60.pdb
?????文件??????90639??2008-04-14?20:00??VC6.0ado讀excel\msado15.tlh
?????文件??????76624??2008-04-14?20:00??VC6.0ado讀excel\msado15.tli
?????文件???????3239??2018-03-23?08:56??VC6.0ado讀excel\test.cpp
?????文件???????3377??2015-02-13?14:59??VC6.0ado讀excel\test.dsp
?????文件????????533??2015-02-13?14:59??VC6.0ado讀excel\test.dsw
?????文件?????295936??2018-03-23?08:56??VC6.0ado讀excel\test.ncb
?????文件??????48640??2018-03-23?08:56??VC6.0ado讀excel\test.opt
?????文件???????1286??2018-03-23?08:55??VC6.0ado讀excel\test.plg
?????文件??????20480??2018-03-23?08:55??VC6.0ado讀excel\test.xls
?????目錄??????????0??2018-03-23?08:55??VC6.0ado讀excel\Debug
?????目錄??????????0??2018-03-23?08:56??VC6.0ado讀excel
-----------?---------??----------?-----??----
??????????????8291930????????????????????20
評論
共有 條評論