2025-04-13T18:10:29
This commit is contained in:
parent
dc94e00d88
commit
ecec86e4fd
@ -1,19 +1,19 @@
|
|||||||
#include "xlsxhandler.h"
|
#include "xlsxhandler.h"
|
||||||
#include "global.h"
|
|
||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
|
|
||||||
void XlsxHandler::test()
|
void XlsxHandler::test()
|
||||||
{
|
{
|
||||||
QString sourceFilePath = QDir::currentPath() + "/sample.xlsx";
|
QString sourceFilePath = QDir::currentPath() + "/sample.xlsx";
|
||||||
qDebug()<<"fileName:"<<sourceFilePath;
|
qDebug() << "fileName:" << sourceFilePath;
|
||||||
readFile(sourceFilePath);
|
readFile(sourceFilePath);
|
||||||
}
|
}
|
||||||
void XlsxHandler::readFile(const QString sourceFilePath)
|
void XlsxHandler::readFile(const QString sourceFilePath)
|
||||||
{
|
{
|
||||||
// 检查文件是否存在
|
// 检查文件是否存在
|
||||||
if (!QFile::exists(sourceFilePath)) {
|
if(!QFile::exists(sourceFilePath))
|
||||||
|
{
|
||||||
qDebug() << "文件不存在:" << sourceFilePath;
|
qDebug() << "文件不存在:" << sourceFilePath;
|
||||||
return ;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 尝试打开文件
|
// 尝试打开文件
|
||||||
@ -26,7 +26,8 @@ void XlsxHandler::readFile(const QString sourceFilePath)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
QXlsx::Worksheet *workSheet = xlsx.currentWorksheet();
|
QXlsx::Worksheet *workSheet = xlsx.currentWorksheet();
|
||||||
if (!workSheet) {
|
if(!workSheet)
|
||||||
|
{
|
||||||
qDebug() << "当前工作表为空。";
|
qDebug() << "当前工作表为空。";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -36,36 +37,88 @@ void XlsxHandler::readFile(const QString sourceFilePath)
|
|||||||
int colCount = workSheet->dimension().columnCount();
|
int colCount = workSheet->dimension().columnCount();
|
||||||
qDebug() << "row:" << rowCount << "col:" << colCount;
|
qDebug() << "row:" << rowCount << "col:" << colCount;
|
||||||
|
|
||||||
logde<<"0:"<<workSheet->cellAt(1, 1)->value().toString().toStdString();
|
logde << "0:" << workSheet->cellAt(1, 1)->value().toString().toStdString();
|
||||||
qDebug()<<workSheet->cellAt(1, 1)->value().toString();
|
qDebug() << workSheet->cellAt(1, 1)->value().toString();
|
||||||
|
|
||||||
|
|
||||||
int index = 2;
|
int index = 2;
|
||||||
Global::ExperimentInfo ei;
|
Global::ExperimentInfo ei;
|
||||||
ei.sampleName = workSheet->cellAt(index++, 2)->value().toString();
|
ei.sampleName = workSheet->cellAt(index++, 2)->value().toString();
|
||||||
logde<<"sample name:"<<ei.sampleName.toStdString();
|
logde << "sample name:" << ei.sampleName.toStdString();
|
||||||
// return;
|
// return;
|
||||||
|
|
||||||
ei.sampleWeight = workSheet->cellAt(index++, 2)->value().toString();
|
ei.sampleWeight = workSheet->cellAt(index++, 2)->value().toString();
|
||||||
ei.date = workSheet->cellAt(index++, 2)->value().toString();
|
ei.date = workSheet->cellAt(index++, 2)->value().toString();
|
||||||
ei.operatorName = workSheet->cellAt(index++, 2)->value().toString();
|
ei.operatorName = workSheet->cellAt(index++, 2)->value().toString();
|
||||||
ei.phaseSize = workSheet->cellAt(index++, 2)->value().toInt();
|
ei.phaseSize = workSheet->cellAt(index++, 2)->value().toInt();
|
||||||
|
|
||||||
QVector<PhaseTotalInfo> phaseTotalVtr;
|
QVector<Global::PhaseTotalInfo> phaseTotalVtr;
|
||||||
for(int i = 0;i < ei.phaseSize;i++){
|
int startLineIndex = 9;
|
||||||
PhaseTotalInfo phaseTotoal;
|
for(int i = 0; i < ei.phaseSize; i++)
|
||||||
readPhaseData(workSheet,phaseTotoal);
|
{
|
||||||
phaseTotalVtr.push_back(phaseTotoal);
|
Global::PhaseTotalInfo phaseTotal;
|
||||||
|
phaseTotal.phaseIndex = i + 1;
|
||||||
|
|
||||||
|
readPhaseData(workSheet, startLineIndex, phaseTotal);
|
||||||
|
phaseTotalVtr.push_back(phaseTotal);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void XlsxHandler::readPhaseData(QXlsx::Worksheet *workSheet, int &startLineIndex,
|
||||||
|
Global::PhaseTotalInfo &phaseTotal)
|
||||||
|
{
|
||||||
|
// int startLineIndex = 9;
|
||||||
|
startLineIndex++;
|
||||||
|
startLineIndex++;
|
||||||
|
|
||||||
|
phaseTotal.phase.cutoff_temp = workSheet->cellAt(startLineIndex++, 2)->value().toFloat();
|
||||||
|
phaseTotal.phase.temp_flow = workSheet->cellAt(startLineIndex++, 2)->value().toFloat();
|
||||||
|
phaseTotal.phase.constant_temp_time_min = (uint16_t)(workSheet->cellAt(startLineIndex++, 2)->value().toInt());
|
||||||
|
phaseTotal.phase.gas = static_cast<GasType>(workSheet->cellAt(startLineIndex++, 2)->value().toInt());
|
||||||
|
|
||||||
|
int dataSize = workSheet->cellAt(startLineIndex++, 2)->value().toInt();
|
||||||
|
for(int i = 0; i < dataSize; i++)
|
||||||
|
{
|
||||||
|
Global::ExperimentData data;
|
||||||
|
data.runTime = workSheet->cellAt(startLineIndex++, 2)->value().toFloat();
|
||||||
|
data.sampleTemp = workSheet->cellAt(startLineIndex++, 3)->value().toFloat();
|
||||||
|
data.dsc = workSheet->cellAt(startLineIndex++, 4)->value().toFloat();
|
||||||
|
|
||||||
|
phaseTotal.dataVtr.push_back(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void XlsxHandler::writeFile()
|
||||||
|
{
|
||||||
|
QXlsx::Document xlsx;
|
||||||
|
xlsx.addSheet("Sheet1"); // 添加一个新的工作表
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
// 创建一个新的 Excel 文档
|
||||||
|
QXlsx::Document xlsx;
|
||||||
|
|
||||||
|
// 检查数据是否为空
|
||||||
|
if (data.isEmpty()) {
|
||||||
|
qWarning() << "没有数据可写入";
|
||||||
|
return ;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
// 遍历数据并写入单元格
|
||||||
|
for (int row = 0; row < data.size(); ++row) {
|
||||||
|
const QVector<QString>& rowData = data[row];
|
||||||
|
for (int col = 0; col < rowData.size(); ++col) {
|
||||||
|
QString cellValue = rowData[col];
|
||||||
|
xlsx.write(row + 1, col + 1, cellValue); // 行和列从1开始
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void XlsxHandler::readPhaseData(QXlsx::Worksheet *workSheet,const int index, PhaseTotalInfo &phase)
|
// 保存文件
|
||||||
{
|
if (!xlsx.saveAs(filePath)) {
|
||||||
const int startIndex = 9;
|
qWarning() << "无法保存文件:" << filePath;
|
||||||
#if 0
|
return ;
|
||||||
int index = startIndex;
|
}
|
||||||
phase.phaseName = workSheet->cellAt(index++, 2)->value().toString();
|
|
||||||
phase.phaseWeight = workSheet->cellAt(index++, 2)->value().toString();
|
qDebug() << "文件保存成功:" << filePath;
|
||||||
|
return ;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,11 +5,14 @@
|
|||||||
|
|
||||||
#include "xlsxdocument.h"
|
#include "xlsxdocument.h"
|
||||||
#include "protocol.h"
|
#include "protocol.h"
|
||||||
|
#include "global.h"
|
||||||
|
|
||||||
namespace XlsxHandler {
|
namespace XlsxHandler {
|
||||||
void test();
|
void test();
|
||||||
void readFile(const QString filePath);
|
void readFile(const QString filePath);
|
||||||
void readPhaseData(QXlsx::Worksheet*,const int index,PhaseTotalInfo&);
|
void readPhaseData(QXlsx::Worksheet*,int& startLineIndex,Global::PhaseTotalInfo&);
|
||||||
|
|
||||||
|
void writeFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // XLSX_H
|
#endif // XLSX_H
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
#include <qfile.h>
|
#include <qfile.h>
|
||||||
#include <qcustomplot.h>
|
#include <qcustomplot.h>
|
||||||
|
|
||||||
//#include "filemanager.h"
|
#include "protocol.h"
|
||||||
|
|
||||||
class Global:public QObject
|
class Global:public QObject
|
||||||
{
|
{
|
||||||
@ -35,6 +35,7 @@ public:
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct ExperimentData {
|
struct ExperimentData {
|
||||||
|
float runTime;
|
||||||
float sampleTemp;
|
float sampleTemp;
|
||||||
float dsc;
|
float dsc;
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user