c++ read excel code details

  • 2020-07-21 09:23:13
  • OfStack

How does c++ read excel? C++ ODBC operates the entire excel process

To read and write Excel table files directly from ODBC, first make sure that the Excel table file driver "MICROSOFT EXCEL DRIVER (*.XLS)" is installed in ODBC. Then, follow these steps:

1. Add to the StdAfx. h file:


#include <afxdb.h> 
#include <odbcinst.h>

2. Create Excel files directly from ODBC (working file name: ES25en.xls)


// Create and write Excel file 
void CRWExcel::WriteToExcel()
{
CDatabase database;
CString sDriver = "MICROSOFT EXCEL DRIVER (*.XLS)"; // Excel Install the driver 
CString sExcelFile = "c:\\demo.xls"; //  To establish the Excel file 
CString sSql;
TRY
{
//  Create a string to access 
sSql.Format("DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s",sDriver, sExcelFile, sExcelFile);
//  Creating a database  ( both Excel Table files )
if( database.OpenEx(sSql,CDatabase::noOdbcDialog) )
{
//  Create table structure ( Name and age )
sSql = "CREATE TABLE demo (Name TEXT,Age NUMBER)";
database.ExecuteSQL(sSql);
//  Insert numerical 
sSql = "INSERT INTO demo (Name,Age) VALUES (' Jingzhou xu ',26)";
database.ExecuteSQL(sSql);
sSql = "INSERT INTO demo (Name,Age) VALUES (' Zhi-hui xu ',22)";
database.ExecuteSQL(sSql);
sSql = "INSERT INTO demo (Name,Age) VALUES (' Guo Hui ',27)";
database.ExecuteSQL(sSql);
} 
//  Close the database 
database.Close();
}
CATCH_ALL(e)
{
TRACE1("Excel Driver not installed : %s",sDriver);
}
END_CATCH_ALL;
}

3. Directly read Excel file via ODBC (tentative file name: ES32en.ES33en)


//  read Excel file 
void CRWExcel::ReadFromExcel() 
{
CDatabase database;
CString sSql;
CString sItem1, sItem2;
CString sDriver;
CString sDsn;
CString sFile = "Demo.xls"; //  Will be read Excel The file name 
//  Retrieves if installed Excel drive  "Microsoft Excel Driver (*.xls)" 
sDriver = GetExcelDriver();
if (sDriver.IsEmpty())
{
//  Did not find Excel drive 
AfxMessageBox(" Is not installed Excel drive !");
return;
}
//  Create a string to access 
sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s", sDriver, sFile);
TRY
{
//  Open the database ( both Excel file )
database.Open(NULL, false, false, sDsn);
CRecordset recset(&database);
//  Sets the query statement to read .
sSql = "SELECT Name, Age "
"FROM demo "
"ORDER BY Name ";
//  Execute query statement 
recset.Open(CRecordset::forwardOnly, sSql, CRecordset::readOnly);
//  Get query results 
while (!recset.IsEOF())
{
// read Excel Internal numerical 
recset.GetFieldValue("Name ", sItem1);
recset.GetFieldValue("Age", sItem2);
//  Move to the next 1 line 
recset.MoveNext();
}
//  Close the database 
database.Close();
}
CATCH(CDBException, e)
{
//  When a database operation produces an exception ...
AfxMessageBox(" Database error : " + e->m_strError);
}
END_CATCH;
}
//  To obtain ODBC In the Excel drive 
CString CRWExcel::GetExcelDriver()
{
char szBuf[2001];
WORD cbBufMax = 2000;
WORD cbBufOut;
char *pszBuf = szBuf;
CString sDriver;
//  Gets the name of the installed driver ( Contain the number in odbcinst.h In the )
if (!SQLGetInstalledDrivers(szBuf, cbBufMax, &cbBufOut))
return "";
//  Retrieves whether an installed driver exists Excel...
do
{
if (strstr(pszBuf, "Excel") != 0)
{
// found  !
sDriver = CString(pszBuf);
break;
}
pszBuf = strchr(pszBuf, '\0') + 1;
}
while (pszBuf[1] != '\0');
return sDriver;
}

That is how c++ read excel in detail, I hope this site can help you organize the content.


Related articles: