#ifndef DTE_H #define DTE_H #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define cacheSqlFileName "sqlite-database-copy.db" //#include "xlsxdocument.h" class TimeRangeSelectorWidget : public QWidget { public: TimeRangeSelectorWidget(QWidget* parent = nullptr) : QWidget(parent) { setupUi(); } std::pairgetIntTime() { int time1 = startTimeEdit->time().hour() * 60 + startTimeEdit->time().minute(); int time2 = endTimeEdit->time().hour() * 60 + startTimeEdit->time().minute(); return { std::min(time1,time2),std::max(time1,time2) }; } QTime getStartTime() const { return startTimeEdit->time(); } QTime getEndTime() const { return endTimeEdit->time(); } private: void setupUi() { QVBoxLayout* layout = new QVBoxLayout(this); QLabel* startLabel = new QLabel("开始时间:", this); startTimeEdit = new QDateTimeEdit(this); startTimeEdit->setDisplayFormat("HH:mm"); QLabel* endLabel = new QLabel("结束时间:", this); endTimeEdit = new QDateTimeEdit(this); endTimeEdit->setDisplayFormat("HH:mm"); layout->addWidget(startLabel); layout->addWidget(startTimeEdit); layout->addWidget(endLabel); layout->addWidget(endTimeEdit); setLayout(layout); } QDateTimeEdit* startTimeEdit; QDateTimeEdit* endTimeEdit; }; class dumpToExcel { public: QWidget* mainWidget; sqlTableDatas globalSqlDatas; QCalendarWidget* calendarW; QPushButton* showMainWidgetBtn; QPushButton* dumpToCsvBtn; QPushButton* dumpToCsvWithDateLimitBtn; QPushButton* openPathSelectBtn; QLineEdit* filePathLineEdit; TimeRangeSelectorWidget* timeSelector; dumpToExcel() { showMainWidgetBtn = new QPushButton(u8"导出数据为表格"); showMainWidgetBtn->setStyleSheet("color: white;"); mainWidget = new QWidget; QHBoxLayout* layout = new QHBoxLayout; QVBoxLayout* VLayout = new QVBoxLayout; mainWidget->setLayout(VLayout); VLayout->addLayout(layout); QWidget* dateWIdget; { calendarW = new QCalendarWidget(); dateWIdget = new QWidget(); QVBoxLayout* layoutTemp = new QVBoxLayout(); dateWIdget->setLayout(layoutTemp); QLabel* labelTemp = new QLabel(u8"请选择日期"); labelTemp->setAlignment(Qt::AlignCenter); layoutTemp->addWidget(labelTemp); layoutTemp->addWidget(calendarW); layout->addWidget(dateWIdget); } QWidget* controlPanel; { controlPanel = new QWidget(); QVBoxLayout* layoutTemp = new QVBoxLayout(); controlPanel->setLayout(layoutTemp); QLabel* title = new QLabel(u8"导出选项"); title->setAlignment(Qt::AlignCenter); QLabel* space = new QLabel(); QLabel* selectFolderLabel = new QLabel(u8"选择导出路径"); QHBoxLayout* layoutTemp2 = new QHBoxLayout(); openPathSelectBtn = new QPushButton("..."); openPathSelectBtn->setFixedWidth(30); timeSelector = new TimeRangeSelectorWidget(); layoutTemp2->addWidget(openPathSelectBtn); filePathLineEdit = new QLineEdit("D://"); layoutTemp2->addWidget(filePathLineEdit); dumpToCsvWithDateLimitBtn = new QPushButton(u8"导出指定时间段数据为csv表格"); dumpToCsvBtn = new QPushButton(u8"导出数据为CSV表格"); layoutTemp->addWidget(title); //layoutTemp->addWidget(data); layoutTemp->addWidget(space); layoutTemp->addWidget(selectFolderLabel); layoutTemp->addLayout(layoutTemp2); layoutTemp->addWidget(dumpToCsvBtn); layoutTemp->addWidget(timeSelector); layoutTemp->addWidget(dumpToCsvWithDateLimitBtn); layoutTemp->setAlignment(Qt::AlignTop); } layout->addWidget(controlPanel); tV = new tableView(); VLayout->addWidget(tV->tableWidget); QDate selectedDate = calendarW->selectedDate(); switchDate(selectedDate.toString("yyyy-MM-dd").toStdString(), "D://"); #pragma region QObject::connect(dumpToCsvWithDateLimitBtn, &QPushButton::clicked, [=]() { QDate selectedDate = calendarW->selectedDate(); std::pairtimes = timeSelector->getIntTime(); saveSqlTableDataToCSV(selectedDate.toString("yyyy-MM-dd"), filePathLineEdit->text() + selectedDate.toString("yyyy-MM-dd") + ".csv",times.first,times.second); }); QObject::connect(openPathSelectBtn, &QPushButton::clicked, [=]() { filePathLineEdit->setText(fileManager::openFolderDialog()+"/"); }); QObject::connect(dumpToCsvBtn, &QPushButton::clicked, [=]() { QDate selectedDate = calendarW->selectedDate(); saveSqlTableDataToCSV(selectedDate.toString("yyyy-MM-dd"), filePathLineEdit->text() + selectedDate.toString("yyyy-MM-dd") + ".csv"); }); QObject::connect(showMainWidgetBtn, &QPushButton::clicked, [=](){ mainWidget->show(); mainWidget->raise(); mainWidget->isActiveWindow(); }); QObject::connect(calendarW, &QCalendarWidget::selectionChanged, [=]() { QDate selectedDate = calendarW->selectedDate(); switchDate(selectedDate.toString("yyyy-MM-dd").toStdString(),"D://"); }); #pragma endregion 信号连接 } void switchDate(std::string dateStr,std::string excelPath) { sqlManager* _sqlManager = &_core._appConfig._sqlManager; QString sqlCopiedFilePath; sqlTableDatas res = _sqlManager->selectAllData(dateStr); #pragma region if (res.rowDatas.size() == 0) { tV->clearAllData(); tV->Init({u8"搜索结果"}); std::vector strVec({ "找不到数据" }); tV->addRowDataValue(strVec); return; } #pragma endregion 检索不到数据的对策 #pragma region tV->clearAllData(); std::vectoravoidVec; std::vectordataNames; //设置表头 for (int i = 0; i < res.rowDatas[0].size(); i++) { QString dataName(QString::fromStdString(res.rowDatas[0][i].key)); dataNames.push_back(dataName); } tV->Init(dataNames); //插入数据 for (auto data : res.rowDatas) { std::vectordatas; for (auto d : data) datas.push_back(QString::fromStdString(d.value)); tV->addRowDataValue(datas); } //隐藏特定列 for(auto index:avoidVec) tV->tableWidget->setColumnHidden(index, true); #pragma endregion 将数据插入tableWidget remove(sqlCopiedFilePath.toLocal8Bit()); } int StringTimeToInt(const std::string input) { size_t firstColonPos = input.find(':'); int timeReturn = 0; if (firstColonPos != std::string::npos) { std::string time1 = input.substr(0, firstColonPos); timeReturn += std::stoi(time1) * 60; size_t secondColonPos = input.find(':', firstColonPos + 1); if (secondColonPos != std::string::npos) { std::string time2 = input.substr(firstColonPos + 1, secondColonPos - firstColonPos - 1); timeReturn += std::stoi(time2); } } return timeReturn; } void saveSqlTableDataToCSV(const QString tableData, const QString& fileName, int time1 = 0, int time2 = 0) { sqlManager* _sqlManager = &_core._appConfig._sqlManager; QString sqlCopiedFilePath; sqlTableDatas res = _sqlManager->selectAllData(tableData.toStdString()); //SN处理 int SNPOS = -1; for (auto i =0;i time2) { continue; } for (const auto& data : rowDatas) { stream << QString::fromStdString(data.value) << ","; } stream << "\n"; } qDebug() << "CSV文件保存成功"; } else { qDebug() << "无法创建CSV文件"; QMessageBox::critical(nullptr, "Error", u8"没有权限或空间不足!"); } } else { // 如果文件已存在,追加内容 if (csvFile.open(QIODevice::Append | QIODevice::Text)) { QTextStream stream(&csvFile); for (const auto& rowDatas : res.rowDatas) { int thisTime = StringTimeToInt(rowDatas[updateTimeIndex].value); if (thisTime < time1 || thisTime > time2) { continue; } for (const auto& data : rowDatas) stream << QString::fromStdString(data.value) << ","; stream << "\n"; } #pragma region std::string command = "explorer /select, \"" + fileName.toStdString() + "\""; size_t pos = 0; while ((pos = command.find("//", pos)) != std::string::npos) { command.replace(pos, 2, "\\"); } system(command.c_str()); #pragma endregion 打开文件夹 QMessageBox::information(nullptr, "Success", u8"成功!"); qDebug() << "CSV文件保存成功"; return; } else { qDebug() << "无法打开CSV文件"; #pragma region std::string command = "explorer /select, \"" + fileName.toStdString() + "\""; size_t pos = 0; while ((pos = command.find("//", pos)) != std::string::npos) { command.replace(pos, 2, "\\"); } system(command.c_str()); #pragma endregion 打开文件夹 QMessageBox::critical(nullptr, "Error", u8"文件已存在且无法被写入!"); } } } #pragma endregion 需要判断时间的循环 #pragma region if (!csvFile.exists()) { // 如果文件不存在,创建文件 if (csvFile.open(QIODevice::WriteOnly | QIODevice::Text)) { QTextStream stream(&csvFile); //顶部标题 for (auto data : res.rowDatas[0]) { QString title; title = QString::fromStdString(data.key); stream << title<<","; } stream << "\n"; for (const auto& rowDatas : res.rowDatas) { for (const auto& data : rowDatas) stream << QString::fromStdString(data.value) << ","; stream << "\n"; } qDebug() << "CSV文件保存成功"; } else { qDebug() << "无法创建CSV文件"; QMessageBox::critical(nullptr, "Error", u8"没有权限或空间不足!"); } } else { // 如果文件已存在,追加内容 if (csvFile.open(QIODevice::Append | QIODevice::Text)) { QTextStream stream(&csvFile); for (const auto& rowDatas : res.rowDatas) { for (const auto& data : rowDatas) stream << QString::fromStdString(data.value) << ","; stream << "\n"; } #pragma region std::string command = "explorer /select, \"" + fileName.toStdString() + "\""; size_t pos = 0; while ((pos = command.find("//", pos)) != std::string::npos) { command.replace(pos, 2, "\\"); } system(command.c_str()); #pragma endregion 打开文件夹 QMessageBox::information(nullptr, "Success", u8"成功!"); qDebug() << "CSV文件保存成功"; return; } else { qDebug() << "无法打开CSV文件"; #pragma region std::string command = "explorer /select, \"" + fileName.toStdString() + "\""; size_t pos = 0; while ((pos = command.find("//", pos)) != std::string::npos) { command.replace(pos, 2, "\\"); } system(command.c_str()); #pragma endregion 打开文件夹 QMessageBox::critical(nullptr, "Error", u8"文件已存在且无法被写入!"); } } #pragma endregion 不需要判断时间的循环 } public slots: private: tableView* tV; }; #pragma once #endif