diff --git a/QXlsx/header/xlsxdocument.h b/QXlsx/header/xlsxdocument.h index 85ad3477..688612ec 100644 --- a/QXlsx/header/xlsxdocument.h +++ b/QXlsx/header/xlsxdocument.h @@ -118,6 +118,8 @@ class QXLSX_EXPORT Document : public QObject 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 978df5eb..5638dfed 100644 --- a/QXlsx/header/xlsxdocument_p.h +++ b/QXlsx/header/xlsxdocument_p.h @@ -23,6 +23,8 @@ class DocumentPrivate 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 9b586752..d2b1d036 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/README.RU.md b/README.RU.md index 294abcc5..a7756c42 100644 --- a/README.RU.md +++ b/README.RU.md @@ -16,30 +16,24 @@ - См. [FAQ](https://github.com/QtExcel/QXlsx/wiki/FAQ). ## Как настроить (Установка) - -: Рекомендуется: - См. [Как настроить проект QXlsx (qmake)](HowToSetProject.md) - См. [Как настроить проект QXlsx (cmake)](HowToSetProject-cmake.md) - -## Github Actions - -[![Android](https://github.com/QtExcel/QXlsx/actions/workflows/android.yml/badge.svg)](https://github.com/QtExcel/QXlsx/actions/workflows/android.yml) [![IOS](https://github.com/QtExcel/QXlsx/actions/workflows/ios.yml/badge.svg)](https://github.com/QtExcel/QXlsx/actions/workflows/ios.yml) [![MacOS](https://github.com/QtExcel/QXlsx/actions/workflows/macos.yml/badge.svg)](https://github.com/QtExcel/QXlsx/actions/workflows/macos.yml) [![Ubuntu](https://github.com/QtExcel/QXlsx/actions/workflows/ubuntu.yml/badge.svg)](https://github.com/QtExcel/QXlsx/actions/workflows/ubuntu.yml) [![Windows](https://github.com/QtExcel/QXlsx/actions/workflows/windows.yml/badge.svg)](https://github.com/QtExcel/QXlsx/actions/workflows/windows.yml) [![CMake](https://github.com/QtExcel/QXlsx/actions/workflows/cmake.yml/badge.svg)](https://github.com/QtExcel/QXlsx/actions/workflows/cmake.yml) [![cmake-ubuntu](https://github.com/QtExcel/QXlsx/actions/workflows/cmake-ubuntu.yml/badge.svg)](https://github.com/QtExcel/QXlsx/actions/workflows/cmake-ubuntu.yml) - - См. [Протестированные среды](TestEnv.md) ## Вклады - См. [Участники] (https://github.com/QtExcel/QXlsx/graphs/contributors). ## Лицензия и ссылки -- QXlsx находится под лицензией MIT. [https://github.com/QtExcel/QXlsx] (https://github.com/QtExcel/QXlsx) +- QXlsx находится под лицензией MIT. https://github.com/QtExcel/QXlsx - Спасибо за создание следующих замечательных проектов. : +1: - - Qt находится под лицензией LGPL v3 или коммерческой лицензией. [https://www.qt.io/]] (https://www.qt.io/) - - QtXlsxWriter находится под лицензией MIT. : +1: [https://github.com/dbzhang800/QtXlsxWriter] (https://github.com/dbzhang800/QtXlsxWriter) - - Qt-Table-Printer находится под лицензией BSD 3-Clause. [https://github.com/T0ny0/Qt-Table-Printer] (https://github.com/T0ny0/Qt-Table-Printer) - - рекурсивно под лицензией MIT. [https://github.com/pkoretic/recurse] (https://github.com/pkoretic/recurse) - - libfort находится под лицензией MIT. [https://github.com/seleznevae/libfort] (https://github.com/seleznevae/libfort) - - colorprintf находится под лицензией MIT. [https://github.com/VittGam/colorprintf] (https://github.com/VittGam/colorprintf) - - HelloActions-Qt находится под лицензией MIT. [https://github.com/jaredtao/HelloActions-Qt] (https://github.com/jaredtao/HelloActions-Qt) + - Qt находится под лицензией LGPL v3 или коммерческой лицензией. https://www.qt.io/ + - QtXlsxWriter находится под лицензией MIT. : +1: https://github.com/dbzhang800/QtXlsxWriter + - Qt-Table-Printer находится под лицензией BSD 3-Clause. https://github.com/T0ny0/Qt-Table-Printer + - рекурсивно под лицензией MIT. https://github.com/pkoretic/recurse + - libfort находится под лицензией MIT. https://github.com/seleznevae/libfort + - colorprintf находится под лицензией MIT. https://github.com/VittGam/colorprintf + - HelloActions-Qt находится под лицензией MIT. (https://github.com/jaredtao/HelloActions-Qt ##: email: Контакт - Оставь мне вопрос. [https://github.com/QtExcel/QXlsx/issues] (https://github.com/QtExcel/QXlsx/issues) @@ -72,4 +66,5 @@ ### This Document - Written by @NikkiKurashov (github) -- Thank you. I am sorry to forget merge your branch and file. + - Thank you. I am sorry to forget merge your branch and file. (from jaytwo) + diff --git a/README.ko.md b/README.ko.md index 7610e425..d3785c89 100644 --- a/README.ko.md +++ b/README.ko.md @@ -13,17 +13,11 @@ ## 사용하는 방법 - [예제](Example.md) 를 참조하세요. - [위키](https://github.com/QtExcel/QXlsx/wiki) 를 참조하세요. -- [FAQ](https://github.com/QtExcel/QXlsx/wiki/FAQ) 를 참조하세요. +-[FAQ](https://github.com/QtExcel/QXlsx/wiki/FAQ) 를 참조하세요. ## 설정하는 방법 (설치) - - :권장: [QXlsx 프로젝트 설정하는 방법 (qmake)](HowToSetProject.ko.md) 참조 - [QXlsx 프로젝트 설정하는 방법 (cmake)](HowToSetProject-cmake.ko.md) 참조 - -## Github Actions - -[![Android](https://github.com/QtExcel/QXlsx/actions/workflows/android.yml/badge.svg)](https://github.com/QtExcel/QXlsx/actions/workflows/android.yml) [![IOS](https://github.com/QtExcel/QXlsx/actions/workflows/ios.yml/badge.svg)](https://github.com/QtExcel/QXlsx/actions/workflows/ios.yml) [![MacOS](https://github.com/QtExcel/QXlsx/actions/workflows/macos.yml/badge.svg)](https://github.com/QtExcel/QXlsx/actions/workflows/macos.yml) [![Ubuntu](https://github.com/QtExcel/QXlsx/actions/workflows/ubuntu.yml/badge.svg)](https://github.com/QtExcel/QXlsx/actions/workflows/ubuntu.yml) [![Windows](https://github.com/QtExcel/QXlsx/actions/workflows/windows.yml/badge.svg)](https://github.com/QtExcel/QXlsx/actions/workflows/windows.yml) [![CMake](https://github.com/QtExcel/QXlsx/actions/workflows/cmake.yml/badge.svg)](https://github.com/QtExcel/QXlsx/actions/workflows/cmake.yml) [![cmake-ubuntu](https://github.com/QtExcel/QXlsx/actions/workflows/cmake-ubuntu.yml/badge.svg)](https://github.com/QtExcel/QXlsx/actions/workflows/cmake-ubuntu.yml) - - [테스트된 환경](TestEnv.md) 참조 ## 컨트리뷰터 @@ -67,3 +61,4 @@ - SimpleXlsxWriter를 Qt에서 사용하세요. - SimpleXlsxWriter는 MS 엑셀 2007 이상 버전에서 사용 가능한 XLSX 파일을 생성할 수 있는 C++ 라이브러리입니다. + diff --git a/README.md b/README.md index e60817e2..52498522 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,10 @@ - See [tested environments](TestEnv.md) for more information. ## Contributions -- See [contributors](https://github.com/QtExcel/QXlsx/graphs/contributors). + + + + ## License and links - QXlsx is under MIT license. [https://github.com/QtExcel/QXlsx](https://github.com/QtExcel/QXlsx) diff --git a/csv/csv.pro b/csv/csv.pro new file mode 100644 index 00000000..2162eeb9 --- /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 00000000..6a53629c --- /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 00000000..55f7b898 --- /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 00000000..6f1b3a8f Binary files /dev/null and b/csv/test.xlsx differ