C++通過ADO讀寫Excel檔案

來源:互聯網
上載者:User

標籤:http   sel   flow   static   nec   base   tac   engine   adc   

介紹

有時候我們需要從excel表格裡匯入、匯出資料。其中一種方式就是通過ADO的方式。在這裡,excel檔案被當作資料庫來處理,該方式不需要用戶端安裝Microsoft Excel,速度也夠快。

連接字串

這裡有兩種類型的連接字串,第一種是針對xls格式的:

Provider=Microsoft.JET.OLEDB.4.0;Data Source=data.xls;Extended Properties="Excel 8.0"

第二種是針對xlsx格式的:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=data.xlsx;Extended Properties="Excel 12.0 Xml" 
寫入

首先建立一個連接字串:

TESTHR(pCon.CreateInstance(__uuidof(Connection)));TESTHR(pCon->Open(connStr, "", "", NULL)); 

然後建立Command對象和表,注意表名就是excel的頁:

TESTHR(pCmd.CreateInstance(__uuidof(Command)));pCmd->ActiveConnection = pCon;       pCmd->CommandText = "CREATE TABLE MySheet    (A int, B varchar, C int, D int, E int, F int, G int, H int, I int, J varchar)";   pCmd->Execute(NULL, NULL, adCmdText); 

建立Recordset並增加記錄:

TESTHR(pRec.CreateInstance(__uuidof(Recordset)));pRec->Open("SELECT * FROM MySheet", _variant_t((IDispatch*)pCon),     adOpenKeyset, adLockOptimistic, adCmdText);for(int i = 0; i < writeRows; ++i){    TESTHR(pRec->AddNew());    char str[11] = {0}; for(int j = 0; j < 10; ++j) str[j] = ‘a‘ + (rand() % 26);    pRec->Fields->GetItem("A")->Value = _variant_t(i);                pRec->Fields->GetItem("B")->Value = _variant_t(str);                pRec->Fields->GetItem("C")->Value = _variant_t(i);    pRec->Fields->GetItem("D")->Value = _variant_t(i);    pRec->Fields->GetItem("E")->Value = _variant_t(i);    pRec->Fields->GetItem("F")->Value = _variant_t(i);    pRec->Fields->GetItem("G")->Value = _variant_t(i);    pRec->Fields->GetItem("H")->Value = _variant_t(i);    pRec->Fields->GetItem("I")->Value = _variant_t(i);    pRec->Fields->GetItem("J")->Value = _variant_t(str);            }TESTHR(pRec->Update());TESTHR(pRec->Close());
讀取

建立和開啟Recordset:

TESTHR(pRec.CreateInstance(__uuidof(Recordset)));       TESTHR(pRec->Open("SELECT * FROM [Sheet1$]", connStr,     adOpenStatic, adLockOptimistic, adCmdText));

如果excel的頁不清楚,可以通過索引來尋找:

TESTHR(pCon.CreateInstance(__uuidof(Connection)));TESTHR(pCon->Open(connStr, "", "", NULL));    pSchema = pCon->OpenSchema(adSchemaTables);      for(int i = 0; i < sheetIndex; ++i) pSchema->MoveNext();std::string sheetName =     (char*)(_bstr_t)pSchema->Fields->GetItem("TABLE_NAME")->Value.bstrVal;

讀取儲存格的值:

while(!pRec->adoEOF){    for(long i = 0; i < pRec->Fields->GetCount(); ++i)    {        if(i > 0) stream << ";";                            _variant_t v = pRec->Fields->GetItem(i)->Value;        if(v.vt == VT_R8)                                                       stream << v.dblVal;        if(v.vt == VT_BSTR)            stream << (char*)(_bstr_t)v.bstrVal;                                    }                                        stream << std::endl;    pRec->MoveNext();} 
參考資料
  1. How to Read and Write Excel Files in C++ via ADO
  2. C++ using adodb to read excel file in 64-bit windows 7?
  3. AccessDatabaseEngine#install instruction

C++通過ADO讀寫Excel檔案

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.