使用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 (将#修改为@)