Easy creation of Excel Sheets using only the ODBC | CodeGuru

Easy creation of Excel Sheets using only the ODBC

Problem Many apps fancy an export function. So wouldn4t it be nice to be able to easily save that result you4ve got there as an Excel sheet? ODBC does make this possible, but there4s one little drawback: Using ODBC the usual way there has to be a registered datasource (DSN) in the ODBC manager. This […]

Written By
CodeGuru Staff
CodeGuru Staff
May 15, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Problem

Many apps fancy an export function. So wouldn4t it be nice to be able to easily save that result you4ve got there as an Excel sheet?

ODBC does make this possible, but there4s 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 you4d have to install that DSN locally on every machine that should support your export function.

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 don4t 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 isn4t installed an exception gets thrown.

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

Needed

In order to get the code below going you have to



  1. have <afxdb.h> included

  2. have an installed ODBC-driver called “MICROSOFT EXCEL DRIVER (*.XLS)”

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.