libxl库读取excel文件,遍历excel中的所有表和表中所有元素

下面的操作在libxl官网上都有,还有一些其他操作,网站链接LibXL documentation

1,  读取xlsx文件时,提供的例子xlCreateBook使用例子有问题:

    读取xlsx(2003excel以后)xlCreateXMLBookA

    xls文件使用xlCreateBookA

2, 下面代码的功能是遍历一个excel中所有的元素.除去第一行,因为我的excel第一行是备注,在实际计算时不会用到

int main()
{
	Book* book = xlCreateBook();
	book->setKey("Halil Kural", "windows-2723210a07c4e90162b26966a8jcdboe");
	if (book)
	{
		if (book->load("1.xls"))
		{

			Sheet* sheet = book->getSheet(1);
			if (sheet)
			{
				int firstRow = sheet->firstRow();
				int lastRow = sheet->lastRow();
				int firstCol = sheet->firstCol();
				int lastCol = sheet->lastCol();
				
				for (int row = firstRow + 1; row < lastRow; ++row)
				{
					for (int c = firstCol; c < lastCol; ++c)
					{
						double db = 0;
						CellType t = sheet->cellType(row, c);
						if (row ==1)
							std::cout << firstRow << "\t" << lastRow << "\t" << sheet->readNum(row, c) << endl;

						switch (t)
						{
						case libxl::CELLTYPE_EMPTY:
							break;
						case libxl::CELLTYPE_NUMBER:
							db = sheet->readNum(row, c);
							if (row == 20)
							std::cout << row << "\t" << c << "\t" << db << endl;
							break;
						case libxl::CELLTYPE_STRING:
							break;
						case libxl::CELLTYPE_BOOLEAN:
							break;
						case libxl::CELLTYPE_BLANK:
							break;
						case libxl::CELLTYPE_ERROR:
							break;
						default:
							break;
						}
					}
				}

			}
		}
		else
		{
			std::cout << "At first run generate !" << std::endl;
		}

		book->release();
	}

	system("pause");
	return 0;
}

在使用官网未购买版本时,如果excel大小超过一定规模就会出现无法读取的情况,需要购买完整版或进行破解。

破解版本我自己试了下:

win32版本:libxl破解版,亲测可用-C++代码类资源-CSDN下载

win64版本:libxl.3.6.5  在创建成功book时,调用

        book->setKey("Halil Kural", "windows-2723210a07c4e90162b26966a8jcdboe");

       就可以使用完整版功能了,在libxl其他版本是否能使用还未测试。 

大家有问题可以留言,交流

下面的代码是我自己做备份的

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <iomanip>
#include <time.h>
#include <conio.h>
#include "libxl.h"

using namespace libxl;
using namespace std;

int main()
{
	Book* book = xlCreateXMLBookA();
	if (book)
	{
		if (book->load("C:\\Users\\lenovo\\Desktop\\TestData.xls"))
		{
			
			Sheet* sheet= book->getSheet(2);
			if (sheet)
			{
				const char* s = sheet->readStr(0, 5);
				if (s) 
					std::wcout << s << std::endl << std::endl;

				const char* f = sheet->readFormula(0, 5);
				if (f) 
					std::wcout << f << std::endl << std::endl;
			}
		}
		else
		{
			std::cout << "At first run generate !" << std::endl;
		}

		book->release();
	}

	system("pause");
	return 0;
}

不知为什么,代码对不齐,将就着看吧

你可以使用C++的第三方读取和操作Excel文件。常用的有libxlExcelFormat和OpenXLSX。这里以libxl为例,介绍如何遍历多个Excel文件和多个工作表。 首先,你需要在你的C++项目添加libxl,并包含头文件: ```c++ #include "libxl.h" using namespace libxl; ``` 然后,遍历多个Excel文件,可以使用以下代码: ```c++ int main() { Book* book = nullptr; std::vector<std::string> excelFiles = {"file1.xlsx", "file2.xlsx"}; // 多个Excel文件 for (const auto& excelFile : excelFiles) { book = xlCreateXMLBook(); if (book->load(excelFile.c_str())) { int sheetCount = book->sheetCount(); for (int i = 0; i < sheetCount; i++) { Sheet* sheet = book->getSheet(i); // 处理当前工作表 // ... } } book->release(); } return 0; } ``` 遍历Excel文件的多个工作表,可以在遍历Excel文件的循环再嵌套一个循环: ```c++ int main() { Book* book = nullptr; std::vector<std::string> excelFiles = {"file.xlsx"}; for (const auto& excelFile : excelFiles) { book = xlCreateXMLBook(); if (book->load(excelFile.c_str())) { int sheetCount = book->sheetCount(); for (int i = 0; i < sheetCount; i++) { Sheet* sheet = book->getSheet(i); int rowCount = sheet->lastRow(); int columnCount = sheet->lastCol(); for (int row = 0; row < rowCount; row++) { for (int col = 0; col < columnCount; col++) { const char* cellValue = sheet->readStr(row, col); // 处理当前单元格 // ... } } } } book->release(); } return 0; } ``` 以上代码可以读取Excel的所有单元格,你可以根据需要修改代码,只读取某些单元格。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值