mirror of
https://github.com/QtExcel/QXlsx.git
synced 2025-01-16 04:42:53 +08:00
new function for saving to csv file
This commit is contained in:
parent
db36c652ed
commit
14d07ebdc0
@ -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);
|
||||
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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
|
||||
@ -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);
|
||||
|
45
csv/csv.pro
Normal file
45
csv/csv.pro
Normal file
@ -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
csv/main.cpp
Normal file
44
csv/main.cpp
Normal file
@ -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
csv/test.qrc
Normal file
6
csv/test.qrc
Normal file
@ -0,0 +1,6 @@
|
||||
<!DOCTYPE RCC>
|
||||
<RCC version="1.0">
|
||||
<qresource>
|
||||
<file>test.xlsx</file>
|
||||
</qresource>
|
||||
</RCC>
|
BIN
csv/test.xlsx
Normal file
BIN
csv/test.xlsx
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user