Click here to Skip to main content
15,885,877 members
Articles / Productivity Apps and Services / Microsoft Office

How to Read and Write Excel Files in C++ via ADO

Rate me:
Please Sign up or sign in to vote.
4.85/5 (22 votes)
10 Nov 2010CPOL 111.3K   6.4K   78   16
Using ADO for Access to XLS and XLSX files in C++

Introduction

Sometimes software developers need to export some data to Excel format or read some cells from Excel file. One way to do it without Excel automation is interaction with ADO. In this case, Excel files are treated as database. This method doesn't require Microsoft Excel and quickly enough, but it doesn't support formatting and formulas.

Connection Strings

There are two types of connection strings. First for binary format (xls):

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

Second for XML format (xlsx):

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

If input file doesn't have a header with column names then add ";HDR=NO" in extended properties.

Writing

First create a Connection object:

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

Afterwards, create Command object and table. Note that name of table is name of sheet:

C++
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); 

Create Recordset and add records with values:

C++
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());

Reading

Create and open Recordset:

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

If name of sheet is unknown, then it's possible to look it up by index:

C++
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;

Extract values of cells:

C++
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();
} 

History

  • 10th November, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Ukraine Ukraine
Author of LibXL library - a simple way for access to Excel files.

Comments and Discussions

 
QuestionProblem reading other data types than strings Pin
Gerald Schade22-Jan-13 13:15
Gerald Schade22-Jan-13 13:15 
AnswerRe: Problem reading other data types than strings Pin
kaban_21-Nov-14 3:39
kaban_21-Nov-14 3:39 
Questiongood code Pin
zhylei13-Jul-12 22:12
zhylei13-Jul-12 22:12 
QuestionHow to access a specific cell? Pin
SamerW7-Jun-12 7:41
SamerW7-Jun-12 7:41 
AnswerRe: How to access a specific cell? Pin
liugh69817-Jul-13 19:57
liugh69817-Jul-13 19:57 
GeneralMy vote of 5 Pin
Roger C Moore16-Jan-12 10:17
Roger C Moore16-Jan-12 10:17 
QuestionQuestion about how to use this code Pin
Member 848617913-Dec-11 22:35
Member 848617913-Dec-11 22:35 
GeneralMy vote of 4 Pin
kaskavalci11-Oct-11 9:19
kaskavalci11-Oct-11 9:19 
GeneralMy vote of 5 Pin
Cholo16-Nov-10 5:46
Cholo16-Nov-10 5:46 
GeneralWrite without header Pin
Wym212-Nov-10 7:29
professionalWym212-Nov-10 7:29 
GeneralRe: Write without header Pin
Dmytro Skrypnyk12-Nov-10 10:01
Dmytro Skrypnyk12-Nov-10 10:01 
GeneralGood! Pin
Paul Belikian11-Nov-10 14:01
Paul Belikian11-Nov-10 14:01 
GeneralMy vote of 5 Pin
Wym211-Nov-10 11:50
professionalWym211-Nov-10 11:50 
Generalspeed Pin
Nick Gorlov10-Nov-10 20:06
Nick Gorlov10-Nov-10 20:06 
GeneralRe: speed Pin
Dmytro Skrypnyk10-Nov-10 20:33
Dmytro Skrypnyk10-Nov-10 20:33 
Generalgood Pin
djzbj10-Nov-10 17:54
djzbj10-Nov-10 17:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.