Guest_imported
New member
- Jan 1, 1970
- 0
How do I display an excel spreadsheet in a Microsft DataGrid Control in a MFC application? I have put a DataGrid and a Microsft ADO Data Control on my dialog. The code below is called when a button on my dialog is clicked. The variable m_adoSource is the ADO Data Control, m_dataGrid is the DataGrid. The connection is made to the excel spreadsheet but I cannot get it to display in the DataGrid. How do I do this?
Code:
void CCleaningToolDlg::OnButtonOpenFile()
{
// TODO: Add your control notification handler code here
//
// get file name from user using standard file dialog
//
//
// Connect to Excel spreadsheet
//
CString path = "E:\\CleaningTool\\TEST FILE.xls";
CString sDriver = GetExcelDriver();
CString connectionString ("Provider=MSDASQL; Driver={" + sDriver + "};" + path);
m_adoSource.SetConnectionString(connectionString);
//m_adoSource.SetRecordSource("SELECT *");
// DEBUG stuff
CString temp = m_adoSource.GetConnectionString();
MessageBox(temp);
//
// Populate grid control with contents of spreadsheet
//
C_Recordset recordSet = m_adoSource.GetRecordset();
m_adoSource.SetRecordSource("SELECT * ");
m_adoSource.Refresh();
m_dataGrid.UpdateData();
m_dataGrid.Refresh();
}
// Get the name of the Excel-ODBC driver
// Contibuted by Christopher W. Backen - Thanx Christoper
CString CCleaningToolDlg::GetExcelDriver()
{
char szBuf[2001];
WORD cbBufMax = 2000;
WORD cbBufOut;
char *pszBuf = szBuf;
CString sDriver;
// Get the names of the installed drivers
// (<odbcinst.h> and <afxdb.h> has to be included )
if (!SQLGetInstalledDrivers(szBuf, cbBufMax, &cbBufOut))
return "";
// Search for the driver...
do
{
if (strstr(pszBuf, "Excel") != 0)
{
// Found !
sDriver = CString(pszBuf);
break;
}
pszBuf = strchr(pszBuf, '\0') + 1;
}
while (pszBuf[1] != '\0');
return sDriver;
}