一、上代码
#include "libxl.h"
#include <QDebug>
#include <QString>
#include <QElapsedTimer>
using namespace libxl;
int main()
{
QString filePath=QString::fromWCharArray(L"./Para.xls");
Book *book = NULL;
if(filePath.endsWith("xls", Qt::CaseInsensitive)) {
book = xlCreateBook();
} else if(filePath.endsWith("xlsx", Qt::CaseInsensitive)) {
book = xlCreateXMLBook();
}
QElapsedTimer timer;
if (book) {
timer.start();
if (book->load(L"./Para.xls")) {
qDebug()<<"loadUseTime"<<timer.elapsed();timer.restart();
int sheetCount = book->sheetCount();
qDebug()<<sheetCount;
for (int sheetIndex = 0; sheetIndex < sheetCount; ++sheetIndex) {
Sheet* sheet = book->getSheet(sheetIndex);
if (sheet) {
qDebug()
<< "Sheet:"
<< QString::fromWCharArray(sheet->name())<<"\n"
<<"row:"<<sheet->lastRow()
<<" , col"<<sheet->lastCol();
int rowCount = sheet->lastRow();
int colCount = sheet->lastCol();
for (int row = 0; row <= rowCount; ++row) {
for (int col = 0; col <= colCount; ++col) {
CellType cellType = sheet->cellType(row, col);
switch (cellType) {
case CELLTYPE_EMPTY:
qDebug() << "Row:" << row << ", Col:" << col << " - Empty cell";
break;
case CELLTYPE_NUMBER:
qDebug() << "Row:" << row << ", Col:" << col << " - Number:" << sheet->readNum(row, col);
break;
case CELLTYPE_STRING:
qDebug() << "Row:" << row << ", Col:" << col << " - String:" << QString::fromStdWString( sheet->readStr(row, col));
break;
default:
qDebug() << "Row:" << row << ", Col:" << col << " - Unknown cell type";
break;
}
}
}
} else {
qDebug() << "Sheet not found.";
}
}
book->release();
} else {
qDebug() << "Failed to load Excel file.";
}
} else {
qDebug() << "Failed to create LibXL book.";
}
return 0;
}