#include " to the "StdAfx.h" file; then create the excel file through ODBC; then read the Excel file directly through ODBC That’s it."/> #include " to the "StdAfx.h" file; then create the excel file through ODBC; then read the Excel file directly through ODBC That’s it.">

Home  >  Article  >  Backend Development  >  How to read excel in c++

How to read excel in c++

藏色散人
藏色散人Original
2020-02-19 09:30:006318browse

How to read excel in c++

c++如何读取excel?C++ ODBC操作excel全过程

推荐:《c++教程

想要通过ODBC直接读、写Excel表格文件,首先,应确保ODBC中已安装有Excel表格文件的驱动"MICROSOFT EXCEL DRIVER (*.XLS)"。然后,可根据下面步骤进行:

1. 在StdAfx.h文件中加入:

#include <afxdb.h> 
#include <odbcinst.h>

2. 通过ODBC直接创建Excel文件(暂定文件名:Demo.xls)

//创建并写入Excel文件
void CRWExcel::WriteToExcel()
{
CDatabase database;
CString sDriver = "MICROSOFT EXCEL DRIVER (*.XLS)"; // Excel安装驱动
CString sExcelFile = "c:\\demo.xls"; // 要建立的Excel文件
CString sSql;
TRY
{
// 创建进行存取的字符串
sSql.Format("DRIVER={%s};DSN=&#39;&#39;;FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s",sDriver, sExcelFile, sExcelFile);
// 创建数据库 (既Excel表格文件)
if( database.OpenEx(sSql,CDatabase::noOdbcDialog) )
{
// 创建表结构(姓名、年龄)
sSql = "CREATE TABLE demo (Name TEXT,Age NUMBER)";
database.ExecuteSQL(sSql);
// 插入数值
sSql = "INSERT INTO demo (Name,Age) VALUES (&#39;徐景周&#39;,26)";
database.ExecuteSQL(sSql);
sSql = "INSERT INTO demo (Name,Age) VALUES (&#39;徐志慧&#39;,22)";
database.ExecuteSQL(sSql);
sSql = "INSERT INTO demo (Name,Age) VALUES (&#39;郭徽&#39;,27)";
database.ExecuteSQL(sSql);
} 
// 关闭数据库
database.Close();
}
CATCH_ALL(e)
{
TRACE1("Excel驱动没有安装: %s",sDriver);
}
END_CATCH_ALL;
}

3. 通过ODBC直接读取Excel文件(暂定文件名:Demo.xls)

// 读取Excel文件
void CRWExcel::ReadFromExcel() 
{
CDatabase database;
CString sSql;
CString sItem1, sItem2;
CString sDriver;
CString sDsn;
CString sFile = "Demo.xls"; // 将被读取的Excel文件名
// 检索是否安装有Excel驱动 "Microsoft Excel Driver (*.xls)" 
sDriver = GetExcelDriver();
if (sDriver.IsEmpty())
{
// 没有发现Excel驱动
AfxMessageBox("没有安装Excel驱动!");
return;
}
// 创建进行存取的字符串
sDsn.Format("ODBC;DRIVER={%s};DSN=&#39;&#39;;DBQ=%s", sDriver, sFile);
TRY
{
// 打开数据库(既Excel文件)
database.Open(NULL, false, false, sDsn);
CRecordset recset(&database);
// 设置读取的查询语句.
sSql = "SELECT Name, Age " 
"FROM demo " 
"ORDER BY Name ";
// 执行查询语句
recset.Open(CRecordset::forwardOnly, sSql, CRecordset::readOnly);
// 获取查询结果
while (!recset.IsEOF())
{
//读取Excel内部数值
recset.GetFieldValue("Name ", sItem1);
recset.GetFieldValue("Age", sItem2);
// 移到下一行
recset.MoveNext();
}
// 关闭数据库
database.Close();
}
CATCH(CDBException, e)
{
// 数据库操作产生异常时...
AfxMessageBox("数据库错误: " + e->m_strError);
}
END_CATCH;
}
// 获取ODBC中Excel驱动
CString CRWExcel::GetExcelDriver()
{
char szBuf[2001];
WORD cbBufMax = 2000;
WORD cbBufOut;
char *pszBuf = szBuf;
CString sDriver;
// 获取已安装驱动的名称(涵数在odbcinst.h里)
if (!SQLGetInstalledDrivers(szBuf, cbBufMax, &cbBufOut))
return "";
// 检索已安装的驱动是否有Excel...
do
{
if (strstr(pszBuf, "Excel") != 0)
{
//发现 !
sDriver = CString(pszBuf);
break;
}
pszBuf = strchr(pszBuf, &#39;\0&#39;) + 1;
}
while (pszBuf[1] != &#39;\0&#39;);
return sDriver;
}

更多编程相关内容,请关注php中文网编程入门栏目!

The above is the detailed content of How to read excel in c++. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to define vb arrayNext article:How to define vb array