温馨提醒,看帖子前请复习下连接数据方式有哪几种,以及各有什么优缺点,然后再来看本贴。
首先阐述一个小问题,如何用excel建立一张表呢?
1.excel 2003的情况,假如excel中第一行是字段名,第二行以后是数据行,则选择多行(包括第一行),然后菜单中选择“插入”->名称->自定义名称。其中这个名称就是表名,用在select * from 表名语句中。
2.excel 2007的情况,假如excel中第一行是字段名,第二行以后是数据行,则选择多行(包括第一行),然后菜单中选择“公式”->自定义名称。其中这个名称就是表名,用在select * from 表名语句中。
然后来写ODBC和ADO方式来访问excel
ODBC:代码如下
- void CDirCheckDemoDlg::OnReadExcel(CString csFile)
- {
- CDatabase database;
- CString sSql;
- CString sItem[11];
- CString sDriver;
- CString sDsn;
- CString sFile,sPath;
- sFile = csFile ; // 将被读取的Excel文件名
- //动态注册数据源
- if(!SQLConfigDataSource(0,ODBC_ADD_DSN,"Microsoft Excel Driver (*.xls)\0","DSN=mydb\0Description=MouseDataBase\0DBQ=sFile\0\0"))
- {
- AfxMessageBox("添加DSN失败...");
- return;
- }
- // 检索是否安装有Excel驱动 "Microsoft Excel Driver (*.xls)"
- sDriver = GetExcelDriver();
- if (sDriver.IsEmpty())
- {
- // 没有发现Excel驱动
- AfxMessageBox("没有安装Excel驱动!");
- return;
- }
- // 创建进行存取的连接字符串
- sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s", sDriver, sFile);
- TRY
- {
- // 建立与ODBC数据源的连接
- database.Open(NULL, false, false, sDsn);
- // 设置读取的查询语句
- sSql = "SELECT *"//sSql = "SELECT Name, Age "
- "FROM ExcelDemo " ;
- "ORDER BY Name ";
- CRecordset recset(&database);
- // 执行查询语句
- recset.Open(CRecordset::forwardOnly, sSql, CRecordset::readOnly);
- // 获取查询结果
- while (!recset.IsEOF())
- {
- //读取Excel内部数值
- recset.GetFieldValue("FileNr 1", sItem[0]);
- recset.GetFieldValue("ReportFlag", sItem[1]);
- recset.GetFieldValue("Date",sItem[2]);
- recset.GetFieldValue("Time",sItem[3]);
- recset.GetFieldValue("Output volt",sItem[4]);
- recset.GetFieldValue("Output cur",sItem[5]);
- recset.GetFieldValue("Reg volt",sItem[6]);
- recset.GetFieldValue("Reg cur",sItem[7]);
- recset.GetFieldValue("ExcVolt",sItem[8]);
- recset.GetFieldValue(" Meas 1",sItem[9]);
- recset.GetFieldValue(" Meas 2",sItem[10]);
- //显示记取的内容
- for (int i = 10;i >= 0;i--)
- {
- m_List.AddString( sItem[i]);//m_List为CListBox控件名
- }
- /*
- CString string;
- string.Format("%s %s",sItem1,sItem2);
- AfxMessageBox(string);*/
- // 移到下一行
- recset.MoveNext();
- }
- // 关闭数据库
- database.Close();
- }
- CATCH(CDBException, e)
- {
- // 数据库操作产生异常时
- AfxMessageBox("数据库错误: " + e->m_strError);
- }
- END_CATCH;
以上是ODBC访问excel 2003代码,注意点:连接字符串,还有要运行程序才知道查询得到的数据顺序是随机的,不是第一句程序访问的数据显示一定是在第一句数据的。
ODBC连接excel 2007代码,请修改相应连接字符串,字符串网上好像没找到,自己找吧。
ADO访问excel 代码:
- void CAdoDlg::OnBtnQuery()
- {
- CoInitialize(NULL);
- _ConnectionPtr pConn(_uuidof(Connection));
- _RecordsetPtr pRst(_uuidof(Recordset));
- // pConn->ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=f://study/win32_app/Ado/Demo.xls;Extended Properties=Excel 8.0;Persist Security Info=False";//访问excel 2003字符串
- pConn->ConnectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=f://study/win32_app/Ado/1.xlsx;Extended Properties='Excel 12.0 Xml;HDR=YES'";//访问excel 2007字符串
- pConn->Open("","","",adModeUnknown);
- // pRst=pConn->Execute("select * from ExcelDemo",NULL,adCmdText);
- pRst=pConn->Execute("select * from File",NULL,adCmdText);
- while (!pRst->rsEOF)
- {
- ((CListBox *)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect("FileNr 1"));
- ((CListBox *)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect("ReportFlag"));
- ((CListBox *)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect("Date"));
- ((CListBox *)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect("Time"));
- ((CListBox *)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect("Output volt"));
- ((CListBox *)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect("Output cur"));
- ((CListBox *)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect("Reg volt"));
- ((CListBox *)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect("Reg cur"));
- ((CListBox *)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect("ExcVolt"));
- ((CListBox *)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect(" Meas 1"));
- ((CListBox *)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect(" Meas 2"));
- pRst->MoveNext();
- }
- pRst->Close();
- pConn->Close();
- pRst.Release();
- pConn.Release();
- CoUninitialize();
- }