注:以下绝大部分图片,代码来自LIBXL官网,http://www.libxl.com libxl版本 : version 3.8.4
完整代码参考:https://download.csdn.net/download/weixin_47753171/88618929
一、配置环境
参考:50.1如何通过类库读写Excel(未安装Excel也可以读写)-CSDN博客
二、代码分析
1、xlCreateBook()和xlCreateXMLBook()区分
/*创建用于处理 XLS 文件的书籍实例。应首先调用此函数以接收书籍指针。此函数和其他类位于 libxl 命名空间中。2007以下*/
Book* book = xlCreateBook();
/*创建用于处理 XLSX 文件的书籍实例。应首先调用此函数以接收书籍指针。 此函数和其他类位于 libxl 命名空间中。*/
Book* book = xlCreateXMLBook();
2、load()函数
/*将整个文件加载到内存中。指定一个临时文件以减少内存消耗。如果发生错误,则返回 false。使用 Book::errorMessage() 获取错误信息*/
//load(stringfilepath);stringfilepath为输入Excel的路径
bool load(const wchar_t* filename, const wchar_t* tempFile = 0);
3、getSheet()函数
/*获取指向具有指定索引的工作表的指针。如果使用Book::loadSheet()或Book:: loadpartial()方法,则使用索引0。应该小于Book::sheetCount()方法的返回值。如果发生错误,返回NULL。使用Book::errorMessage()获取错误信息。*/
index=0时为第一个工作表
Sheet* getSheet(int index) const
4、firstRow()第一行,lastRow()第二行;firstCol()第一列; lastCol()第二列
int firstRow() const
//返回包含已使用单元格(包括仅具有格式的空白单元格)的工作表中第一行的从零开始的索引。
int lastRow()常量
//返回工作表中包含已使用单元格(包括仅具有格式的空白单元格)的最后一行之后的行从零开始的索引。
int firstCol() const
//返回包含已使用单元格(包括仅具有格式的空白单元格)的工作表中第一列的从零开始的索引。
int lastCol() const
//返回工作表中包含已使用单元格(包括仅具有格式的空白单元格)的最后一列之后的列的从零开始的索引。
5、cellType()函数
CellType cellType(int row, int col) const
/*返回单元格的类型。
细胞类型值说明
celltype_empty为空,表示单元格不存在
celltype_number数值
celltype_string字符串值
celltype_boolean布尔值
celltype_blank空白,该单元格只包含格式信息
celltype_error错误*/
6、isFormula()函数
bool isFormula(int row, int col) const
//检查单元格是否包含公式。
7、readFormula()函数
const wchar_t* readFormula(int row, int col, Format** Format = 0)
//从单元格中读取公式字符串及其格式。如果指定的单元格不包含公式或发生错误,则返回NULL。使用Book::errorMessage()获取错误信息。
8、<表达式1>?<表达式2>:<表达式3>这是C++种唯一一个三目运算符。
如果表达式1成立,则输出表达式2的值,否则输出表达式3的值。
9、release()函数
void release()
删除该对象并释放资源。
三、整体代码
#include <iostream>
#include "libxl.h"
#include <string>
#pragma comment(lib,"libxl.lib")
using namespace libxl;
using namespace std;
int main()
{
//
Book *book = xlCreateXMLBook();//2007以下的xlCreateBook
book->setKey("Michael Hillier", "windows-222329040ec5ec046fb46767a7h1gej6");
if (!book)
{
printf("创建实例失败", 1);
return 0;
}
if (book->load("D:\\ug\\TKLTOOLS\\Date\\AAA.xlsx"))
{
Sheet* sheet = book->getSheet(0);
if (sheet)
{
for (int row = sheet->firstRow(); row < sheet->lastRow(); ++row)
{
for (int col = sheet->firstCol(); col < sheet->lastCol(); ++col)
{
CellType cellType = sheet->cellType(row, col);
std::cout << "(" << row << ", " << col << ") = ";
if (sheet->isFormula(row, col))
{
string s = sheet->readFormula(row, col);
std::cout << (s.c_str() ? s.c_str() : "null") << " [formula]";
}
else
{
switch (cellType)
{
case CELLTYPE_EMPTY: std::cout << "[empty]"; break;
case CELLTYPE_NUMBER:
{
double d = sheet->readNum(row, col);
std::cout << d << " [number]";
break;
}
case CELLTYPE_STRING:
{
string s = sheet->readStr(row, col);
std::cout << (s.c_str() ? s.c_str() : "null") << " [string]";
break;
}
case CELLTYPE_BOOLEAN:
{
bool b = sheet->readBool(row, col);
std::cout << (b ? "true" : "false") << " [boolean]";
break;
}
case CELLTYPE_BLANK: std::cout << "[blank]"; break;
case CELLTYPE_ERROR: std::cout << "[error]"; break;
}
}
std::cout << std::endl;
}
}
}
}
book->release();
return 0;
}
四、运行结果