Files
2024-08-14 11:15:16 +08:00

59 lines
2.4 KiB
C++

#include <appCore.hpp>
#include <writeExcel.h>
#include <globalStruct.h>
std::vector < std::pair<std::string, std::string>> writeExcelLoop(XLWorksheet& sheet, customExcelStruct& input, int depth, int cols)
{
std::vector < std::pair<std::string, std::string>> mergeStr;
std::string startUnit = char(65 + depth) + std::to_string(cols);
int depthCount = 0;
for (int i = 0; i < input.nexCols.size(); i++)
{
std::vector < std::pair<std::string, std::string>>
sonMerges = writeExcelLoop(sheet, input.nexCols[i], depth + 1, cols + depthCount);
mergeStr.insert(mergeStr.end(), sonMerges.begin(), sonMerges.end());
depthCount += getMax((int)input.nexCols[i].nexCols.size(), 1);
}
std::string endUnit = char(65 + depth) + std::to_string(cols + getMax(depthCount - 1, 0));
sheet.cell(startUnit).value() = input.text;
mergeStr.push_back(std::make_pair(startUnit, endUnit));
return mergeStr;
}
void writeExcel(std::string path, std::string sheetName, std::vector<std::string> titles, customExcelStruct input)
{
std::vector<std::pair<std::string, std::string>> mergeStr(0);
try {
fileManager::setFileReadOnly(path, false);
XLDocument doc;
if (std::filesystem::exists(path) == true)
doc.open(path);
else
doc.create(path);
if (doc.workbook().worksheetExists(sheetName) == false)
doc.workbook().addWorksheet(sheetName);
auto sheet = doc.workbook().worksheet(sheetName);
int lastrow = sheet.rowCount();
if (lastrow == 0)
{
auto sheet = doc.workbook().worksheet(sheetName);
for (int i = 0; i < titles.size(); i++)
{
std::string unit = char(65 + i) + std::string("1");
sheet.cell(unit).value() = titles[i];
}
}
lastrow = sheet.rowCount();
mergeStr = writeExcelLoop(sheet, input, 0, lastrow + 1);
doc.save();
doc.close();
}
catch (const std::exception& e) {
std::cerr << "An error occurred: " << e.what() << std::endl;
fileManager::setFileReadOnly(path, true);
}
/*for (auto &str : mergeStr)
if(str.second.compare(""))
_core._pythonCpp.runPythonScript("./pyScript/mergeExcelCells.py", { path,sheetName, str.first, str.second });*/
fileManager::setFileReadOnly(path, true);
}