Click here to Skip to main content
13,257,667 members (78,732 online)
Click here to Skip to main content
Add your own
alternative version


105 bookmarked
Posted 12 Jan 2000

Creating Excel Sheets using ODBC

, 12 Jan 2000
Rate this:
Please Sign up or sign in to vote.
Writing to Excel spreadsheets using only ODBC
<!-- Article Starts -->

The Problem

Many apps offer an export function. So wouldn´t it be nice to be able to easily save that result as an Excel sheet?

ODBC does make this possible, but there´s one little drawback: Using ODBC the usual way there has to be a registered datasource (DSN) in the ODBC manager.

This is not very useful because you´d have to install that DSN locally on every machine that should support your export function.

The Solution

Omiting the DSN tag in the connect string of CDatabase::OpenEx() gives us the opportunity to refer the ODBC-Driver directly using its name so we don´t have to have a DSN registered. This, of course, implies that the name of the ODBC-Driver is exactly known.

If you just want to test if a certain driver is present (to show the supported extensions in the CFileOpenDlg for example) just try to CDatabase::OpenEx() it. If it isn´t installed an exception gets thrown.

To create and write to that Excel sheet you simply use SQL as shown in the code sample below.

What is Needed

In order to get the code below going you have to

  • have <afxdb.h> included
  • have an installed ODBC-driver called "MICROSOFT EXCEL DRIVER (*.XLS)"

The Source code

// this example creates the Excel file C:\DEMO.XLS, puts in a worksheet with two
// columns (one text the other numeric) an appends three no-sense records.   
void MyDemo::Put2Excel()
  CDatabase database;
  CString sDriver = "MICROSOFT EXCEL DRIVER (*.XLS)"; // exactly the same name as in the ODBC-Manager
  CString sExcelFile = "c:\\demo.xls";                // Filename and path for the file to be created
  CString sSql;
    // Build the creation string for access without DSN
                sDriver, sExcelFile, sExcelFile);

    // Create the database (i.e. Excel sheet)
    if( database.OpenEx(sSql,CDatabase::noOdbcDialog) )
      // Create table structure
      sSql = "CREATE TABLE demo (Name TEXT,Age NUMBER)";

      // Insert data
      sSql = "INSERT INTO demo (Name,Age) VALUES ('Bruno Brutalinsky',45)";

      sSql = "INSERT INTO demo (Name,Age) VALUES ('Fritz Pappenheimer',30)";

      sSql = "INSERT INTO demo (Name,Age) VALUES ('Hella Wahnsinn',28)";

    // Close database
    TRACE1("Driver not installed: %s",sDriver);


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

No Biography provided

You may also be interested in...


Comments and Discussions

GeneralRe: ODBC excel driver Pin
Herschel S. Krustofsky11-Apr-03 9:55
memberHerschel S. Krustofsky11-Apr-03 9:55 
GeneralPlain C Version Available Pin
Anonymous19-Feb-03 5:08
sussAnonymous19-Feb-03 5:08 
GeneralRe: Plain C Version Available Pin
BilloKhan18-Nov-05 16:34
memberBilloKhan18-Nov-05 16:34 
GeneralRe: Plain C Version Available Pin
liaohaiwen10-Apr-08 20:10
memberliaohaiwen10-Apr-08 20:10 
GeneralStoring date and time values in Excel using ODBC Pin
Klaus Kurt9-Jan-03 6:41
memberKlaus Kurt9-Jan-03 6:41 
Generalsingle quote mark Pin
Martin Step10-Jul-02 0:34
memberMartin Step10-Jul-02 0:34 
GeneralRe: single quote mark Pin
oneleg14-Mar-05 2:46
memberoneleg14-Mar-05 2:46 
Generaloverwriting cells in existing table Pin
Anonymous26-Jun-02 2:26
memberAnonymous26-Jun-02 2:26 
Using INSERT INTO in existing tables new data is added at the end of the table. But how to overwrite existing data with the new data in the table?
I can't get through with following possibility I've found:

sSql = "UPDATE demo SET Person = 'Hermann' WHERE Nummer = 2";

How can i manage this in a loop? Something like:

int i;
for (i=0; i<=10; i++)
sSql = "UPDATE demo SET Person = 'Hermann' WHERE Nummer = '" + i + "'";

is not working 'cause VC is telling me I'm trying to add two pointers.

Something like "recset.MoveNext()" would be fine...
By the way: What is the opposite of the CRecordSet instruction of GetFieldValue? Overwriting cells with a CRecordSet instruction would help me. I could use the "while"-loop and move in the recset with MoveNext().
GeneralRe: overwriting cells in existing table Pin
Obliterator11-Jul-02 4:31
memberObliterator11-Jul-02 4:31 
Generalexcel template Pin
Johnny007_24/710-Jun-02 8:49
memberJohnny007_24/710-Jun-02 8:49 
General... Pin
Anonymous10-Jun-02 8:19
memberAnonymous10-Jun-02 8:19 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 13 Jan 2000
Article Copyright 2000 by Alexander Mikula
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid