关于以ODBC和ADO方式访问excel数据库总结

1635人阅读 评论(4) 收藏 举报

温馨提醒,看帖子前请复习下连接数据方式有哪几种,以及各有什么优缺点,然后再来看本贴。 

首先阐述一个小问题,如何用excel建立一张表呢?

1.excel 2003的情况,假如excel中第一行是字段名,第二行以后是数据行,则选择多行(包括第一行),然后菜单中选择“插入”->名称->自定义名称。其中这个名称就是表名,用在select * from 表名语句中。

2.excel 2007的情况,假如excel中第一行是字段名,第二行以后是数据行,则选择多行(包括第一行),然后菜单中选择“公式”->自定义名称。其中这个名称就是表名,用在select * from 表名语句中。

然后来写ODBC和ADO方式来访问excel

ODBC:代码如下

  1. void CDirCheckDemoDlg::OnReadExcel(CString csFile)  
  2. {  
  3.     CDatabase database;  
  4.     CString sSql;  
  5.     CString sItem[11];  
  6.     CString sDriver;  
  7.     CString sDsn;  
  8.     CString sFile,sPath;      
  9.     sFile = csFile ;            // 将被读取的Excel文件名  
  10.   
  11.     //动态注册数据源  
  12.       
  13.       
  14.      if(!SQLConfigDataSource(0,ODBC_ADD_DSN,"Microsoft Excel Driver (*.xls)\0","DSN=mydb\0Description=MouseDataBase\0DBQ=sFile\0\0"))  
  15.     {  
  16.     AfxMessageBox("添加DSN失败...");  
  17.     return;  
  18.     }  
  19.   
  20.   
  21.     // 检索是否安装有Excel驱动 "Microsoft Excel Driver (*.xls)"   
  22.     sDriver = GetExcelDriver();  
  23.     if (sDriver.IsEmpty())  
  24.     {  
  25.         // 没有发现Excel驱动  
  26.         AfxMessageBox("没有安装Excel驱动!");  
  27.         return;  
  28.     }  
  29.       
  30.     // 创建进行存取的连接字符串  
  31.     sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s", sDriver, sFile);  
  32.       
  33.     TRY  
  34.     {  
  35.         // 建立与ODBC数据源的连接  
  36.         database.Open(NULL, falsefalse, sDsn);      
  37.         // 设置读取的查询语句  
  38.         sSql = "SELECT *"//sSql = "SELECT Name, Age "        
  39.             "FROM ExcelDemo " ;                  
  40.         "ORDER BY Name ";  
  41.         CRecordset recset(&database);  
  42.         // 执行查询语句  
  43.         recset.Open(CRecordset::forwardOnly, sSql, CRecordset::readOnly);  
  44.           
  45.         // 获取查询结果  
  46.         while (!recset.IsEOF())  
  47.         {  
  48.             //读取Excel内部数值  
  49.             recset.GetFieldValue("FileNr 1", sItem[0]);  
  50.             recset.GetFieldValue("ReportFlag", sItem[1]);  
  51.             recset.GetFieldValue("Date",sItem[2]);  
  52.             recset.GetFieldValue("Time",sItem[3]);  
  53.             recset.GetFieldValue("Output volt",sItem[4]);  
  54.             recset.GetFieldValue("Output cur",sItem[5]);  
  55.             recset.GetFieldValue("Reg volt",sItem[6]);  
  56.             recset.GetFieldValue("Reg cur",sItem[7]);  
  57.             recset.GetFieldValue("ExcVolt",sItem[8]);  
  58.             recset.GetFieldValue(" Meas 1",sItem[9]);  
  59.             recset.GetFieldValue(" Meas 2",sItem[10]);            
  60.             //显示记取的内容  
  61.             for (int i = 10;i >= 0;i--)  
  62.             {  
  63.                 m_List.AddString( sItem[i]);//m_List为CListBox控件名  
  1. }  
  2.   
  3.   
  4. /* 
  5. CString string; 
  6. string.Format("%s %s",sItem1,sItem2); 
  7. AfxMessageBox(string);*/  
  8.          // 移到下一行  
  9.          recset.MoveNext();  
  10.      }  
  11.   
  12.      // 关闭数据库  
  13.      database.Close();  
  14.   
  15.  }  
  16.  CATCH(CDBException, e)  
  17.  {  
  18.      // 数据库操作产生异常时  
  19.      AfxMessageBox("数据库错误: " + e->m_strError);  
  20.  }  
  21.  END_CATCH;  

以上是ODBC访问excel 2003代码,注意点:连接字符串,还有要运行程序才知道查询得到的数据顺序是随机的,不是第一句程序访问的数据显示一定是在第一句数据的。

ODBC连接excel 2007代码,请修改相应连接字符串,字符串网上好像没找到,自己找吧。

ADO访问excel 代码:

  1. void CAdoDlg::OnBtnQuery()   
  2. {  
  3.   
  4.     CoInitialize(NULL);  
  5.     _ConnectionPtr pConn(_uuidof(Connection));  
  6.     _RecordsetPtr pRst(_uuidof(Recordset));  
  7.    
  8.    // 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字符串  
  9.  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字符串  
  10.     pConn->Open("","","",adModeUnknown);  
  11.    
  12. // pRst=pConn->Execute("select * from ExcelDemo",NULL,adCmdText);  
  13.     pRst=pConn->Execute("select * from File",NULL,adCmdText);  
  14.    while (!pRst->rsEOF)  
  15.    {  
  16.            ((CListBox *)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect("FileNr 1"));  
  17.          ((CListBox *)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect("ReportFlag"));  
  18.          ((CListBox *)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect("Date"));  
  19.          ((CListBox *)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect("Time"));  
  20.         ((CListBox *)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect("Output volt"));  
  21.         ((CListBox *)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect("Output cur"));  
  22.         ((CListBox *)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect("Reg volt"));  
  23.         ((CListBox *)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect("Reg cur"));  
  24.         ((CListBox *)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect("ExcVolt"));  
  25.         ((CListBox *)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect(" Meas 1"));  
  26.         ((CListBox *)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect(" Meas 2"));  
  27.         pRst->MoveNext();  
  28.    }  
  29.   
  30.       pRst->Close();  
  31.      pConn->Close();  
  32.      pRst.Release();  
  33.     pConn.Release();  
  34.     CoUninitialize();  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值