// Retrieve the name of the Excel driver. This is // necessary because Microsoft tends to use language // specific names like "Microsoft Excel Driver (*.xls)" versus // "Microsoft Excel Treiber (*.xls)" sDriver = "Microsoft Excel Driver (*.xls)"; if( sDriver.IsEmpty() ) { // Blast! We didn磘 find that driver! AfxMessageBox("No Excel ODBC driver found"); return; }
// Create a pseudo DSN including the name of the Driver and the Excel file // so we don磘 have to have an explicit DSN installed in our ODBC admin sDsn.Format("ODBC;DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s", sDriver, sFile, sFile); TRY { // Open the database using the former created pseudo DSN database.Open(NULL, FALSE, FALSE, sDsn); //database.OpenEx(sSql,CDatabase::noOdbcDialog);
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 the database database.Close();
} CATCH(CDBException, e) { // A database exception occured. Pop out the details... AfxMessageBox("Database error: "+e->m_strError); } END_CATCH;
CDatabase database;
CString sSql;
CString sDriver;
CString sDsn;
CString sFile = "WriteExcel.xls";
// Retrieve the name of the Excel driver. This is
// necessary because Microsoft tends to use language
// specific names like "Microsoft Excel Driver (*.xls)" versus
// "Microsoft Excel Treiber (*.xls)"
sDriver = "Microsoft Excel Driver (*.xls)";
if( sDriver.IsEmpty() )
{
// Blast! We didn磘 find that driver!
AfxMessageBox("No Excel ODBC driver found");
return;
}
// Create a pseudo DSN including the name of the Driver and the Excel file
// so we don磘 have to have an explicit DSN installed in our ODBC admin
sDsn.Format("ODBC;DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s",
sDriver, sFile, sFile); TRY
{
// Open the database using the former created pseudo DSN
database.Open(NULL, FALSE, FALSE, sDsn);
//database.OpenEx(sSql,CDatabase::noOdbcDialog);
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 the database
database.Close();
}
CATCH(CDBException, e)
{
// A database exception occured. Pop out the details...
AfxMessageBox("Database error: "+e->m_strError);
}
END_CATCH;