From 1cfb1de72cd204d747179d65f7032e41e861ae81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=A2=D0=B0=D0=BD?= =?UTF-8?q?=D0=B0=D0=B5=D0=B2?= Date: Sun, 17 Mar 2024 12:20:38 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=D0=A0=D0=B0=D0=B7=D0=B1=D0=BE=D1=80=20?= =?UTF-8?q?json=20=D0=B8=20=D1=81=D0=BE=D1=85=D1=80=D0=B0=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B2=20xlsx?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 76 +++++++++++++++++ jsontoexcel.cpp | 216 ++++++++++++++++++++++++++++++++++++++++++++++++ jsontoexcel.h | 27 ++++++ jsontoexcel.pro | 27 ++++++ main.cpp | 11 +++ main.h | 44 ++++++++++ mainwindow.cpp | 53 ++++++++++++ mainwindow.h | 42 ++++++++++ mainwindow.ui | 51 ++++++++++++ test.json | 50 +++++++++++ 10 files changed, 597 insertions(+) create mode 100644 .gitignore create mode 100644 jsontoexcel.cpp create mode 100644 jsontoexcel.h create mode 100644 jsontoexcel.pro create mode 100644 main.cpp create mode 100644 main.h create mode 100644 mainwindow.cpp create mode 100644 mainwindow.h create mode 100644 mainwindow.ui create mode 100644 test.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..11bd1e4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,76 @@ +# This file is used to ignore files which are generated +# ---------------------------------------------------------------------------- + +*~ +*.autosave +*.a +*.core +*.moc +*.o +*.obj +*.orig +*.rej +*.so +*.so.* +*_pch.h.cpp +*_resource.rc +*.qm +.#* +*.*# +core +!core/ +tags +.DS_Store +.directory +*.debug +Makefile* +*.prl +*.app +moc_*.cpp +ui_*.h +qrc_*.cpp +Thumbs.db +*.res +*.rc +/.qmake.cache +/.qmake.stash +/.qtc_clangd +/.output +debug +release +# qtcreator generated files +*.pro.user* + +# xemacs temporary files +*.flc + +# Vim temporary files +.*.swp + +# Visual Studio generated files +*.ib_pdb_index +*.idb +*.ilk +*.pdb +*.sln +*.suo +*.vcproj +*vcproj.*.*.user +*.ncb +*.sdf +*.opensdf +*.vcxproj +*vcxproj.* + +# MinGW generated files +*.Debug +*.Release + +# Python byte code +*.pyc + +# Binaries +# -------- +*.dll +*.exe + diff --git a/jsontoexcel.cpp b/jsontoexcel.cpp new file mode 100644 index 0000000..65b16ed --- /dev/null +++ b/jsontoexcel.cpp @@ -0,0 +1,216 @@ +#include "jsontoexcel.h" + +JsonToExcel::JsonToExcel(QObject *parent) + : QObject(parent) + +{ + +} +JsonToExcel::~JsonToExcel() +{ + +} +void JsonToExcel::jsonParsToExcel(QString readFile) +{ + QJsonParseError error; + qDebug() << "EXCEL:" << readFile; + QJsonDocument jsonResponse = QJsonDocument::fromJson(readFile.toUtf8(), &error); + qDebug() << "Error: " << error.errorString() << error.offset << error.error; + + QAxObject *excel = new QAxObject("Excel.Application", NULL); + + if (excel->isNull()) + { + QMessageBox(QMessageBox::Icon::Critical, tr("export to excel"), tr("error opening excel")).exec(); + + return; + } + + qDebug() << "Open"; + + QAxObject* workbooks = excel->querySubObject("Workbooks"); + + QAxObject *workbook = workbooks->querySubObject("Add"); + + QAxObject* sheets = workbook->querySubObject("Sheets"); + QAxObject* sheet = sheets->querySubObject("Item( int )", 1); + + int row=3; + + if(jsonResponse.isObject()) + { + QJsonObject jsonObject = jsonResponse.object(); + qDebug() << jsonObject.keys().count(); + + if (jsonObject.keys().count()!= 0) + { + for(int i=0;iquerySubObject("Cells(QVariant,QVariant)", row, 1); + cell_value_num->setProperty("Value", i); + + qDebug() << "Signal: " << jsonKeys; + QAxObject* cell_value_signal_name = sheet->querySubObject("Cells(QVariant,QVariant)", row, 2); + cell_value_signal_name->setProperty("Value",jsonKeys); + + qDebug() << "Key: " <querySubObject("Cells(QVariant,QVariant)", 2, j+4); + cell_value_top->setProperty("Value",jsonObjectArr.keys().at(j)); + + QAxObject* cell_value = sheet->querySubObject("Cells(QVariant,QVariant)", row, j+4); + cell_value->setProperty("Value",jsonValueObj.toString()); + + delete cell_value; + delete cell_value_num; + delete cell_value_signal_name; + delete cell_value_top; + + } + + } + + row ++; + } + + //jsonArrayParser(jsonArray); + + } + else if (jsonValue.isObject()) + { + qDebug() << "Error [ object in object]!"; + } + else + { + qDebug() << jsonObject.keys().at(i) << ":" << jsonValue.toString(); + { + + QAxObject* cell_value_top = sheet->querySubObject("Cells(QVariant,QVariant)", 2, i+2); + cell_value_top->setProperty("Value",jsonObject.keys().at(i)); + + QAxObject* cell_value = sheet->querySubObject("Cells(QVariant,QVariant)", 3, i+2); + cell_value->setProperty("Value",jsonValue.toString()); + + + } + + } + + //row ++; + + } + + + + } + + } + else if(jsonResponse.isArray()) + { + + + QJsonArray jsonArray = jsonResponse.array(); + + qDebug()<querySubObject("Cells(QVariant,QVariant)", 2, j+2); + cell_value_top->setProperty("Value",jsonObject.keys().at(j)); + + QAxObject* cell_value = sheet->querySubObject("Cells(QVariant,QVariant)", k+3, j+2); + cell_value->setProperty("Value",jsonValueObj.toString()); + + + } + + } + else + { + + + qDebug()<< jsonValue.toDouble(); + + //QAxObject* cell_value_top = sheet->querySubObject("Cells(QVariant,QVariant)", 2, j+2); + //cell_value_top->setProperty("Value",jsonObject.keys().at(j)); + + QAxObject* cell_value = sheet->querySubObject("Cells(QVariant,QVariant)", k+3, 2); + + if (jsonValue.isString()) + { + cell_value->setProperty("Value",jsonValue.toString()); + } + else if(jsonValue.isBool()) + { + cell_value->setProperty("Value",jsonValue.toBool()); + } + else if(jsonValue.isDouble()) + { + cell_value->setProperty("Value",jsonValue.toDouble()); + } + + + + } + + + } + + //jsonArrayParser(jsonArray); + + } + + + sheet->querySubObject("Columns")->dynamicCall("AutoFit()"); + delete sheet; + + delete sheets; + delete workbook; + delete workbooks; + + excel->dynamicCall ("SetVisible (bool Visible)", "true"); + + delete excel; + +} diff --git a/jsontoexcel.h b/jsontoexcel.h new file mode 100644 index 0000000..fdad843 --- /dev/null +++ b/jsontoexcel.h @@ -0,0 +1,27 @@ +#ifndef JSONTOEXCEL_H +#define JSONTOEXCEL_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +class JsonToExcel: public QObject +{ + Q_OBJECT +public: + JsonToExcel(QObject *parent = 0); + ~JsonToExcel(); +public slots: + void jsonParsToExcel(QString readFile); +}; + +#endif // JSONTOEXCEL_H diff --git a/jsontoexcel.pro b/jsontoexcel.pro new file mode 100644 index 0000000..d0d5a22 --- /dev/null +++ b/jsontoexcel.pro @@ -0,0 +1,27 @@ +QT += core gui axcontainer + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +CONFIG += c++17 + +# You can make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += \ + jsontoexcel.cpp \ + main.cpp \ + mainwindow.cpp + +HEADERS += \ + jsontoexcel.h \ + main.h \ + mainwindow.h + +FORMS += \ + mainwindow.ui + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..fd3e533 --- /dev/null +++ b/main.cpp @@ -0,0 +1,11 @@ +#include "mainwindow.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + return a.exec(); +} diff --git a/main.h b/main.h new file mode 100644 index 0000000..1f06a9c --- /dev/null +++ b/main.h @@ -0,0 +1,44 @@ +#ifndef MAIN_H +#define MAIN_H + +#include +#include +#include +#include + +class Request : public QObject +{ + +public slots: + void showReply() + { + QJsonDocument jsonResponse = QJsonDocument::fromJson(doc.toUtf8()); + QJsonObject jsonObject = jsonResponse.object(); + QJsonArray jsonArray = jsonObject["response"].toArray(); + int count = jsonArray.count(); + for (int i=0; isetupUi(this); + + _jsontoexcel = new JsonToExcel(); + + + connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(selectFile())); + connect(ui->jsonPars, SIGNAL(clicked()), this, SLOT(on_jsonPars_clicked())); +} + +MainWindow::~MainWindow() +{ + delete ui; +} + +void MainWindow::selectFile() +{ + QString path = QFileDialog::getOpenFileName(this, tr("select excel template path")); + if (path.isEmpty()) + return; + + ui->lineEdit->setText(path); + //readFile(); +} + +QString MainWindow::readFile() +{ + QString val; + QFile file; + qDebug() << ui->lineEdit->text(); + file.setFileName(ui->lineEdit->text()); + file.open(QIODevice::ReadOnly | QIODevice::Text); + val = file.readAll(); + //qDebug() << val; + file.close(); + + + //on_jsonPars_clicked(&val); + + return val; +} + +void MainWindow::on_jsonPars_clicked() +{ + _jsontoexcel->jsonParsToExcel(readFile()); +} + diff --git a/mainwindow.h b/mainwindow.h new file mode 100644 index 0000000..60e4ef5 --- /dev/null +++ b/mainwindow.h @@ -0,0 +1,42 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "jsontoexcel.h" + + +QT_BEGIN_NAMESPACE +namespace Ui { class MainWindow; } +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + + +private slots: + void selectFile(); + QString readFile(); + void on_jsonPars_clicked(); + + +private: + JsonToExcel *_jsontoexcel; + Ui::MainWindow *ui; + +}; +#endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui new file mode 100644 index 0000000..051f59a --- /dev/null +++ b/mainwindow.ui @@ -0,0 +1,51 @@ + + + MainWindow + + + + 0 + 0 + 664 + 85 + + + + MainWindow + + + + + + + + + + ... + + + + + + + jsonPars + + + + + + + + + 0 + 0 + 664 + 22 + + + + + + + + diff --git a/test.json b/test.json new file mode 100644 index 0000000..fcd0c61 --- /dev/null +++ b/test.json @@ -0,0 +1,50 @@ +{ + "table":[ + { + "freq": "0", + "coherence": "1.7683167", + "delta_phase": "0", + "division_ampl": "1", + "own_frequency": "false" + }, + { + "freq": "108.5", + "coherence": "1.025", + "delta_phase": "0", + "division_ampl": "1", + "own_frequency": "false" + }, + { + "freq": "132", + "coherence": "4.22506", + "delta_phase": "0", + "division_ampl": "1", + "own_frequency": "false" + } + ], + + "table1":[ + { + "freq": "0", + "coherence": "1.7683167", + "delta_phase": "0", + "division_ampl": "1", + "own_frequency": "false" + }, + { + "freq": "110.5", + "coherence": "1.025", + "delta_phase": "0", + "division_ampl": "1", + "own_frequency": "false" + }, + { + "freq": "189", + "coherence": "4.22506", + "delta_phase": "0", + "division_ampl": "1", + "own_frequency": "false" + } + + ] +} \ No newline at end of file