diff --git a/QXlsx/source/xlsxdocument.cpp b/QXlsx/source/xlsxdocument.cpp index d2b1d03..cb7111d 100644 --- a/QXlsx/source/xlsxdocument.cpp +++ b/QXlsx/source/xlsxdocument.cpp @@ -478,14 +478,12 @@ bool DocumentPrivate::saveCsv(QString mainCSVFileName) const { Q_Q(const Document); - int sheetIndexNumber = 0; - foreach (QString curretnSheetName, q->sheetNames()) - { + int sheetIndexNumber = 0; + const auto sheetNames = q->sheetNames(); + for (const auto &curretnSheetName : sheetNames) { QXlsx::AbstractSheet *currentSheet = q->sheet(curretnSheetName); - - if (NULL == currentSheet) - { + if (currentSheet == nullptr) { continue; } @@ -495,39 +493,32 @@ bool DocumentPrivate::saveCsv(QString mainCSVFileName) const currentSheet->workbook()->setActiveSheet(sheetIndexNumber); - Worksheet *wsheet = (Worksheet *) currentSheet->workbook()->activeSheet(); - if (NULL == wsheet) - { + Worksheet *wsheet = static_cast(currentSheet->workbook()->activeSheet()); + if (wsheet == nullptr) { continue; } - QString strSheetName = wsheet->sheetName(); // sheet name - - QVector clList = wsheet->getFullCells(&maxRow, &maxCol); + QString strSheetName = wsheet->sheetName(); // sheet name QVector> cellValues; - for (int rc = 0; rc < maxRow; rc++) - { + for (int rc = 0; rc < maxRow; rc++) { QVector tempValue; - for (int cc = 0; cc < maxCol; cc++) - { - tempValue.push_back(QString("")); + for (int cc = 0; cc < maxCol; cc++) { + tempValue.push_back(QString{}); } cellValues.push_back(tempValue); } - for (int ic = 0; ic < clList.size(); ++ic) - { - CellLocation cl = clList.at(ic); - + const QVector clList = wsheet->getFullCells(&maxRow, &maxCol); + for (const auto &cl : clList) { int row = cl.row - 1; int col = cl.col - 1; std::shared_ptr ptrCell = cl.cell; // cell pointer - QVariant var = ptrCell->value(); - QString str = var.toString(); + QVariant var = ptrCell->value(); + QString str = var.toString(); cellValues[row][col] = str; } @@ -535,32 +526,28 @@ bool DocumentPrivate::saveCsv(QString mainCSVFileName) const // TODO: // (1) save as csv file name (using { mainCSVFileName + strSheetName }) - QString csvFileName = mainCSVFileName + QString("_") + strSheetName + QString(".csv"); + QString csvFileName = mainCSVFileName + u'_' + strSheetName + QLatin1String(".csv"); QFile csvFile(csvFileName); - if ( ! csvFile.open( QIODevice::WriteOnly ) ) - { + if (!csvFile.open(QIODevice::WriteOnly)) { continue; } // (2) save sheet values // such as A,,B,,,,C,,,D,, - for (int rc = 0; rc < maxRow; rc++) - { - for (int cc = 0; cc < maxCol; cc++) - { + for (int rc = 0; rc < maxRow; rc++) { + for (int cc = 0; cc < maxCol; cc++) { QString cellData = cellValues[rc][cc]; - if ( cellData.size() >= 0 ) - { - csvFile.write( cellData.toUtf8() ); // cell data + if (cellData.size() >= 0) { + csvFile.write(cellData.toUtf8()); // cell data } - csvFile.write( QString(",").toLatin1() ); // delimeter + csvFile.write(","); // delimeter } - csvFile.write( QString("\n").toLatin1() ); // CR + csvFile.write("\n"); // CR csvFile.flush(); } @@ -571,7 +558,6 @@ bool DocumentPrivate::saveCsv(QString mainCSVFileName) const } // foreach (QString curretnSheetName, q->sheetNames()) ... - return true; } @@ -1383,16 +1369,13 @@ bool Document::saveAs(QIODevice *device) const return d->savePackage(device); } - bool Document::saveAsCsv(const QString mainCSVFileName) const { Q_D(const Document); - return d->saveCsv( mainCSVFileName ); + return d->saveCsv(mainCSVFileName); } - - bool Document::isLoadPackage() const { Q_D(const Document); diff --git a/QXlsx/source/xlsxworkbook.cpp b/QXlsx/source/xlsxworkbook.cpp index b183b37..8a907ed 100644 --- a/QXlsx/source/xlsxworkbook.cpp +++ b/QXlsx/source/xlsxworkbook.cpp @@ -607,19 +607,18 @@ bool Workbook::loadFromXmlFile(QIODevice *device) AbstractSheet *sheet = addSheet(name, sheetId, type); sheet->setSheetState(state); - if (relationship.target.startsWith("/")) { + if (relationship.target.startsWith(u'/')) { QString fullPath = QDir::cleanPath(relationship.target.mid(1)); sheet->setFilePath(fullPath); - }else{ + } else { QString strFilePath = filePath(); - - // const QString fullPath = QDir::cleanPath(splitPath(strFilePath).constFirst() + - // QLatin1String("/") + relationship.target); + + // const QString fullPath = QDir::cleanPath(splitPath(strFilePath).constFirst() + // + QLatin1String("/") + relationship.target); const auto parts = splitPath(strFilePath); - QString fullPath = - QDir::cleanPath(parts.first() + QLatin1String("/") + relationship.target); - + QString fullPath = QDir::cleanPath(parts.first() + u'/' + relationship.target); + sheet->setFilePath(fullPath); } } else if (reader.name() == QLatin1String("workbookPr")) {