Click here to Skip to main content
13,899,656 members
Click here to Skip to main content
Add your own
alternative version


106 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

QuestionDelete command odbc Pin
Member 1109944418-Jun-16 16:34
memberMember 1109944418-Jun-16 16:34 
QuestionExcel 2010--not working Pin
diptipanchal13-Oct-15 20:50
memberdiptipanchal13-Oct-15 20:50 
QuestionNeed explanation Pin
TijanaPavlovic26-Dec-13 20:58
memberTijanaPavlovic26-Dec-13 20:58 
QuestionHow can add column in existing table that also contains some record? Pin
Le@rner14-Nov-11 23:07
memberLe@rner14-Nov-11 23:07 
GeneralMy vote of 2 Pin
Mahdi Nejadsahebi22-Oct-11 2:21
memberMahdi Nejadsahebi22-Oct-11 2:21 
GeneralColumn selection Pin
Roger Handwerker10-Jun-11 23:07
memberRoger Handwerker10-Jun-11 23:07 

First of all, I'm starting to check the tool, I found it very good.
I like very much the distribution of info in the scrren, but I would like to make a different field selection for the columns between central column view and the bottom field view; I would like to have all the info in the bottom and much more less information in the "central" column view. Also, I would like to be able to reorder columns on my own, like it is possible in MS Project, for example.
Any help on that ? Would that be good improvements proposal if it is not possible in current version?

Best wishes,

QuestionHow to control the column width? Pin
danlei012020-Apr-11 20:59
memberdanlei012020-Apr-11 20:59 
GeneralData type - custom date Pin
florinvistig14-Jan-10 1:51
memberflorinvistig14-Jan-10 1:51 
GeneralGood Work! Pin
prasad0210-Dec-08 6:16
memberprasad0210-Dec-08 6:16 
GeneralProblem with long data field Pin
yongdiego29-Jul-08 14:13
memberyongdiego29-Jul-08 14:13 
GeneralRe: Problem with long data field Pin
Alexander Tsoi23-Oct-08 23:29
memberAlexander Tsoi23-Oct-08 23:29 
QuestionWriting to Multiple Sheets Pin
Gana15-Nov-07 22:47
memberGana15-Nov-07 22:47 
AnswerRe: Writing to Multiple Sheets Pin
JamesHoward97228-Dec-11 5:43
memberJamesHoward97228-Dec-11 5:43 
General10 for you Pin
IBrana18-Jul-07 17:07
memberIBrana18-Jul-07 17:07 
QuestionAccessing Excel via ADO while the sheet is open Pin
k1n6b0b6-Jun-07 9:35
memberk1n6b0b6-Jun-07 9:35 
QuestionCan I create an Access databae file (.mdb) using the same principle? [modified] Pin
pani6827-Dec-06 22:44
memberpani6827-Dec-06 22:44 
GeneralWhich one first execute please tell me Pin
esambath28-Nov-06 23:02
memberesambath28-Nov-06 23:02 
GeneralI want the code for Ms project Pin
jesukaran john silvester27-Jul-06 19:56
memberjesukaran john silvester27-Jul-06 19:56 
GeneralAdding records PinPopular
Sangeetha Jayaraman19-Apr-06 2:38
memberSangeetha Jayaraman19-Apr-06 2:38 
GeneralMs Excel Pin
Anonymous25-Oct-05 21:18
memberAnonymous25-Oct-05 21:18 
GeneralInsert into A1 Pin
Orgen Kl17-Oct-05 23:27
memberOrgen Kl17-Oct-05 23:27 
Questionwhat to include? Pin
drekon5-Sep-05 9:38
memberdrekon5-Sep-05 9:38 
Questionwhat to include Pin
drekon5-Sep-05 9:38
memberdrekon5-Sep-05 9:38 
Generalso simple so good ! Pin
LeonidPh31-Aug-05 4:02
memberLeonidPh31-Aug-05 4:02 
Generalgood thank u Pin
vikas amin23-Aug-05 21:31
membervikas amin23-Aug-05 21:31 

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 | Cookies | Terms of Use | Mobile
Web01 | 2.8.190306.1 | Last Updated 13 Jan 2000
Article Copyright 2000 by Alexander Mikula
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid