Click here to Skip to main content
15,867,686 members
Articles / Database Development / SQL Server
Article

Creating Excel Sheets using ODBC

Rate me:
Please Sign up or sign in to vote.
4.69/5 (25 votes)
12 Jan 2000 341.4K   106   42
Writing to Excel spreadsheets using only ODBC

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;
    
  TRY
  {
    // Build the creation string for access without DSN
       
    sSql.Format("DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s",
                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)";
      database.ExecuteSQL(sSql);

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

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

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

    // Close database
    database.Close();
  }
  CATCH_ALL(e)
  {
    TRACE1("Driver not installed: %s",sDriver);
  }
  END_CATCH_ALL;
}

License

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


Written By
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralODBC excel driver Pin
Gianluca Nastasi23-Mar-03 23:25
Gianluca Nastasi23-Mar-03 23:25 
GeneralRe: ODBC excel driver Pin
Herschel S. Krustofsky11-Apr-03 8:55
Herschel S. Krustofsky11-Apr-03 8:55 
GeneralPlain C Version Available Pin
Anonymous19-Feb-03 4:08
Anonymous19-Feb-03 4:08 
GeneralRe: Plain C Version Available Pin
AghaKhan18-Nov-05 15:34
AghaKhan18-Nov-05 15:34 
GeneralRe: Plain C Version Available Pin
liaohaiwen10-Apr-08 19:10
liaohaiwen10-Apr-08 19:10 
GeneralStoring date and time values in Excel using ODBC Pin
Klaus Kurt9-Jan-03 5:41
Klaus Kurt9-Jan-03 5:41 
Generalsingle quote mark Pin
9-Jul-02 23:34
suss9-Jul-02 23:34 
GeneralRe: single quote mark Pin
oneleg14-Mar-05 1:46
oneleg14-Mar-05 1:46 
Generaloverwriting cells in existing table Pin
26-Jun-02 1:26
suss26-Jun-02 1:26 
GeneralRe: overwriting cells in existing table Pin
Obliterator11-Jul-02 3:31
Obliterator11-Jul-02 3:31 
Generalexcel template Pin
10-Jun-02 7:49
suss10-Jun-02 7:49 
General... Pin
10-Jun-02 7:19
suss10-Jun-02 7: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.