jsontoexcel/jsontoexcel.cpp

217 lines
6.7 KiB
C++
Raw Normal View History

#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;
}