feat: Разбор json и сохранение в xlsx

develop
Максим Танаев 2024-03-17 12:20:38 +03:00
parent d6f86fb8f0
commit 1cfb1de72c
10 changed files with 597 additions and 0 deletions

76
.gitignore vendored 100644
View File

@ -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

216
jsontoexcel.cpp 100644
View File

@ -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;i<jsonObject.keys().count();i++)
{
QJsonValue jsonValue = jsonObject.value(jsonObject.keys().at(i));
QString jsonKeys = jsonObject.keys().at(i);
//qDebug() << jsonValue;
if (jsonValue.isArray())
{
QJsonArray jsonArray = jsonValue.toArray();
for(int k=0;k<jsonArray.size();k++)
{
QJsonValue jsonValueArr = jsonArray.at(k);
if(jsonValueArr.isObject())
{
QJsonObject jsonObjectArr = jsonValueArr.toObject();
for(int j=0; j<jsonObjectArr.keys().count(); j++)
{
qDebug() << "№: " << i;//jsonObjectArr.keys().at(j);
QAxObject* cell_value_num = sheet->querySubObject("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: " <<jsonObjectArr.keys().at(j);
QJsonValue jsonValueObj = jsonObjectArr.value(jsonObjectArr.keys().at(j));
qDebug() << "Value: "<<jsonValueObj.toString();
QAxObject* cell_value_top = sheet->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()<<jsonArray.size();
for(int k=0;k<jsonArray.size();k++)
{
QJsonValue jsonValue = jsonArray.at(k);
if(jsonValue.isObject())
{
QJsonObject jsonObject = jsonValue.toObject();
for(int j=0; j<jsonObject.keys().count(); j++)
{
qDebug() << jsonObject.keys().at(j);
QJsonValue jsonValueObj = jsonObject.value(jsonObject.keys().at(j));
qDebug() << jsonValueObj.toString();
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, 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;
}

27
jsontoexcel.h 100644
View File

@ -0,0 +1,27 @@
#ifndef JSONTOEXCEL_H
#define JSONTOEXCEL_H
#include <QObject>
#include <QMainWindow>
#include <QFileDialog>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonValue>
#include <QJsonParseError>
#include <QFile>
#include <QAxObject>
#include <QMessageBox>
class JsonToExcel: public QObject
{
Q_OBJECT
public:
JsonToExcel(QObject *parent = 0);
~JsonToExcel();
public slots:
void jsonParsToExcel(QString readFile);
};
#endif // JSONTOEXCEL_H

27
jsontoexcel.pro 100644
View File

@ -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

11
main.cpp 100644
View File

@ -0,0 +1,11 @@
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

44
main.h 100644
View File

@ -0,0 +1,44 @@
#ifndef MAIN_H
#define MAIN_H
#include <QObject>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
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; i<count; i++)
{
QJsonObject obj = jsonArray[i].toObject();
qDebug() << "regtype =" << obj["regtype"].toString();
qDebug() << "address =" << obj["address"].toInteger();
qDebug() << "name =" << obj["name"].toString();
qDebug() << "val_type =" << obj["val_type"].toString();
if (obj["val_type"].toString() == "long")
{
qDebug() << "value =" << obj["value"].toInteger();
}
else
{
qDebug() << "value =" << obj["value"].toDouble();
}
qDebug();
}
};
};
#endif // MAIN_H

53
mainwindow.cpp 100644
View File

@ -0,0 +1,53 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(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());
}

42
mainwindow.h 100644
View File

@ -0,0 +1,42 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFileDialog>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonValue>
#include <QJsonParseError>
#include <QFile>
#include <QAxObject>
#include <QMessageBox>
#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

51
mainwindow.ui 100644
View File

@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>664</width>
<height>85</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="lineEdit"/>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="jsonPars">
<property name="text">
<string>jsonPars</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>664</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>

50
test.json 100644
View File

@ -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"
}
]
}