Skip to content

Commit

Permalink
Merge pull request #345 from j2doll/j2doll/csv
Browse files Browse the repository at this point in the history
- branch J2doll/csv
- new function for saving as csv file format (testing now)
  • Loading branch information
j2doll authored Aug 8, 2024
2 parents db36c65 + f1bd57e commit f2594e0
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 23 deletions.
2 changes: 2 additions & 0 deletions QXlsx/header/xlsxdocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions QXlsx/header/xlsxdocument_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
115 changes: 115 additions & 0 deletions QXlsx/source/xlsxdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<CellLocation> clList = wsheet->getFullCells(&maxRow, &maxCol);

QVector<QVector<QString>> cellValues;
for (int rc = 0; rc < maxRow; rc++)
{
QVector<QString> 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<Cell> 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
Expand Down Expand Up @@ -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);
Expand Down
25 changes: 10 additions & 15 deletions README.RU.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

9 changes: 2 additions & 7 deletions README.ko.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) 참조

## 컨트리뷰터
Expand Down Expand Up @@ -67,3 +61,4 @@

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

5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
- See [tested environments](TestEnv.md) for more information.

## Contributions
- See [contributors](https://github.com/QtExcel/QXlsx/graphs/contributors).

<a href="https://github.com/QtExcel/QXlsx/graphs/contributors">
<img src="https://contrib.rocks/image?repo=QtExcel/QXlsx" />
</a>

## License and links
- QXlsx is under MIT license. [https://github.com/QtExcel/QXlsx](https://github.com/QtExcel/QXlsx)
Expand Down
45 changes: 45 additions & 0 deletions csv/csv.pro
Original file line number Diff line number Diff line change
@@ -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


44 changes: 44 additions & 0 deletions csv/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// main.cpp

#include <iostream>
// using namespace std;

#include <QCoreApplication>
#include <QDebug>
#include <QVariant>
#include <QVector>
#include <QtCore>
#include <QtGlobal>

#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;
}
6 changes: 6 additions & 0 deletions csv/test.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!DOCTYPE RCC>
<RCC version="1.0">
<qresource>
<file>test.xlsx</file>
</qresource>
</RCC>
Binary file added csv/test.xlsx
Binary file not shown.

0 comments on commit f2594e0

Please sign in to comment.