下面的操作在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;
}
不知为什么,代码对不齐,将就着看吧