forked from QtExcel/QXlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
159 lines (125 loc) · 3.88 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// QXlsx
// MIT License
// https://github.com/j2doll/QXlsx
// main.cpp
#include <QtGlobal>
#include <QObject>
#include <QString>
#include <QUrl>
#include <QList>
#include <QVariant>
#include <QDebug>
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <cstdio>
#include <iostream>
#include "xlsxdocument.h"
#include "xlsxchartsheet.h"
#include "xlsxcellrange.h"
#include "xlsxchart.h"
#include "xlsxrichstring.h"
#include "xlsxworkbook.h"
using namespace QXlsx;
#include "XlsxTableModel.h"
#include "DynArray2D.h"
std::string convertFromNumberToExcelColumn(int n);
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute( Qt::AA_EnableHighDpiScaling );
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
QQmlContext* ctxt = engine.rootContext();
QXlsx::Document xlsx( ":/test.xlsx" ); // load xlsx
if (!xlsx.load())
{
qDebug() << "[ERROR] Failed to load xlsx";
return (-1);
}
QList<QString> colTitle; // list of column title
QVector<CellLocation> vcl; // vector of cell(s) location
Worksheet* wsheet = (Worksheet*) xlsx.workbook()->activeSheet();
if ( NULL == wsheet )
{
qDebug() << "[ERROR] Failed to get active sheet";
return (-2);
}
int rowMax = -1;
int colMax = -1;
vcl = wsheet->getFullCells( &rowMax, &colMax );
Q_ASSERT( (-1) != rowMax ); // To CHECK
Q_ASSERT( (-1) != colMax );
for (int ic = 0 ; ic < colMax ; ic++)
{
std::string strCol = convertFromNumberToExcelColumn(ic + 1);
QString colName = QString::fromStdString( strCol );
colTitle.append( colName );
}
// make cell matrix that has (colMax x rowMax) size.
DynArray2D< std::string > dynIntArray(colMax, rowMax);
for ( int icl = 0; icl < vcl.size(); ++icl )
{
CellLocation cl = vcl.at(icl); // cell location
// NOTICE: First cell of tableWidget is 0.
// But first cell of Qxlsx document is 1.
int row = cl.row - 1;
int col = cl.col - 1;
QSharedPointer<Cell> ptrCell = cl.cell; // cell pointer
// value of cell
QVariant var = cl.cell.data()->value();
QString str = var.toString();
// set string value to (col, row)
dynIntArray.setValue( col, row, str.toStdString() );
}
QList<VLIST> xlsxData;
for (int ir = 0; ir < rowMax; ir++ )
{
VLIST vl;
for (int ic = 0; ic < colMax; ic++)
{
std::string value = dynIntArray.getValue( ic, ir );
vl.append( QString::fromStdString(value) );
}
xlsxData.append(vl);
}
// set model for tableview
XlsxTableModel xlsxTableModel(colTitle, xlsxData);
ctxt->setContextProperty( "xlsxModel", &xlsxTableModel );
engine.load( QUrl(QStringLiteral("qrc:/main.qml")) ); // load QML
if ( engine.rootObjects().isEmpty() )
{
qDebug() << "Failed to load qml";
return (-1);
}
int ret = app.exec();
return ret;
}
std::string convertFromNumberToExcelColumn(int n)
{
// main code from https://www.geeksforgeeks.org/find-excel-column-name-given-number/
// Function to print Excel column name for a given column number
std::string stdString;
char str[1000]; // To store result (Excel column name)
int i = 0; // To store current index in str which is result
while ( n > 0 )
{
// Find remainder
int rem = n % 26;
// If remainder is 0, then a 'Z' must be there in output
if ( rem == 0 )
{
str[i++] = 'Z';
n = (n/26) - 1;
}
else // If remainder is non-zero
{
str[i++] = (rem-1) + 'A';
n = n / 26;
}
}
str[i] = '\0';
// Reverse the string and print result
std::reverse( str, str + strlen(str) );
stdString = str;
return stdString;
}