5,316,172 members and growing! (18,506 online)
Email Password   helpLost your password?
Database » Database » General     Intermediate

Creating Excel Sheets using ODBC

By Alexander Mikula

Writing to Excel spreadsheets using only ODBC
SQL, VC6, C++Windows, NT4, SQL Server, Visual Studio, DBA, Dev

Posted: 12 Jan 2000
Updated: 12 Jan 2000
Views: 149,848
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
45 votes for this Article.
Popularity: 7.37 Rating: 4.46 out of 5
1 vote, 5.9%
1
0 votes, 0.0%
2
1 vote, 5.9%
3
4 votes, 23.5%
4
11 votes, 64.7%
5

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



Location: Austria Austria

Other popular Database articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 30 (Total in Forum: 30) (Refresh)FirstPrevNext
Subject  Author Date 
QuestionWriting to Multiple Sheetsmembergana23:47 15 Nov '07  
General10 for youmemberIBrana18:07 18 Jul '07  
QuestionAccessing Excel via ADO while the sheet is openmemberk1n6b0b10:35 6 Jun '07  
GeneralCan I create an Access databae file (.mdb) using the same principle? [modified]memberpani6823:44 27 Dec '06  
GeneralWhich one first execute please tell mememberesambath0:02 29 Nov '06  
GeneralI want the code for Ms projectmemberjesukaran john silvester20:56 27 Jul '06  
GeneralAdding recordsmemberSangeetha Jayaraman3:38 19 Apr '06  
GeneralMs ExcelsussAnonymous22:18 25 Oct '05  
GeneralInsert into A1memberJuergen Klingler0:27 18 Oct '05  
Generalwhat to include?memberdrekon10:38 5 Sep '05  
Generalwhat to includememberdrekon10:38 5 Sep '05  
Generalso simple so good !memberLeonidPh5:02 31 Aug '05  
Generalgood thank umembervikas amin22:31 23 Aug '05  
Generalhow to insert more than one table in same sheetsussshsak23:49 6 May '04  
GeneralRe: how to insert more than one table in same sheetmemberphucid18:06 9 Oct '05  
Generalchange font style and colors in spreadsheet?sussxxhimanshu20:11 23 Feb '04  
GeneralWorks, but why not this?:memberkleinb21:33 21 Apr '03  
GeneralRe: Works, but why not this?:memberpolofreak0:46 8 Jun '05  
GeneralODBC excel driversussGianluca Nastasi0:25 24 Mar '03  
GeneralRe: ODBC excel drivermemberHerschel S. Krustofsky9:55 11 Apr '03  
GeneralPlain C Version AvailablesussAnonymous5:08 19 Feb '03  
GeneralRe: Plain C Version AvailablememberBilloKhan16:34 18 Nov '05  
GeneralRe: Plain C Version Availablememberliaohaiwen20:10 10 Apr '08  
GeneralStoring date and time values in Excel using ODBCmemberKlaus Kurt6:41 9 Jan '03  
Generalsingle quote markmemberMartin Step0:34 10 Jul '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 Jan 2000
Editor: Chris Maunder
Copyright 2000 by Alexander Mikula
Everything else Copyright © CodeProject, 1999-2008
Web09 | Advertise on the Code Project