Click here to Skip to main content
Licence 
First Posted 12 Jan 2000
Views 220,100
Bookmarked 99 times

Creating Excel Sheets using ODBC

By Alexander Mikula | 12 Jan 2000
Writing to Excel spreadsheets using only ODBC
 
Part of The SQL Zone sponsored by
See Also
1 vote, 5.0%
1
1 vote, 5.0%
2
1 vote, 5.0%
3
4 votes, 20.0%
4
13 votes, 65.0%
5
4.59/5 - 48 votes
1 removed
μ 4.44, σa 1.79 [?]

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 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

About the Author

Alexander Mikula



Austria Austria

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow can add column in existing table that also contains some record? PinmemberLe@rner0:07 15 Nov '11  
GeneralMy vote of 2 PingroupMahdi Nejadsahebi3:21 22 Oct '11  
GeneralColumn selection PinmemberRoger Handwerker0:07 11 Jun '11  
QuestionHow to control the column width? Pinmemberdanlei012021:59 20 Apr '11  
GeneralData type - custom date Pinmemberflorinvistig2:51 14 Jan '10  
GeneralGood Work! Pinmemberprasad027:16 10 Dec '08  
GeneralProblem with long data field Pinmemberyongdiego15:13 29 Jul '08  
GeneralRe: Problem with long data field PinmemberAlexander Tsoi0:29 24 Oct '08  
QuestionWriting to Multiple Sheets Pinmembergana23:47 15 Nov '07  
AnswerRe: Writing to Multiple Sheets PinmemberJamesHoward9726:43 28 Dec '11  
General10 for you PinmemberIBrana18:07 18 Jul '07  
QuestionAccessing Excel via ADO while the sheet is open Pinmemberk1n6b0b10:35 6 Jun '07  
QuestionCan I create an Access databae file (.mdb) using the same principle? [modified] Pinmemberpani6823:44 27 Dec '06  
GeneralWhich one first execute please tell me Pinmemberesambath0:02 29 Nov '06  
GeneralI want the code for Ms project Pinmemberjesukaran john silvester20:56 27 Jul '06  
GeneralAdding records PinmemberSangeetha Jayaraman3:38 19 Apr '06  
GeneralMs Excel PinsussAnonymous22:18 25 Oct '05  
GeneralInsert into A1 PinmemberJuergen Klingler0:27 18 Oct '05  
Questionwhat to include? Pinmemberdrekon10:38 5 Sep '05  
Questionwhat to include Pinmemberdrekon10:38 5 Sep '05  
Generalso simple so good ! PinmemberLeonidPh5:02 31 Aug '05  
Generalgood thank u Pinmembervikas amin22:31 23 Aug '05  
Questionhow to insert more than one table in same sheet Pinsussshsak23:49 6 May '04  
AnswerRe: how to insert more than one table in same sheet Pinmemberphucid18:06 9 Oct '05  
Questionchange font style and colors in spreadsheet? Pinsussxxhimanshu20:11 23 Feb '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.5.120210.1 | Last Updated 13 Jan 2000
Article Copyright 2000 by Alexander Mikula
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid