⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test0.cpp

📁 c语言访问Excel数据库
💻 CPP
字号:
#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
//#include "test.h"
//#include <afxdb.h> 
//#include <odbcinst.h>
//#include <afxwin.h>
//#include <afxext.h>
//#include <afxcmn.h>
//#include <afx.h>
#include <iostream>
using namespace std;

//CString GetExcelDriver();
//void	ReadExcel();

/*void ado(){
	_ConnectionPtr m_pConnection; 
	_RecordsetPtr m_pResultRecordset; 
	try 
	{ 
		CString DBPathName=_T("G:\\灰色预测测试\\负荷预测历史数据.xls"); 
		CString strWay="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" DBPathName ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1;""" ; 
		HRESULT hr=m_pConnection.CreateInstance(__uuidof(Connection)); 
		m_pConnection->Open((LPCSTR)strWay,"","",adModeUnknown);
	} 
	catch(_com_error &;amp;e) { 
		AfxMessageBox(e.Description()); 
	}
	CString m_strSQL="select * from [历史数据$]"; 
	
	try { 
		m_pResultRecordset.CreateInstance(__uuidof(Recordset)); 
		m_pResultRecordset->Open(m_strSQL.AllocSysString(), m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText); 
	} 
	catch(_com_error &;amp;e) { 
		AfxMessageBox(e.Description()); 
	} 
}
*/

/*
void ReadExcel(){
	CDatabase database;
	CString sSql;
	CString sItem1,sItem2,sItem3;
	CString sDriver;
	CString sDsn;
	CString sPath,sFile;
	int nPos;

	sSql = "Hello ";
	GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
	sPath.ReleaseBuffer();
	
	nPos = sPath.ReverseFind('\\');
	sPath = sPath.Left(nPos);

	sFile = sPath + "\\demo.xls";//Get Excel filename
	//AfxMessageBox(sFile);
	sDriver = GetExcelDriver();
	if(sDriver.IsEmpty()){
		AfxMessageBox("can't find Excel Driver!");
		return;
	}

	sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s",sDriver,sFile);
	TRY{
		database.Open(NULL,false,false,sDsn);
		CRecordset recordset(&database);
        sSql = "SELECT Name, Age FROM Exceldemo " ; 
		//search
		recordset.Open(CRecordset::forwardOnly, sSql, CRecordset::readOnly);
		while(!recordset.IsEOF()){
			recordset.GetFieldValue("Name", sItem1);
			recordset.GetFieldValue("Age", sItem2);
			//recordset.GetFieldValue("直达公路里程(km)", sItem3);

			//cout<<sItem1<<"\t"<<sItem2<<"\t"<<endl;
			AfxMessageBox(sItem1);
			recordset.MoveNext();
		}
		database.Close();
	}
	CATCH(CDBException, e){
		AfxMessageBox("Database Error: "+e->m_strError);
	}
	END_CATCH;
}

CString GetExcelDriver(){
	char szBuf[2001];
	WORD cbBufMax = 2000;
	WORD cbBufOut;
	char *pszBuf = szBuf;
	CString sDriver;

	//reads the [ODBC Drivers] section of the system information and returns a list of descriptions of the installed drivers
	if(!SQLGetInstalledDrivers(szBuf,cbBufMax,&cbBufOut))
		return "";//failure

	do{
		if(strstr(pszBuf,"Excel") != 0){
			//Find
			sDriver = CString(pszBuf);
			break;
		}
		pszBuf = strchr(pszBuf,'\0') + 1;
	}while(pszBuf[1] != '\0');

	return sDriver;
}
*/

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	//ReadExcel();
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -