diff --git a/QXlsx/header/xlsxdocument.h b/QXlsx/header/xlsxdocument.h index 85ad347..688612e 100644 --- a/QXlsx/header/xlsxdocument.h +++ b/QXlsx/header/xlsxdocument.h @@ -118,6 +118,8 @@ public: bool saveAs(const QString &xlsXname) const; bool saveAs(QIODevice *device) const; + bool saveAsCsv(const QString mainCSVFileName) const; + // copy style from one xlsx file to other static bool copyStyle(const QString &from, const QString &to); diff --git a/QXlsx/header/xlsxdocument_p.h b/QXlsx/header/xlsxdocument_p.h index 978df5e..5638dfe 100644 --- a/QXlsx/header/xlsxdocument_p.h +++ b/QXlsx/header/xlsxdocument_p.h @@ -23,6 +23,8 @@ public: bool loadPackage(QIODevice *device); bool savePackage(QIODevice *device) const; + bool saveCsv(const QString mainCSVFileName) const; + // copy style from one xlsx file to other static bool copyStyle(const QString &from, const QString &to); diff --git a/QXlsx/source/xlsxdocument.cpp b/QXlsx/source/xlsxdocument.cpp index 9b58675..d2b1d03 100644 --- a/QXlsx/source/xlsxdocument.cpp +++ b/QXlsx/source/xlsxdocument.cpp @@ -470,6 +470,111 @@ bool DocumentPrivate::savePackage(QIODevice *device) const return true; } +// +// j2doll/csv branch +// +// Save from XLSX to CSV +bool DocumentPrivate::saveCsv(QString mainCSVFileName) const +{ + Q_Q(const Document); + + int sheetIndexNumber = 0; + foreach (QString curretnSheetName, q->sheetNames()) + { + + QXlsx::AbstractSheet *currentSheet = q->sheet(curretnSheetName); + + if (NULL == currentSheet) + { + continue; + } + + // get full cells of sheet + int maxRow = -1; + int maxCol = -1; + + currentSheet->workbook()->setActiveSheet(sheetIndexNumber); + + Worksheet *wsheet = (Worksheet *) currentSheet->workbook()->activeSheet(); + if (NULL == wsheet) + { + continue; + } + + QString strSheetName = wsheet->sheetName(); // sheet name + + QVector clList = wsheet->getFullCells(&maxRow, &maxCol); + + QVector> cellValues; + for (int rc = 0; rc < maxRow; rc++) + { + QVector tempValue; + + 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); + + 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(); + + cellValues[row][col] = str; + } + + // TODO: + // (1) save as csv file name (using { mainCSVFileName + strSheetName }) + + QString csvFileName = mainCSVFileName + QString("_") + strSheetName + QString(".csv"); + QFile csvFile(csvFileName); + 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++) + { + + QString cellData = cellValues[rc][cc]; + + if ( cellData.size() >= 0 ) + { + csvFile.write( cellData.toUtf8() ); // cell data + } + + csvFile.write( QString(",").toLatin1() ); // delimeter + } + + csvFile.write( QString("\n").toLatin1() ); // CR + + csvFile.flush(); + } + + // file.flush(); + + csvFile.close(); + + } // foreach (QString curretnSheetName, q->sheetNames()) ... + + + return true; +} + bool DocumentPrivate::copyStyle(const QString &from, const QString &to) { // create a temp file because the zip writer cannot modify already existing zips @@ -1278,6 +1383,16 @@ 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 ); +} + + + bool Document::isLoadPackage() const { Q_D(const Document); diff --git a/csv/csv.pro b/csv/csv.pro new file mode 100644 index 0000000..2162eeb --- /dev/null +++ b/csv/csv.pro @@ -0,0 +1,45 @@ +# csv.pro + +TARGET = csv +TEMPLATE = app + +QT += core +QT += gui + +CONFIG += console +CONFIG -= app_bundle + +# NOTE: You can fix value of QXlsx path of source code. +# QXLSX_PARENTPATH=./ +# QXLSX_HEADERPATH=./header/ +# QXLSX_SOURCEPATH=./source/ +include(../QXlsx/QXlsx.pri) + +########################################################################## +# The following define makes your compiler emit warnings if you use +# any feature of Qt which as been marked deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +########################################################################## +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain +# version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 +# disables all the APIs deprecated before Qt 6.0.0 + +########################################################################## +# source code + +SOURCES += \ +main.cpp + +# RESOURCES += \ +# test.qrc + +RESOURCES += \ + test.qrc + + diff --git a/csv/main.cpp b/csv/main.cpp new file mode 100644 index 0000000..6a53629 --- /dev/null +++ b/csv/main.cpp @@ -0,0 +1,44 @@ +// main.cpp + +#include +// using namespace std; + +#include +#include +#include +#include +#include +#include + +#include "xlsxcellrange.h" +#include "xlsxchart.h" +#include "xlsxchartsheet.h" +#include "xlsxdocument.h" +#include "xlsxrichstring.h" +#include "xlsxworkbook.h" + + +int main(int argc, char *argv[]) +{ + QCoreApplication app(argc, argv); + + + { + using namespace QXlsx; + + QString xlsxFileName = ":/test.xlsx"; + QXlsx::Document xlsxDoc(xlsxFileName); + if (!xlsxDoc.isLoadPackage()) { + return 0; // failed to load + } + + QString csvFileName = "hello.csv"; + if ( xlsxDoc.saveAsCsv(csvFileName) ){ + qDebug() << "save as csv file"; + } + + } + + + return 0; +} diff --git a/csv/test.qrc b/csv/test.qrc new file mode 100644 index 0000000..55f7b89 --- /dev/null +++ b/csv/test.qrc @@ -0,0 +1,6 @@ + + + + test.xlsx + + diff --git a/csv/test.xlsx b/csv/test.xlsx new file mode 100644 index 0000000..6f1b3a8 Binary files /dev/null and b/csv/test.xlsx differ