2025-04-13T18:10:29
This commit is contained in:
parent
dc94e00d88
commit
ecec86e4fd
@ -1,19 +1,19 @@
|
||||
#include "xlsxhandler.h"
|
||||
#include "global.h"
|
||||
#include "logger.h"
|
||||
|
||||
void XlsxHandler::test()
|
||||
{
|
||||
QString sourceFilePath = QDir::currentPath() + "/sample.xlsx";
|
||||
qDebug()<<"fileName:"<<sourceFilePath;
|
||||
qDebug() << "fileName:" << sourceFilePath;
|
||||
readFile(sourceFilePath);
|
||||
}
|
||||
void XlsxHandler::readFile(const QString sourceFilePath)
|
||||
{
|
||||
// 检查文件是否存在
|
||||
if (!QFile::exists(sourceFilePath)) {
|
||||
if(!QFile::exists(sourceFilePath))
|
||||
{
|
||||
qDebug() << "文件不存在:" << sourceFilePath;
|
||||
return ;
|
||||
return;
|
||||
}
|
||||
|
||||
// 尝试打开文件
|
||||
@ -26,7 +26,8 @@ void XlsxHandler::readFile(const QString sourceFilePath)
|
||||
#endif
|
||||
|
||||
QXlsx::Worksheet *workSheet = xlsx.currentWorksheet();
|
||||
if (!workSheet) {
|
||||
if(!workSheet)
|
||||
{
|
||||
qDebug() << "当前工作表为空。";
|
||||
return;
|
||||
}
|
||||
@ -36,36 +37,88 @@ void XlsxHandler::readFile(const QString sourceFilePath)
|
||||
int colCount = workSheet->dimension().columnCount();
|
||||
qDebug() << "row:" << rowCount << "col:" << colCount;
|
||||
|
||||
logde<<"0:"<<workSheet->cellAt(1, 1)->value().toString().toStdString();
|
||||
qDebug()<<workSheet->cellAt(1, 1)->value().toString();
|
||||
logde << "0:" << workSheet->cellAt(1, 1)->value().toString().toStdString();
|
||||
qDebug() << workSheet->cellAt(1, 1)->value().toString();
|
||||
|
||||
|
||||
int index = 2;
|
||||
int index = 2;
|
||||
Global::ExperimentInfo ei;
|
||||
ei.sampleName = workSheet->cellAt(index++, 2)->value().toString();
|
||||
logde<<"sample name:"<<ei.sampleName.toStdString();
|
||||
// return;
|
||||
ei.sampleName = workSheet->cellAt(index++, 2)->value().toString();
|
||||
logde << "sample name:" << ei.sampleName.toStdString();
|
||||
// return;
|
||||
|
||||
ei.sampleWeight = workSheet->cellAt(index++, 2)->value().toString();
|
||||
ei.date = workSheet->cellAt(index++, 2)->value().toString();
|
||||
ei.operatorName = workSheet->cellAt(index++, 2)->value().toString();
|
||||
ei.phaseSize = workSheet->cellAt(index++, 2)->value().toInt();
|
||||
|
||||
QVector<PhaseTotalInfo> phaseTotalVtr;
|
||||
for(int i = 0;i < ei.phaseSize;i++){
|
||||
PhaseTotalInfo phaseTotoal;
|
||||
readPhaseData(workSheet,phaseTotoal);
|
||||
phaseTotalVtr.push_back(phaseTotoal);
|
||||
QVector<Global::PhaseTotalInfo> phaseTotalVtr;
|
||||
int startLineIndex = 9;
|
||||
for(int i = 0; i < ei.phaseSize; i++)
|
||||
{
|
||||
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)
|
||||
{
|
||||
const int startIndex = 9;
|
||||
#if 0
|
||||
int index = startIndex;
|
||||
phase.phaseName = workSheet->cellAt(index++, 2)->value().toString();
|
||||
phase.phaseWeight = workSheet->cellAt(index++, 2)->value().toString();
|
||||
// 保存文件
|
||||
if (!xlsx.saveAs(filePath)) {
|
||||
qWarning() << "无法保存文件:" << filePath;
|
||||
return ;
|
||||
}
|
||||
|
||||
qDebug() << "文件保存成功:" << filePath;
|
||||
return ;
|
||||
#endif
|
||||
}
|
||||
|
@ -5,11 +5,14 @@
|
||||
|
||||
#include "xlsxdocument.h"
|
||||
#include "protocol.h"
|
||||
#include "global.h"
|
||||
|
||||
namespace XlsxHandler {
|
||||
void test();
|
||||
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
|
||||
|
@ -5,7 +5,7 @@
|
||||
#include <qfile.h>
|
||||
#include <qcustomplot.h>
|
||||
|
||||
//#include "filemanager.h"
|
||||
#include "protocol.h"
|
||||
|
||||
class Global:public QObject
|
||||
{
|
||||
@ -35,6 +35,7 @@ public:
|
||||
#endif
|
||||
|
||||
struct ExperimentData {
|
||||
float runTime;
|
||||
float sampleTemp;
|
||||
float dsc;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user