vs2019通过libxl使用数组指针读入excel
时间: 2023-07-16 15:12:46 浏览: 214
首先,需要下载并安装 libxl 库,并将其添加到项目中。接下来,可以使用以下代码来读取 Excel 文件中的数据并存储在数组指针中:
```c++
#include <iostream>
#include "libxl.h"
using namespace libxl;
int main() {
Book* book = xlCreateBook();
if (book) {
if (book->load("example.xlsx")) {
Sheet* sheet = book->getSheet(0);
if (sheet) {
int numRows = sheet->lastRow();
int numCols = sheet->lastCol();
int** data = new int*[numRows];
for (int i = 0; i < numRows; ++i) {
data[i] = new int[numCols];
for (int j = 0; j < numCols; ++j) {
data[i][j] = sheet->readNum(i + 1, j + 1);
}
}
// 打印数据
for (int i = 0; i < numRows; ++i) {
for (int j = 0; j < numCols; ++j) {
std::cout << data[i][j] << " ";
}
std::cout << std::endl;
}
// 释放内存
for (int i = 0; i < numRows; ++i) {
delete[] data[i];
}
delete[] data;
}
}
book->release();
}
return 0;
}
```
该代码首先创建一个 `Book` 对象,然后加载 Excel 文件。接着,它获取第一个工作表,并计算行数和列数。然后,它创建一个二维数组,并使用 `Sheet::readNum()` 方法读取每个单元格的数据并存储在数组中。最后,它打印数组中的数据并释放内存。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![PRG](https://img-home.csdnimg.cn/images/20250102104920.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![md](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)