关于用ODBC读取excel文件的问题,当某列既有数字又有字符串时

flysky_zhou 2009-04-11 10:50:45
用ODBC读取excel文件时,该excel的某列既有数字又有字符串

如该列的第一行里是数字
该列的第二行里是字符串时

读取第一行数据能正确读取到

但在读取第二行时就不能获取到该单元格的内容

请各位大大指点指点,谢谢了
...全文
321 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
flysky_zhou 2009-04-12
  • 打赏
  • 举报
回复
去那边发了

自己顶下
  • 打赏
  • 举报
回复
楼主,发去数据库版问问吧。
flysky_zhou 2009-04-11
  • 打赏
  • 举报
回复
连接字符串

m_Database = new CDatabase;
GetExcelDriver();
m_sDsn.Format("DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s", m_sExcelDriver, m_sFile, m_sFile);
flysky_zhou 2009-04-11
  • 打赏
  • 举报
回复

bool CSpreadSheet::Open()
{
if (m_bExcel) // If file is an Excel spreadsheet
{
m_Database->OpenEx(m_sDsn, CDatabase::noOdbcDialog);

// Open Sheet
m_rSheet = new CRecordset( m_Database );
m_sSql.Format("SELECT * FROM [%s$A1:IV65536]", m_sSheetName);
try
{
m_rSheet->Open(CRecordset::forwardOnly, m_sSql, CRecordset::readOnly);
}
catch(...)
{
delete m_rSheet;
m_rSheet = NULL;
m_Database->Close();
return false;
}

// Get number of columns
m_dTotalColumns = m_rSheet->m_nResultCols;

if (m_dTotalColumns != 0)
{
m_aRows.RemoveAll();
m_stempString.Empty();
m_bAppend = true;
m_dTotalRows++; // Keep count of total number of rows

// Get field names i.e header row
for (int i = 0; i < m_dTotalColumns; i++)
{
m_stempSql = m_rSheet->m_rgODBCFieldInfos[i].m_strName;
m_aFieldNames.Add(m_stempSql);

// Join up all the columns into a string
if (i != m_dTotalColumns-1) // Not last column
{
m_stempString = m_stempString + "\"" + m_stempSql + "\"" + m_sSeparator;
}
else // Last column
{
m_stempString = m_stempString + "\"" + m_stempSql + "\"";
}
}

// Store the header row as the first row in memory
m_aRows.Add(m_stempString);

// Read and store the rest of the rows in memory
while (!m_rSheet->IsEOF())
{
m_dTotalRows++; // Keep count of total number of rows
try
{
// Get all the columns in a row
m_stempString.Empty();
for (short column = 0; column < m_dTotalColumns; column++)
{
//主要是这里,当某列的数据既有数字又有字符串时
m_rSheet->GetFieldValue(column, m_stempSql);

// Join up all the columns into a string
if (column != m_dTotalColumns-1) // Not last column
{
m_stempString = m_stempString + "\"" + m_stempSql + "\"" + m_sSeparator;
}
else // Last column
{
m_stempString = m_stempString + "\"" + m_stempSql + "\"";
}
}

// Store the obtained row in memory
m_aRows.Add(m_stempString);
m_rSheet->MoveNext();
}
catch (...)
{
m_sLastError = "Error reading row\n";
delete m_rSheet;
m_rSheet = NULL;
m_Database->Close();
return false;
}
}
}

m_rSheet->Close();
delete m_rSheet;
m_rSheet = NULL;
m_Database->Close();
m_dCurrentRow = 1;
return true;
}


这个ODBC读取excel的代码我也是在csdn上面找到的
当excel的内容如下时:
第一行为列头 测试1 测试2 测试3
1 2 3
a 22 33

读取第二行1、2、3时是没有问题的
当读取第三行的第一个单元格的内容时就获取不到,其它的都正常

是不是用ODBC读取excel时某列的内容的类型要完全一致,要么都是数字要么都是字符串???????
mengde007 2009-04-11
  • 打赏
  • 举报
回复
不好意思,帮顶了
「已注销」 2009-04-11
  • 打赏
  • 举报
回复
给下代码。
你这么问,恐怕很难回答!

一些可能:
读取后是否释放了某些指针?
读取时buf是否足够大?
是否涉及Unicode/ANSI问题?

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧