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
*********************************************************
---------------------------------------------------------
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
*********************************************************
---------------------------------------------------------