1
0
mirror of https://github.com/QtExcel/QXlsx.git synced 2025-01-30 05:02:52 +08:00

update example : copycat, helloandroid, webserver

This commit is contained in:
Jay Two 2019-02-27 16:21:34 +09:00
parent 3d1c9f59cf
commit deb03a0221
8 changed files with 180 additions and 50 deletions

View File

@ -52,11 +52,14 @@ XlsxTab::~XlsxTab()
if ( NULL != vLayout )
{
vLayout->deleteLater();
vLayout = NULL;
}
if ( NULL != table )
{
table->clear();
table->deleteLater();
table = NULL;
}
}

46
HelloAndroid/DynArray2D.h Normal file
View File

@ -0,0 +1,46 @@
// DynArray2D.h
#ifndef DYNARRAY2D_H
#define DYNARRAY2D_H
// Code from https://www.qtcentre.org/threads/31440-two-dimensional-array-size-determined-dynamically
// Some code is fixed by j2doll
template <typename T> class DynArray2D
{
public:
DynArray2D(int n, int m)
{
_n = n;
_array = new T*[n];
for(int i = 0; i < n; i++)
{
_array[i] = new T[m];
}
}
void setValue(int n, int m, T v)
{
_array[n][m] = v;
}
T getValue(int n, int m)
{
return _array[n][m];
}
~DynArray2D()
{
for (int i = 0 ; i < _n ; i++)
{
delete [] _array[i];
}
delete [] _array;
}
protected:
T **_array;
int _n;
};
#endif // DYNARRAY2D_H

View File

@ -26,7 +26,8 @@ DEFINES += QT_DEPRECATED_WARNINGS
include(../QXlsx/QXlsx.pri)
HEADERS += \
XlsxTableModel.h
XlsxTableModel.h \
DynArray2D.h
SOURCES += \
main.cpp \

View File

@ -29,6 +29,9 @@
using namespace QXlsx;
#include "XlsxTableModel.h"
#include "DynArray2D.h"
std::string convertFromNumberToExcelColumn(int n);
int main(int argc, char *argv[])
{
@ -40,44 +43,73 @@ int main(int argc, char *argv[])
QQmlContext* ctxt = engine.rootContext();
QXlsx::Document xlsx( ":/test.xlsx" ); // load xlsx
if (!xlsx.isLoadPackage())
if (!xlsx.load())
{
qDebug() << "[ERROR] Failed to load xlsx";
return (-1);
}
// A1 B1
// B2 C2
// A3 B3 C3
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);
}
QList<QString> colTitle;
int rowMax = -1;
int colMax = -1;
vcl = wsheet->getFullCells( &rowMax, &colMax );
colTitle.append(QString("A"));
colTitle.append(QString("B"));
colTitle.append(QString("C"));
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);
}
VLIST vl1;
vl1.append( xlsx.read("A1") );
vl1.append( xlsx.read("B1") );
vl1.append( xlsx.read("C1") );
xlsxData.append( vl1 );
VLIST vl2;
vl2.append( xlsx.read("A2") );
vl2.append( xlsx.read("B2") );
vl2.append( xlsx.read("C2") );
xlsxData.append( vl2 );
VLIST vl3;
vl3.append( xlsx.read("A3") );
vl3.append( xlsx.read("B3") );
vl3.append( xlsx.read("C3") );
xlsxData.append( vl3 );
// set model for tableview
XlsxTableModel xlsxTableModel(colTitle, xlsxData);
ctxt->setContextProperty( "xlsxModel", &xlsxTableModel ); // set model for tableview
ctxt->setContextProperty( "xlsxModel", &xlsxTableModel );
engine.load( QUrl(QStringLiteral("qrc:/main.qml")) ); // load QML
if ( engine.rootObjects().isEmpty() )
@ -89,3 +121,39 @@ int main(int argc, char *argv[])
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;
}

View File

@ -3,24 +3,36 @@
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
Window {
id : mainWindow;
visible : true;
title : qsTr("Hello World");
title : qsTr("Hello Android");
/* width: 640; height: 480; */
Component
{
id : columnComponent;
TableViewColumn { resizable : true; movable : false; /*width: 100;*/ }
TableViewColumn {
resizable : true;
movable : false;
/*width: 100;*/
}
}
TableView {
TableView
{
id : mainTableView;
anchors.fill : parent;
model : xlsxModel;
frameVisible : true;
highlightOnFocus : true;
horizontalScrollBarPolicy : Qt.ScrollBarAlwaysOn;
verticalScrollBarPolicy : Qt.ScrollBarAlwaysOn;
resources:
{
var roleList = xlsxModel.customRoleNames;

Binary file not shown.

View File

@ -1,7 +1,7 @@
#
# WebServer.pro
#
# QXlsx https://github.com/QtExcel/QXlsx
# QXlsx https://github.com/QtExcel/QXlsx
TARGET = WebServer
TEMPLATE = app
@ -11,9 +11,10 @@ QT += network
QT -= gui
CONFIG += console
CONFIG += c++14
CONFIG -= app_bundle
# C++14 or higher version is required.
CONFIG += c++14
QMAKE_CXXFLAGS += -std=c++14
macx {

View File

@ -35,7 +35,7 @@ int main(int argc, char *argv[])
ctx.response.send(g_htmlDoc);
});
quint16 listenPort = 3001;
quint16 listenPort = 3001; // default port
auto result = app.listen( listenPort );
if ( result.error() )
{
@ -50,7 +50,9 @@ int main(int argc, char *argv[])
QString getHtml(QString strFilename)
{
QString ret;
ret = ret + QString("<html>\n");
ret = ret + QString("<head>\n");
ret = ret + QString("<title>") + strFilename + QString("</title>\n");
ret = ret + QString("<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>\n" );
@ -59,10 +61,10 @@ QString getHtml(QString strFilename)
ret = ret + QString("<body>\n");
QString strTableStyle = \
"<style>\n"\
" table { border-collapse: collapse; } \n"\
" td, th { border: 1px solid black; } \n"\
"</style>\n";
"<style>\n"\
" table { border-collapse: collapse; } \n"\
" td, th { border: 1px solid black; } \n"\
"</style>\n";
ret = ret + strTableStyle;
if (!loadXlsx(strFilename, ret))
@ -125,22 +127,19 @@ bool loadXlsx(QString fileName, QString& strHtml)
for ( int ic = 0; ic < clList.size(); ++ic )
{
// cell location
CellLocation cl = clList.at(ic);
// cell location
CellLocation cl = clList.at(ic);
int row = cl.row - 1;
int col = cl.col - 1;
int row = cl.row - 1;
int col = cl.col - 1;
////////////////////////////////////////////////////////////////////
// cell pointer
QSharedPointer<Cell> ptrCell = cl.cell;
QSharedPointer<Cell> ptrCell = cl.cell; // cell pointer
///////////////////////////////////////////////////////////////////
// value of cell
QVariant var = cl.cell.data()->value();
QString str = var.toString();
// value of cell
QVariant var = cl.cell.data()->value();
QString str = var.toString();
cellValues[row][col] = str;
cellValues[row][col] = str;
}
QString strTableRecord;