明輝手游網(wǎng)中心:是一個免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺!

直接通過ODBC讀寫Excel表格文件

[摘要]譯者:徐景周(原作:Alexander Mikula)想要通過ODBC直接讀、寫Excel表格文件,首先,應(yīng)確保ODBC中已安裝有Excel表格文件的驅(qū)動"MICROSOFT EXCEL ...
譯者:徐景周(原作:Alexander Mikula)

想要通過ODBC直接讀、寫Excel表格文件,首先,應(yīng)確保ODBC中已安裝有Excel表格文件的驅(qū)動"MICROSOFT EXCEL DRIVER (*.XLS)"。然后,可根據(jù)下面步驟進(jìn)行:

1. 在StdAfx.h文件中加入:

#include <afxdb.h>
#include <odbcinst.h>

2. 通過ODBC直接創(chuàng)建Excel文件并在表中插入數(shù)據(jù)(暫定文件名:Demo.xls) //創(chuàng)建并寫入Excel文件
void CRWExcel::WriteToExcel()
{
  CDatabase database;
  CString sDriver = "MICROSOFT EXCEL DRIVER (*.XLS)"; // Excel安裝驅(qū)動
  CString sExcelFile = "c:\\demo.xls";                // 要建立的Excel文件
  CString sSql;
    
  TRY
  {
    // 創(chuàng)建進(jìn)行存取的字符串
    sSql.Format("DRIVER={%s};DSN='''';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s",
                sDriver, sExcelFile, sExcelFile);

    // 創(chuàng)建數(shù)據(jù)庫 (既Excel表格文件)
    if( database.OpenEx(sSql,CDatabase::noOdbcDialog) )
    {
      // 創(chuàng)建表結(jié)構(gòu)(姓名、年齡)
      sSql = "CREATE TABLE demo (Name TEXT,Age NUMBER)";
      database.ExecuteSQL(sSql);

      // 插入數(shù)值
      sSql = "INSERT INTO demo (Name,Age) VALUES (''徐景周'',26)";
      database.ExecuteSQL(sSql);

      sSql = "INSERT INTO demo (Name,Age) VALUES (''徐志慧'',22)";
      database.ExecuteSQL(sSql);

      sSql = "INSERT INTO demo (Name,Age) VALUES (''郭徽'',27)";
      database.ExecuteSQL(sSql);
    }      

    // 關(guān)閉數(shù)據(jù)庫
    database.Close();
  }
  CATCH_ALL(e)
  {
    TRACE1("Excel驅(qū)動沒有安裝: %s",sDriver);
  }
  END_CATCH_ALL;
}

3. 通過ODBC直接讀取Excel文件(暫定文件名:Demo.xls) // 讀取Excel文件
void CRWExcel::ReadFromExcel()
{
    CDatabase database;
    CString sSql;
    CString sItem1, sItem2;
    CString sDriver;
    CString sDsn;
    CString sFile = "Demo.xls";  // 將被讀取的Excel文件名
                                     
    // 檢索是否安裝有Excel驅(qū)動 "Microsoft Excel Driver (*.xls)"
    sDriver = GetExcelDriver();
    if (sDriver.IsEmpty())
    {
        // 沒有發(fā)現(xiàn)Excel驅(qū)動
        AfxMessageBox("沒有安裝Excel驅(qū)動!");
        return;
    }
    
    // 創(chuàng)建進(jìn)行存取的字符串
    sDsn.Format("ODBC;DRIVER={%s};DSN='''';DBQ=%s", sDriver, sFile);

    TRY
    {
        // 打開數(shù)據(jù)庫(既Excel文件)
        database.Open(NULL, false, false, sDsn);
        
        CRecordset recset(&database);

        // 設(shè)置讀取的查詢語句.
        sSql = "SELECT Name, Age "       
               "FROM demo "                 
               "ORDER BY Name ";
    
        // 執(zhí)行查詢語句
        recset.Open(CRecordset::forwardOnly, sSql, CRecordset::readOnly);

        // 獲取查詢結(jié)果
        while (!recset.IsEOF())
        {
            //讀取Excel內(nèi)部數(shù)值
            recset.GetFieldValue("Name ", sItem1);
            recset.GetFieldValue("Age", sItem2);

            // 移到下一行
            recset.MoveNext();
        }

        // 關(guān)閉數(shù)據(jù)庫
        database.Close();
                             
    }
    CATCH(CDBException, e)
    {
        // 數(shù)據(jù)庫操作產(chǎn)生異常時...
        AfxMessageBox("數(shù)據(jù)庫錯誤: " + e->m_strError);
    }
    END_CATCH;
}

4. 獲取ODBC中Excel驅(qū)動的函數(shù) CString CRWExcel::GetExcelDriver()
{
    char szBuf[2001];
    WORD cbBufMax = 2000;
    WORD cbBufOut;
    char *pszBuf = szBuf;
    CString sDriver;

    // 獲取已安裝驅(qū)動的名稱(涵數(shù)在odbcinst.h里)
    if (!SQLGetInstalledDrivers(szBuf, cbBufMax, &cbBufOut))
        return "";
    
    // 檢索已安裝的驅(qū)動是否有Excel...
    do
    {
        if (strstr(pszBuf, "Excel") != 0)
        {
            //發(fā)現(xiàn) !
            sDriver = CString(pszBuf);
            break;
        }
        pszBuf = strchr(pszBuf, ''\0'') + 1;
    }
    while (pszBuf[1] != ''\0'');

    return sDriver;
}

作者信息:
姓名:徐景周(未來工作室 Future Studio)
EMAIL:jingzhou_xu@163.net