2018-11-28 17:28:51 +09:00
|
|
|
// main.cpp
|
2018-12-29 21:57:10 +09:00
|
|
|
// QXlsx // MIT License // https://github.com/j2doll/QXlsx
|
2018-11-28 17:28:51 +09:00
|
|
|
//
|
|
|
|
|
|
|
|
#include <QtGlobal>
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QtCore>
|
|
|
|
#include <QVariant>
|
2018-12-28 21:10:21 +09:00
|
|
|
#include <QDir>
|
2018-11-28 17:28:51 +09:00
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
using namespace std;
|
|
|
|
|
2018-12-30 17:30:10 +09:00
|
|
|
// [0] include QXlsx headers
|
2018-11-28 17:28:51 +09:00
|
|
|
#include "xlsxdocument.h"
|
|
|
|
#include "xlsxchartsheet.h"
|
|
|
|
#include "xlsxcellrange.h"
|
|
|
|
#include "xlsxchart.h"
|
|
|
|
#include "xlsxrichstring.h"
|
|
|
|
#include "xlsxworkbook.h"
|
|
|
|
using namespace QXlsx;
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2018-12-30 17:30:10 +09:00
|
|
|
QCoreApplication app(argc, argv);
|
2018-11-28 17:28:51 +09:00
|
|
|
|
2018-12-30 17:30:10 +09:00
|
|
|
// [1] Writing excel file(*.xlsx)
|
2018-11-28 17:28:51 +09:00
|
|
|
QXlsx::Document xlsxW;
|
|
|
|
xlsxW.write("A1", "Hello Qt!"); // write "Hello Qt!" to cell(A,1). it's shared string.
|
2018-12-29 21:57:10 +09:00
|
|
|
if ( xlsxW.saveAs("Test.xlsx") ) // save the document as 'Test.xlsx'
|
2018-12-28 21:10:21 +09:00
|
|
|
{
|
|
|
|
qDebug() << "[debug] success to write xlsx file";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qDebug() << "[debug][error] failed to write xlsx file";
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug() << "[debug] current directory is " << QDir::currentPath();
|
2018-11-28 17:28:51 +09:00
|
|
|
|
2018-12-30 17:30:10 +09:00
|
|
|
// [2] Reading excel file(*.xlsx)
|
2018-12-30 17:37:39 +09:00
|
|
|
Document xlsxR("Test.xlsx");
|
|
|
|
if ( xlsxR.load() ) // load excel file
|
2018-12-30 17:30:10 +09:00
|
|
|
{
|
2018-12-28 21:10:21 +09:00
|
|
|
qDebug() << "[debug] success to load xlsx file.";
|
|
|
|
|
2018-12-30 17:30:10 +09:00
|
|
|
int row = 1; int col = 1;
|
2018-11-28 17:28:51 +09:00
|
|
|
Cell* cell = xlsxR.cellAt(row, col); // get cell pointer.
|
|
|
|
if ( cell != NULL )
|
|
|
|
{
|
|
|
|
QVariant var = cell->readValue(); // read cell value (number(double), QDateTime, QString ...)
|
2018-12-29 21:57:10 +09:00
|
|
|
qDebug() << "[debug] cell(1,1) is " << var; // Display value. It is 'Hello Qt!'.
|
2018-11-28 17:28:51 +09:00
|
|
|
}
|
2018-12-28 21:10:21 +09:00
|
|
|
else
|
|
|
|
{
|
|
|
|
qDebug() << "[debug][error] cell(1,1) is not set.";
|
|
|
|
}
|
2018-12-30 17:30:10 +09:00
|
|
|
}
|
2018-12-28 21:10:21 +09:00
|
|
|
else
|
|
|
|
{
|
|
|
|
qDebug() << "[debug][error] failed to load xlsx file.";
|
|
|
|
}
|
2018-12-30 17:30:10 +09:00
|
|
|
|
|
|
|
return 0;
|
2018-11-28 17:28:51 +09:00
|
|
|
}
|