使用CDATA Base/ODBC驱动程序获取excel文件表名
我正在使用OBDC使用CDATA和excel驱动程序从一些excel文件导入数据。 代码是这样的:使用CDATA Base/ODBC驱动程序获取excel文件表名,excel,visual-studio-2010,visual-c++,mfc,cdatabase,Excel,Visual Studio 2010,Visual C++,Mfc,Cdatabase,我正在使用OBDC使用CDATA和excel驱动程序从一些excel文件导入数据。 代码是这样的: CString GetExcelDriver() { TCHAR szBuf[2001]; WORD cbBufMax = 2000; WORD cbBufOut; TCHAR *pszBuf = szBuf; CString sDriver; // Get the names of the installed drivers ("odbc
CString GetExcelDriver()
{
TCHAR szBuf[2001];
WORD cbBufMax = 2000;
WORD cbBufOut;
TCHAR *pszBuf = szBuf;
CString sDriver;
// Get the names of the installed drivers ("odbcinst.h" has to be included )
if(!SQLGetInstalledDrivers(szBuf,cbBufMax,& cbBufOut))
return _T("");
// Search for the driver...
do
{
if(_tcsstr( pszBuf, _T("Excel")) != 0 )
{
// Found !
return pszBuf;
break;
}pszBuf = _tcschr( pszBuf, _T('\0')) + 1;
}while( pszBuf[1] != _T('\0'));
return _T("");
}
void ImportData()
{
CString strExcelPath = _T("excel_file.xls");
CString strSQL;
CString strValue;
strSQL.Format(_T("DRIVER={%s};DSN='';DBQ=%s")
, GetExcelDriver()
, strExcelPath);
if(dbFile.Open(NULL,false,false,strSQL))
{
CRecordset rs(&dbFile);
rs.Open(CRecordset::forwardOnly, _T("SELECT * FROM table_name"), CRecordset::readOnly);
int nColumnCount = rs.m_nResultCols;
while(!rs.IsEOF())
{
try
{
rs.GetFieldValue((short)0, strValue);
rs.GetFieldValue((short)1, strValue);
/*
rs.GetFieldValue((short)2, strValue);
rs.GetFieldValue((short)3, strValue);
rs.GetFieldValue((short)4, strValue);
.
.
.
*/
}
catch(CException* pEx)
{
// DB Exception
}
rs.MoveNext();
}
}
}
但要这样做,我需要excel工作表名(表名)。只要我保持硬编码,它是好的,但在用户文件的情况下,它将无法工作。经过长时间的谷歌搜索,没有找到可以让我得到工作表名称的。如果有人建议从excel文件中获取工作表名称,我将不胜感激。excel表格可以指定名称。这些名称不必与图纸名称匹配。您询问的是错误问题的解决方案。@I非常感谢您的输入。如果是这种情况,我如何才能得到表的名称。我认为仍然有一些方法可以获得第一个表的名称。Excel有一个自动化界面,允许您发现几乎所有的文档内容。或者使用能够理解.xls文件的第三方库。