Skip to content

Commit f2594e0

Browse files
authored
Merge pull request #345 from j2doll/j2doll/csv
- branch J2doll/csv - new function for saving as csv file format (testing now)
2 parents db36c65 + f1bd57e commit f2594e0

File tree

10 files changed

+230
-23
lines changed

10 files changed

+230
-23
lines changed

QXlsx/header/xlsxdocument.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ class QXLSX_EXPORT Document : public QObject
118118
bool saveAs(const QString &xlsXname) const;
119119
bool saveAs(QIODevice *device) const;
120120

121+
bool saveAsCsv(const QString mainCSVFileName) const;
122+
121123
// copy style from one xlsx file to other
122124
static bool copyStyle(const QString &from, const QString &to);
123125

QXlsx/header/xlsxdocument_p.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class DocumentPrivate
2323
bool loadPackage(QIODevice *device);
2424
bool savePackage(QIODevice *device) const;
2525

26+
bool saveCsv(const QString mainCSVFileName) const;
27+
2628
// copy style from one xlsx file to other
2729
static bool copyStyle(const QString &from, const QString &to);
2830

QXlsx/source/xlsxdocument.cpp

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,111 @@ bool DocumentPrivate::savePackage(QIODevice *device) const
470470
return true;
471471
}
472472

473+
//
474+
// j2doll/csv branch
475+
//
476+
// Save from XLSX to CSV
477+
bool DocumentPrivate::saveCsv(QString mainCSVFileName) const
478+
{
479+
Q_Q(const Document);
480+
481+
int sheetIndexNumber = 0;
482+
foreach (QString curretnSheetName, q->sheetNames())
483+
{
484+
485+
QXlsx::AbstractSheet *currentSheet = q->sheet(curretnSheetName);
486+
487+
if (NULL == currentSheet)
488+
{
489+
continue;
490+
}
491+
492+
// get full cells of sheet
493+
int maxRow = -1;
494+
int maxCol = -1;
495+
496+
currentSheet->workbook()->setActiveSheet(sheetIndexNumber);
497+
498+
Worksheet *wsheet = (Worksheet *) currentSheet->workbook()->activeSheet();
499+
if (NULL == wsheet)
500+
{
501+
continue;
502+
}
503+
504+
QString strSheetName = wsheet->sheetName(); // sheet name
505+
506+
QVector<CellLocation> clList = wsheet->getFullCells(&maxRow, &maxCol);
507+
508+
QVector<QVector<QString>> cellValues;
509+
for (int rc = 0; rc < maxRow; rc++)
510+
{
511+
QVector<QString> tempValue;
512+
513+
for (int cc = 0; cc < maxCol; cc++)
514+
{
515+
tempValue.push_back(QString(""));
516+
}
517+
518+
cellValues.push_back(tempValue);
519+
}
520+
521+
for (int ic = 0; ic < clList.size(); ++ic)
522+
{
523+
CellLocation cl = clList.at(ic);
524+
525+
int row = cl.row - 1;
526+
int col = cl.col - 1;
527+
528+
std::shared_ptr<Cell> ptrCell = cl.cell; // cell pointer
529+
QVariant var = ptrCell->value();
530+
QString str = var.toString();
531+
532+
cellValues[row][col] = str;
533+
}
534+
535+
// TODO:
536+
// (1) save as csv file name (using { mainCSVFileName + strSheetName })
537+
538+
QString csvFileName = mainCSVFileName + QString("_") + strSheetName + QString(".csv");
539+
QFile csvFile(csvFileName);
540+
if ( ! csvFile.open( QIODevice::WriteOnly ) )
541+
{
542+
continue;
543+
}
544+
545+
// (2) save sheet values
546+
// such as A,,B,,,,C,,,D,,
547+
548+
for (int rc = 0; rc < maxRow; rc++)
549+
{
550+
for (int cc = 0; cc < maxCol; cc++)
551+
{
552+
553+
QString cellData = cellValues[rc][cc];
554+
555+
if ( cellData.size() >= 0 )
556+
{
557+
csvFile.write( cellData.toUtf8() ); // cell data
558+
}
559+
560+
csvFile.write( QString(",").toLatin1() ); // delimeter
561+
}
562+
563+
csvFile.write( QString("\n").toLatin1() ); // CR
564+
565+
csvFile.flush();
566+
}
567+
568+
// file.flush();
569+
570+
csvFile.close();
571+
572+
} // foreach (QString curretnSheetName, q->sheetNames()) ...
573+
574+
575+
return true;
576+
}
577+
473578
bool DocumentPrivate::copyStyle(const QString &from, const QString &to)
474579
{
475580
// create a temp file because the zip writer cannot modify already existing zips
@@ -1278,6 +1383,16 @@ bool Document::saveAs(QIODevice *device) const
12781383
return d->savePackage(device);
12791384
}
12801385

1386+
1387+
bool Document::saveAsCsv(const QString mainCSVFileName) const
1388+
{
1389+
Q_D(const Document);
1390+
1391+
return d->saveCsv( mainCSVFileName );
1392+
}
1393+
1394+
1395+
12811396
bool Document::isLoadPackage() const
12821397
{
12831398
Q_D(const Document);

README.RU.md

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,24 @@
1616
- См. [FAQ](https://github.com/QtExcel/QXlsx/wiki/FAQ).
1717

1818
## Как настроить (Установка)
19-
2019
-: Рекомендуется:
2120
- См. [Как настроить проект QXlsx (qmake)](HowToSetProject.md)
2221
- См. [Как настроить проект QXlsx (cmake)](HowToSetProject-cmake.md)
23-
24-
## Github Actions
25-
26-
[![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)
27-
2822
- См. [Протестированные среды](TestEnv.md)
2923

3024
## Вклады
3125
- См. [Участники] (https://github.com/QtExcel/QXlsx/graphs/contributors).
3226

3327
## Лицензия и ссылки
34-
- QXlsx находится под лицензией MIT. [https://github.com/QtExcel/QXlsx] (https://github.com/QtExcel/QXlsx)
28+
- QXlsx находится под лицензией MIT. https://github.com/QtExcel/QXlsx
3529
- Спасибо за создание следующих замечательных проектов. : +1:
36-
- Qt находится под лицензией LGPL v3 или коммерческой лицензией. [https://www.qt.io/]] (https://www.qt.io/)
37-
- QtXlsxWriter находится под лицензией MIT. : +1: [https://github.com/dbzhang800/QtXlsxWriter] (https://github.com/dbzhang800/QtXlsxWriter)
38-
- Qt-Table-Printer находится под лицензией BSD 3-Clause. [https://github.com/T0ny0/Qt-Table-Printer] (https://github.com/T0ny0/Qt-Table-Printer)
39-
- рекурсивно под лицензией MIT. [https://github.com/pkoretic/recurse] (https://github.com/pkoretic/recurse)
40-
- libfort находится под лицензией MIT. [https://github.com/seleznevae/libfort] (https://github.com/seleznevae/libfort)
41-
- colorprintf находится под лицензией MIT. [https://github.com/VittGam/colorprintf] (https://github.com/VittGam/colorprintf)
42-
- HelloActions-Qt находится под лицензией MIT. [https://github.com/jaredtao/HelloActions-Qt] (https://github.com/jaredtao/HelloActions-Qt)
30+
- Qt находится под лицензией LGPL v3 или коммерческой лицензией. https://www.qt.io/
31+
- QtXlsxWriter находится под лицензией MIT. : +1: https://github.com/dbzhang800/QtXlsxWriter
32+
- Qt-Table-Printer находится под лицензией BSD 3-Clause. https://github.com/T0ny0/Qt-Table-Printer
33+
- рекурсивно под лицензией MIT. https://github.com/pkoretic/recurse
34+
- libfort находится под лицензией MIT. https://github.com/seleznevae/libfort
35+
- colorprintf находится под лицензией MIT. https://github.com/VittGam/colorprintf
36+
- HelloActions-Qt находится под лицензией MIT. (https://github.com/jaredtao/HelloActions-Qt
4337

4438
##: email: Контакт
4539
- Оставь мне вопрос. [https://github.com/QtExcel/QXlsx/issues] (https://github.com/QtExcel/QXlsx/issues)
@@ -72,4 +66,5 @@
7266

7367
### This Document
7468
- Written by @NikkiKurashov (github)
75-
- Thank you. I am sorry to forget merge your branch and file.
69+
- Thank you. I am sorry to forget merge your branch and file. (from jaytwo)
70+

README.ko.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,11 @@
1313
## 사용하는 방법
1414
- [예제](Example.md) 를 참조하세요.
1515
- [위키](https://github.com/QtExcel/QXlsx/wiki) 를 참조하세요.
16-
- [FAQ](https://github.com/QtExcel/QXlsx/wiki/FAQ) 를 참조하세요.
16+
-[FAQ](https://github.com/QtExcel/QXlsx/wiki/FAQ) 를 참조하세요.
1717

1818
## 설정하는 방법 (설치)
19-
2019
- :권장: [QXlsx 프로젝트 설정하는 방법 (qmake)](HowToSetProject.ko.md) 참조
2120
- [QXlsx 프로젝트 설정하는 방법 (cmake)](HowToSetProject-cmake.ko.md) 참조
22-
23-
## Github Actions
24-
25-
[![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)
26-
2721
- [테스트된 환경](TestEnv.md) 참조
2822

2923
## 컨트리뷰터
@@ -67,3 +61,4 @@
6761

6862
- SimpleXlsxWriter를 Qt에서 사용하세요.
6963
- SimpleXlsxWriter는 MS 엑셀 2007 이상 버전에서 사용 가능한 XLSX 파일을 생성할 수 있는 C++ 라이브러리입니다.
64+

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727
- See [tested environments](TestEnv.md) for more information.
2828

2929
## Contributions
30-
- See [contributors](https://github.com/QtExcel/QXlsx/graphs/contributors).
30+
31+
<a href="https://github.com/QtExcel/QXlsx/graphs/contributors">
32+
<img src="https://contrib.rocks/image?repo=QtExcel/QXlsx" />
33+
</a>
3134

3235
## License and links
3336
- QXlsx is under MIT license. [https://github.com/QtExcel/QXlsx](https://github.com/QtExcel/QXlsx)

csv/csv.pro

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# csv.pro
2+
3+
TARGET = csv
4+
TEMPLATE = app
5+
6+
QT += core
7+
QT += gui
8+
9+
CONFIG += console
10+
CONFIG -= app_bundle
11+
12+
# NOTE: You can fix value of QXlsx path of source code.
13+
# QXLSX_PARENTPATH=./
14+
# QXLSX_HEADERPATH=./header/
15+
# QXLSX_SOURCEPATH=./source/
16+
include(../QXlsx/QXlsx.pri)
17+
18+
##########################################################################
19+
# The following define makes your compiler emit warnings if you use
20+
# any feature of Qt which as been marked deprecated (the exact warnings
21+
# depend on your compiler). Please consult the documentation of the
22+
# deprecated API in order to know how to port your code away from it.
23+
DEFINES += QT_DEPRECATED_WARNINGS
24+
25+
##########################################################################
26+
# You can also make your code fail to compile if you use deprecated APIs.
27+
# In order to do so, uncomment the following line.
28+
# You can also select to disable deprecated APIs only up to a certain
29+
# version of Qt.
30+
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000
31+
# disables all the APIs deprecated before Qt 6.0.0
32+
33+
##########################################################################
34+
# source code
35+
36+
SOURCES += \
37+
main.cpp
38+
39+
# RESOURCES += \
40+
# test.qrc
41+
42+
RESOURCES += \
43+
test.qrc
44+
45+

csv/main.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// main.cpp
2+
3+
#include <iostream>
4+
// using namespace std;
5+
6+
#include <QCoreApplication>
7+
#include <QDebug>
8+
#include <QVariant>
9+
#include <QVector>
10+
#include <QtCore>
11+
#include <QtGlobal>
12+
13+
#include "xlsxcellrange.h"
14+
#include "xlsxchart.h"
15+
#include "xlsxchartsheet.h"
16+
#include "xlsxdocument.h"
17+
#include "xlsxrichstring.h"
18+
#include "xlsxworkbook.h"
19+
20+
21+
int main(int argc, char *argv[])
22+
{
23+
QCoreApplication app(argc, argv);
24+
25+
26+
{
27+
using namespace QXlsx;
28+
29+
QString xlsxFileName = ":/test.xlsx";
30+
QXlsx::Document xlsxDoc(xlsxFileName);
31+
if (!xlsxDoc.isLoadPackage()) {
32+
return 0; // failed to load
33+
}
34+
35+
QString csvFileName = "hello.csv";
36+
if ( xlsxDoc.saveAsCsv(csvFileName) ){
37+
qDebug() << "save as csv file";
38+
}
39+
40+
}
41+
42+
43+
return 0;
44+
}

csv/test.qrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!DOCTYPE RCC>
2+
<RCC version="1.0">
3+
<qresource>
4+
<file>test.xlsx</file>
5+
</qresource>
6+
</RCC>

csv/test.xlsx

4.35 KB
Binary file not shown.

0 commit comments

Comments
 (0)