Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to access data in Excel worksheet in VC++?

Status
Not open for further replies.

Jiko

Technical User
Apr 5, 2002
35
SE
Hi!
Have used the code below, and have som questions. Does anyone know more about the line: (marked with ********)
sSql = "SELECT StudentID, Marks FROM MarksDemo";

Whats MarksDemo, is it the tabel in the Excel-file?

Have tried to create a table in Excel (called MarksDemo) using...(2nd place marked with ********)
1) Select the portion of Excel sheet to act as a table
2) Insert->Names->Define
3) Insert->Name->Apply
But it doesnt work, I guess I'm doing it wrong, get a message like:
Microsoft Excel found no references to be replaced.

-Martin

----------------------------------------------------------
Hi,
Consider your worksheet as a database & then try the following code:
void ExcelReadDlg::OnExcelreadButton1()
{
// TODO: Add your control notification handler code here
CDatabase database;
CString sSql;
CString sItem1, sItem2;
CString sDriver;
CString sDsn;
CString sFile="ReadExcel.xls";

m_ctrlList.ResetContent();
sDriver = GetExcelDriver();

if (sDriver.IsEmpty())
{
AfxMessageBox("No Excel ODBC driver found");
return;
}

sDsn.Format("ODBC;DRIVER={%s};DSN=;DBQ=s",sDriver,
sFile);

TRY
{
database.Open(NULL, false, false, sDsn);
CRecordset recset(&database);
*********************************************************
sSql = "SELECT StudentID, Marks FROM MarksDemo";
*********************************************************
recset.Open(CRecordset::forwardOnly, sSql,
CRecordset::readOnly);

while(!recset.IsEOF()) {
recset.GetFieldValue("StudentID", sItem1);
recset.GetFieldValue("Marks",sItem2);
m_ctrlList.AddString(sItem1 + "-->"+sItem2);
recset.MoveNext();
}

database.Close();
}
CATCH(CDBException, e) {
AfxMessageBox("Database error");
}
END_CATCH;
}

CString ExcelReadDlg::GetExcelDriver()
{
char szBuf[2001];
WORD cbBufMax = 2000;
WORD cbBufOut;
char *pszBuf = szBuf;
CString sDriver;

if (!SQLGetInstalledDrivers(szBuf, cbBufMax, &cbBufOut))
return "";

do{
if (strstr(pszBuf, "Excel") != 0) {
//Found
sDriver = CString (pszBuf);
break;
}
pszBuf = strchr (pszBuf, '\0') + 1;
}
while (pszBuf[1] != '\0');

return sDriver;
}
*********************************************************
The above is the modified version of the code I got from other site.
Excel Sheet:
StudentID Marks Rank
000-75-4423 100 1
...... ...... ...
...... ....... ....

1) Select the portion of Excel sheet to act as a table
2) Insert->Names->Define
3) Insert->Name->Apply

Make sure that Excel sheet should not be loaded while you are running the program
*********************************************************
---------------------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top