VS2008用mfc读Excel文件

来源:互联网 发布:淘宝博娱乐 编辑:程序博客网 时间:2024/06/03 02:05


1、获取驱动代码:自己添加的成员函数CString CMFC_SEECOLORDlg::GetExcelDriver(void),这里参考自:http://bbs.csdn.net/topics/340079163#new_post

CString CMFC_SEECOLORDlg::GetExcelDriver(void){wchar_t szBuf[2001];wchar_t excl[]=L"Excel";WORD cbBufMax = 2000;WORD cbBufOut;wchar_t *pszBuf = szBuf;CString sDriver;// 获取已安装驱动的名称(涵数在odbcinst.h里)if (!SQLGetInstalledDrivers(szBuf, cbBufMax, &cbBufOut))return L"";// 检索已安装的驱动是否有Excel...     do{if (wcsstr(pszBuf, excl) != 0){//发现 !sDriver = CString(pszBuf);break;}wchar_t ze={'\0'};pszBuf = wcschr(pszBuf,ze) + 1;}while (pszBuf[1] != '\0');return sDriver;}

2、读Excel的代码:加一个读Excel按钮,添加它的响应函数void CMFC_SEECOLORDlg::OnBnClickedButtonRead(),百度得到的,嘿嘿、、

void CMFC_SEECOLORDlg::OnBnClickedButtonRead(){// TODO: Add your control notification handler code hereCDatabase database;CString sSql;CString sItem1, sItem2,sItem3;CString sDriver;CString sDsn;CString sFile,sPath;//获取主程序所在路径,存在sPath中GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);sPath.ReleaseBuffer ();// MessageBox(sPath);int nPos;nPos=sPath.ReverseFind ('\\');sPath=sPath.Left (nPos);sFile = sPath + _T("\\Demo.xls");     // 将被读取的Excel文件名// 检索是否安装有Excel驱动 "Microsoft Excel Driver (*.xls)" sDriver = GetExcelDriver();MessageBox(sDriver);if (sDriver.IsEmpty()){// 没有发现Excel驱动AfxMessageBox(_T("没有安装Excel驱动!"));return;}// 创建进行存取的字符串sDsn.Format(_T("ODBC;DRIVER={%s};DSN=' ';DBQ=%s"), sDriver, sFile);TRY{// 打开数据库(既Excel文件)database.Open(NULL, false, false, sDsn);CRecordset recset(&database);// 设置读取的查询语句.sSql = "SELECT Num,Name, Age " //设置索引顺序     "FROM Exceldemo " ;                "ORDER BY Name ";// 执行查询语句recset.Open(CRecordset::forwardOnly, sSql, CRecordset::readOnly);// 获取查询结果while (!recset.IsEOF()){//读取Excel内部数值recset.GetFieldValue(_T("Num"), sItem1);recset.GetFieldValue(_T("Name"), sItem2);recset.GetFieldValue(_T("Age"), sItem3);//显示记取的内容m_ExcelList.AddString( sItem1 + _T(" --> ")+sItem2+ _T(" --> ")+sItem3 );// 移到下一行recset.MoveNext();}// 关闭数据库database.Close();}CATCH(CDBException, e){// 数据库操作产生异常时...AfxMessageBox(_T("数据库错误: ")+ e->m_strError);}END_CATCH;}


3、写Excel的代码:加一个写Excel按钮,添加它的响应函数void CMFC_SEECOLORDlg::OnBnClickedButtonWrite(),也是百度了,文库里有下载、、

void CMFC_SEECOLORDlg::OnBnClickedButtonWrite(){// TODO: Add your control notification handler code here// TODO: Add your control notification handler code hereCDatabase database;CString sDriver = _T("MICROSOFT EXCEL DRIVER (*.XLS)"); // Excel安装驱动CString sExcelFile,sPath; CString sSql;//获取主程序所在路径,存在sPath中GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);sPath.ReleaseBuffer ();int nPos;nPos=sPath.ReverseFind ('\\');sPath=sPath.Left (nPos);sExcelFile = sPath + _T("\\Demo.xls");     // 要建立的Excel文件TRY{// 创建进行存取的字符串sSql.Format(_T("DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s"),sDriver, sExcelFile, sExcelFile);// 创建数据库 (既Excel表格文件)if( database.OpenEx(sSql,CDatabase::noOdbcDialog) ){// 创建表结构(序号、姓名、年龄)sSql = "CREATE TABLE Exceldemo (Num Number,Name TEXT,Age NUMBER)";//可以用中文表示序号,姓名,年龄等database.ExecuteSQL(sSql);// 插入数值sSql = "INSERT INTO Exceldemo (Num,Name,Age) VALUES (1,'小西',24)";database.ExecuteSQL(sSql);sSql = "INSERT INTO Exceldemo (Num,Name,Age) VALUES (2,'小东',22)";database.ExecuteSQL(sSql);sSql = "INSERT INTO Exceldemo (Num,Name,Age) VALUES (3,'小朱',25)";database.ExecuteSQL(sSql);sSql = "INSERT INTO Exceldemo (Num,Name,Age) VALUES (4,'小鸭',27)";database.ExecuteSQL(sSql);}     // 关闭数据库database.Close();AfxMessageBox(_T("Excel文件写入成功!"));}CATCH_ALL(e){TRACE1("Excel驱动没有安装: %s",sDriver);}END_CATCH_ALL;}