如何解决使用CDatabase / ODBC驱动程序获取Excel文件表名称
我正在使用OBDC使用CDatabase和Excel驱动程序从某些Excel文件导入数据。 代码是这样的:
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,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工作表名称(table_name)。只要我保持该硬编码,就可以了,但是对于用户文件,它将不起作用。经过漫长的谷歌搜索,找不到能使我获得工作表名称的地方。如果有人建议从excel文件中获取工作表名称,我将不胜感激。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。