@@ -478,14 +478,12 @@ bool DocumentPrivate::saveCsv(QString mainCSVFileName) const
478
478
{
479
479
Q_Q (const Document);
480
480
481
- int sheetIndexNumber = 0 ;
482
- foreach (QString curretnSheetName, q->sheetNames ())
483
- {
481
+ int sheetIndexNumber = 0 ;
482
+ const auto sheetNames = q->sheetNames ();
483
+ for ( const auto &curretnSheetName : sheetNames) {
484
484
485
485
QXlsx::AbstractSheet *currentSheet = q->sheet (curretnSheetName);
486
-
487
- if (NULL == currentSheet)
488
- {
486
+ if (currentSheet == nullptr ) {
489
487
continue ;
490
488
}
491
489
@@ -495,72 +493,61 @@ bool DocumentPrivate::saveCsv(QString mainCSVFileName) const
495
493
496
494
currentSheet->workbook ()->setActiveSheet (sheetIndexNumber);
497
495
498
- Worksheet *wsheet = (Worksheet *) currentSheet->workbook ()->activeSheet ();
499
- if (NULL == wsheet)
500
- {
496
+ Worksheet *wsheet = static_cast <Worksheet *>(currentSheet->workbook ()->activeSheet ());
497
+ if (wsheet == nullptr ) {
501
498
continue ;
502
499
}
503
500
504
- QString strSheetName = wsheet->sheetName (); // sheet name
505
-
506
- QVector<CellLocation> clList = wsheet->getFullCells (&maxRow, &maxCol);
501
+ QString strSheetName = wsheet->sheetName (); // sheet name
507
502
508
503
QVector<QVector<QString>> cellValues;
509
- for (int rc = 0 ; rc < maxRow; rc++)
510
- {
504
+ for (int rc = 0 ; rc < maxRow; rc++) {
511
505
QVector<QString> tempValue;
512
506
513
- for (int cc = 0 ; cc < maxCol; cc++)
514
- {
515
- tempValue.push_back (QString (" " ));
507
+ for (int cc = 0 ; cc < maxCol; cc++) {
508
+ tempValue.push_back (QString{});
516
509
}
517
510
518
511
cellValues.push_back (tempValue);
519
512
}
520
513
521
- for (int ic = 0 ; ic < clList.size (); ++ic)
522
- {
523
- CellLocation cl = clList.at (ic);
524
-
514
+ const QVector<CellLocation> clList = wsheet->getFullCells (&maxRow, &maxCol);
515
+ for (const auto &cl : clList) {
525
516
int row = cl.row - 1 ;
526
517
int col = cl.col - 1 ;
527
518
528
519
std::shared_ptr<Cell> ptrCell = cl.cell ; // cell pointer
529
- QVariant var = ptrCell->value ();
530
- QString str = var.toString ();
520
+ QVariant var = ptrCell->value ();
521
+ QString str = var.toString ();
531
522
532
523
cellValues[row][col] = str;
533
524
}
534
525
535
526
// TODO:
536
527
// (1) save as csv file name (using { mainCSVFileName + strSheetName })
537
528
538
- QString csvFileName = mainCSVFileName + QString ( " _ " ) + strSheetName + QString (" .csv" );
529
+ QString csvFileName = mainCSVFileName + u ' _ ' + strSheetName + QLatin1String (" .csv" );
539
530
QFile csvFile (csvFileName);
540
- if ( ! csvFile.open ( QIODevice::WriteOnly ) )
541
- {
531
+ if (!csvFile.open (QIODevice::WriteOnly)) {
542
532
continue ;
543
533
}
544
534
545
535
// (2) save sheet values
546
536
// such as A,,B,,,,C,,,D,,
547
537
548
- for (int rc = 0 ; rc < maxRow; rc++)
549
- {
550
- for (int cc = 0 ; cc < maxCol; cc++)
551
- {
538
+ for (int rc = 0 ; rc < maxRow; rc++) {
539
+ for (int cc = 0 ; cc < maxCol; cc++) {
552
540
553
541
QString cellData = cellValues[rc][cc];
554
542
555
- if ( cellData.size () >= 0 )
556
- {
557
- csvFile.write ( cellData.toUtf8 () ); // cell data
543
+ if (cellData.size () >= 0 ) {
544
+ csvFile.write (cellData.toUtf8 ()); // cell data
558
545
}
559
546
560
- csvFile.write ( QString ( " ," ). toLatin1 () ); // delimeter
547
+ csvFile.write (" ," ); // delimeter
561
548
}
562
549
563
- csvFile.write ( QString ( " \n " ). toLatin1 () ); // CR
550
+ csvFile.write (" \n " ); // CR
564
551
565
552
csvFile.flush ();
566
553
}
@@ -571,7 +558,6 @@ bool DocumentPrivate::saveCsv(QString mainCSVFileName) const
571
558
572
559
} // foreach (QString curretnSheetName, q->sheetNames()) ...
573
560
574
-
575
561
return true ;
576
562
}
577
563
@@ -1383,16 +1369,13 @@ bool Document::saveAs(QIODevice *device) const
1383
1369
return d->savePackage (device);
1384
1370
}
1385
1371
1386
-
1387
1372
bool Document::saveAsCsv (const QString mainCSVFileName) const
1388
1373
{
1389
1374
Q_D (const Document);
1390
1375
1391
- return d->saveCsv ( mainCSVFileName );
1376
+ return d->saveCsv (mainCSVFileName);
1392
1377
}
1393
1378
1394
-
1395
-
1396
1379
bool Document::isLoadPackage () const
1397
1380
{
1398
1381
Q_D (const Document);
0 commit comments