Using QXlsx::Document instead of QXlsx::Workbook
This commit is contained in:
+150
-4
@@ -2,20 +2,27 @@
|
||||
#include "xlsxdocument_p.h"
|
||||
#include "xlsxworkbook.h"
|
||||
#include "xlsxworksheet.h"
|
||||
#include "xlsxpackage_p.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QPointF>
|
||||
|
||||
namespace QXlsx {
|
||||
|
||||
DocumentPrivate::DocumentPrivate(Document *p) :
|
||||
q_ptr(p), defaultPackageName(QStringLiteral("Book1"))
|
||||
q_ptr(p), defaultPackageName(QStringLiteral("Book1.xlsx"))
|
||||
{
|
||||
workbook = new Workbook(p);
|
||||
}
|
||||
|
||||
void DocumentPrivate::init()
|
||||
{
|
||||
if (workbook->worksheets().size() == 0)
|
||||
workbook->addWorksheet();
|
||||
}
|
||||
|
||||
bool DocumentPrivate::loadPackage(QIODevice *device)
|
||||
{
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -28,6 +35,7 @@ bool DocumentPrivate::loadPackage(QIODevice *device)
|
||||
Document::Document(QObject *parent) :
|
||||
QObject(parent), d_ptr(new DocumentPrivate(this))
|
||||
{
|
||||
d_ptr->init();
|
||||
}
|
||||
|
||||
Document::Document(const QString &name, QObject *parent) :
|
||||
@@ -39,6 +47,7 @@ Document::Document(const QString &name, QObject *parent) :
|
||||
if (xlsx.open(QFile::ReadOnly))
|
||||
d_ptr->loadPackage(&xlsx);
|
||||
}
|
||||
d_ptr->init();
|
||||
}
|
||||
|
||||
Document::Document(QIODevice *device, QObject *parent) :
|
||||
@@ -46,21 +55,158 @@ Document::Document(QIODevice *device, QObject *parent) :
|
||||
{
|
||||
if (device && device->isReadable())
|
||||
d_ptr->loadPackage(device);
|
||||
d_ptr->init();
|
||||
}
|
||||
|
||||
Format *Document::createFormat()
|
||||
{
|
||||
Q_D(Document);
|
||||
return d->workbook->addFormat();
|
||||
}
|
||||
|
||||
int Document::write(const QString row_column, const QVariant &value, Format *format)
|
||||
{
|
||||
return activedWorksheet()->write(row_column, value, format);
|
||||
}
|
||||
|
||||
int Document::write(int row, int col, const QVariant &value, Format *format)
|
||||
{
|
||||
return activedWorksheet()->write(row, col, value, format);
|
||||
}
|
||||
|
||||
int Document::insertImage(int row, int column, const QImage &image, double xOffset, double yOffset, double xScale, double yScale)
|
||||
{
|
||||
return activedWorksheet()->insertImage(row, column, image, QPointF(xOffset, yOffset), xScale, yScale);
|
||||
}
|
||||
|
||||
int Document::mergeCells(const QString &range)
|
||||
{
|
||||
return activedWorksheet()->mergeCells(range);
|
||||
}
|
||||
|
||||
int Document::unmergeCells(const QString &range)
|
||||
{
|
||||
return activedWorksheet()->unmergeCells(range);
|
||||
}
|
||||
|
||||
bool Document::setRow(int row, double height, Format *format, bool hidden)
|
||||
{
|
||||
return activedWorksheet()->setRow(row, height, format, hidden);
|
||||
}
|
||||
|
||||
bool Document::setColumn(int colFirst, int colLast, double width, Format *format, bool hidden)
|
||||
{
|
||||
return activedWorksheet()->setColumn(colFirst, colLast, width, format, hidden);
|
||||
}
|
||||
|
||||
QString Document::documentProperty(const QString &key) const
|
||||
{
|
||||
Q_D(const Document);
|
||||
if (d->documentProperties.contains(key))
|
||||
return d->documentProperties[key];
|
||||
else
|
||||
return QString();
|
||||
}
|
||||
|
||||
/*!
|
||||
Set the document properties such as Title, Author etc.
|
||||
|
||||
The method can be used to set the document properties of the Excel
|
||||
file created by Qt Xlsx. These properties are visible when you use the
|
||||
Office Button -> Prepare -> Properties option in Excel and are also
|
||||
available to external applications that read or index windows files.
|
||||
|
||||
The properties \a key that can be set are:
|
||||
|
||||
\list
|
||||
\li title
|
||||
\li subject
|
||||
\li creator
|
||||
\li manager
|
||||
\li company
|
||||
\li category
|
||||
\li keywords
|
||||
\li description
|
||||
\li status
|
||||
\endlist
|
||||
*/
|
||||
void Document::setDocumentProperty(const QString &key, const QString &property)
|
||||
{
|
||||
Q_D(Document);
|
||||
d->documentProperties[key] = property;
|
||||
}
|
||||
|
||||
QStringList Document::documentPropertyNames() const
|
||||
{
|
||||
Q_D(const Document);
|
||||
return d->documentProperties.keys();
|
||||
}
|
||||
|
||||
Workbook *Document::workbook() const
|
||||
{
|
||||
Q_D(const Document);
|
||||
return d->workbook;
|
||||
}
|
||||
|
||||
bool Document::addWorksheet(const QString &name)
|
||||
{
|
||||
Q_D(Document);
|
||||
return d->workbook->addWorksheet(name);
|
||||
}
|
||||
|
||||
bool Document::insertWorkSheet(int index, const QString &name)
|
||||
{
|
||||
Q_D(Document);
|
||||
return d->workbook->insertWorkSheet(index, name);
|
||||
}
|
||||
|
||||
Worksheet *Document::activedWorksheet() const
|
||||
{
|
||||
Q_D(const Document);
|
||||
if (d->workbook->worksheets().size() == 0)
|
||||
return 0;
|
||||
|
||||
return d->workbook->worksheets().at(d->workbook->activedWorksheet());
|
||||
}
|
||||
|
||||
int Document::activedWorksheetIndex() const
|
||||
{
|
||||
Q_D(const Document);
|
||||
return d->workbook->activedWorksheet();
|
||||
}
|
||||
|
||||
void Document::setActivedWorksheetIndex(int index)
|
||||
{
|
||||
Q_D(Document);
|
||||
d->workbook->setActivedWorksheet(index);
|
||||
}
|
||||
|
||||
bool Document::save()
|
||||
{
|
||||
return false;
|
||||
Q_D(Document);
|
||||
QString name = d->packageName.isEmpty() ? d->defaultPackageName : d->packageName;
|
||||
|
||||
return saveAs(name);
|
||||
}
|
||||
|
||||
bool Document::saveAs(const QString &name)
|
||||
{
|
||||
QFile file(name);
|
||||
if (file.open(QIODevice::WriteOnly))
|
||||
return saveAs(&file);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Document::saveAs(QIODevice *device)
|
||||
{
|
||||
return false;
|
||||
Q_D(Document);
|
||||
|
||||
// activedWorksheet()->setHidden(false);
|
||||
// activedWorksheet()->setSelected(true);
|
||||
|
||||
//Create the package based on current workbook
|
||||
Package package(this);
|
||||
return package.createPackage(device);
|
||||
}
|
||||
|
||||
Document::~Document()
|
||||
|
||||
+27
-2
@@ -4,11 +4,15 @@
|
||||
#include "xlsxglobal.h"
|
||||
#include <QObject>
|
||||
class QIODevice;
|
||||
class QImage;
|
||||
|
||||
namespace QXlsx {
|
||||
|
||||
class Workbook;
|
||||
class Worksheet;
|
||||
class Package;
|
||||
class Format;
|
||||
|
||||
class DocumentPrivate;
|
||||
class Q_XLSX_EXPORT Document : public QObject
|
||||
{
|
||||
@@ -17,15 +21,36 @@ class Q_XLSX_EXPORT Document : public QObject
|
||||
|
||||
public:
|
||||
explicit Document(QObject *parent = 0);
|
||||
Document(const QString &name, QObject *parent=0);
|
||||
Document(const QString &xlsxName, QObject *parent=0);
|
||||
Document(QIODevice *device, QObject *parent=0);
|
||||
~Document();
|
||||
|
||||
Format *createFormat();
|
||||
int write(const QString cell, const QVariant &value, Format *format=0);
|
||||
int write(int row, int col, const QVariant &value, Format *format=0);
|
||||
int insertImage(int row, int column, const QImage &image, double xOffset=0, double yOffset=0, double xScale=1, double yScale=1);
|
||||
int mergeCells(const QString &range);
|
||||
int unmergeCells(const QString &range);
|
||||
bool setRow(int row, double height, Format* format=0, bool hidden=false);
|
||||
bool setColumn(int colFirst, int colLast, double width, Format* format=0, bool hidden=false);
|
||||
|
||||
QString documentProperty(const QString &name) const;
|
||||
void setDocumentProperty(const QString &name, const QString &property);
|
||||
QStringList documentPropertyNames() const;
|
||||
|
||||
Workbook *workbook() const;
|
||||
bool addWorksheet(const QString &name = QString());
|
||||
bool insertWorkSheet(int index, const QString &name = QString());
|
||||
Worksheet *activedWorksheet() const;
|
||||
int activedWorksheetIndex() const;
|
||||
void setActivedWorksheetIndex(int index);
|
||||
|
||||
bool save();
|
||||
bool saveAs(const QString &name);
|
||||
bool saveAs(const QString &xlsXname);
|
||||
bool saveAs(QIODevice *device);
|
||||
|
||||
private:
|
||||
friend class Package;
|
||||
Q_DISABLE_COPY(Document)
|
||||
DocumentPrivate * const d_ptr;
|
||||
};
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
#define XLSXDOCUMENT_P_H
|
||||
|
||||
#include "xlsxdocument.h"
|
||||
#include "xlsxworkbook.h"
|
||||
|
||||
#include <QMap>
|
||||
|
||||
namespace QXlsx {
|
||||
|
||||
@@ -10,6 +13,7 @@ class DocumentPrivate
|
||||
Q_DECLARE_PUBLIC(Document)
|
||||
public:
|
||||
DocumentPrivate(Document *p);
|
||||
void init();
|
||||
|
||||
bool loadPackage(QIODevice *device);
|
||||
|
||||
@@ -17,6 +21,8 @@ public:
|
||||
const QString defaultPackageName; //default name when package name not specified
|
||||
QString packageName; //name of the .xlsx file
|
||||
|
||||
QMap<QString, QString> documentProperties; //core, app and custom properties
|
||||
Workbook *workbook;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -76,9 +76,10 @@ namespace QXlsx {
|
||||
elements of the package and writes them into the XLSX file.
|
||||
*/
|
||||
|
||||
Package::Package(Workbook *workbook) :
|
||||
m_workbook(workbook)
|
||||
Package::Package(Document *document) :
|
||||
m_document(document)
|
||||
{
|
||||
m_workbook = m_document->workbook();
|
||||
m_worksheet_count = 0;
|
||||
m_chartsheet_count = 0;
|
||||
foreach (Worksheet *sheet, m_workbook->worksheets()) {
|
||||
@@ -89,17 +90,33 @@ Package::Package(Workbook *workbook) :
|
||||
}
|
||||
}
|
||||
|
||||
bool Package::parsePackage(QIODevice *packageDevice, Document *document)
|
||||
bool Package::parsePackage(QIODevice *packageDevice)
|
||||
{
|
||||
ZipReader zipReader(packageDevice);
|
||||
QStringList filePaths = zipReader.filePaths();
|
||||
|
||||
if (!filePaths.contains(QLatin1String("_rels/.rel")))
|
||||
return false;
|
||||
Relationships rootRels = readRelsFile(zipReader, QStringLiteral("_rels/.rel"));
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Package::createPackage(const QString &packageName)
|
||||
Relationships Package::readRelsFile(ZipReader &zipReader, const QString &filePath)
|
||||
{
|
||||
ZipWriter zipWriter(packageName);
|
||||
QByteArray relsData = zipReader.fileData(filePath);
|
||||
QBuffer buffer(&relsData);
|
||||
buffer.open(QIODevice::ReadOnly);
|
||||
|
||||
Relationships rels;
|
||||
rels.loadFromXmlFile(&buffer);
|
||||
return rels;
|
||||
}
|
||||
|
||||
bool Package::createPackage(QIODevice *package)
|
||||
{
|
||||
ZipWriter zipWriter(package);
|
||||
if (zipWriter.error())
|
||||
return false;
|
||||
|
||||
@@ -208,8 +225,8 @@ void Package::writeDocPropsAppFile(ZipWriter &zipWriter)
|
||||
{
|
||||
DocPropsApp props;
|
||||
|
||||
foreach (QByteArray name, m_workbook->dynamicPropertyNames())
|
||||
props.setProperty(name.data(), m_workbook->property(name.data()));
|
||||
foreach (QString name, m_document->documentPropertyNames())
|
||||
props.setProperty(name.toUtf8().data(), m_document->documentProperty(name));
|
||||
|
||||
if (m_worksheet_count)
|
||||
props.addHeadingPair(QStringLiteral("Worksheets"), m_worksheet_count);
|
||||
@@ -239,8 +256,8 @@ void Package::writeDocPropsCoreFile(ZipWriter &zipWriter)
|
||||
{
|
||||
DocPropsCore props;
|
||||
|
||||
foreach (QByteArray name, m_workbook->dynamicPropertyNames())
|
||||
props.setProperty(name.data(), m_workbook->property(name.data()));
|
||||
foreach (QString name, m_document->documentPropertyNames())
|
||||
props.setProperty(name.toUtf8().data(), m_document->documentProperty(name));
|
||||
|
||||
QByteArray data;
|
||||
QBuffer buffer(&data);
|
||||
|
||||
@@ -33,17 +33,21 @@ namespace QXlsx {
|
||||
|
||||
class Workbook;
|
||||
class ZipWriter;
|
||||
class ZipReader;
|
||||
class Document;
|
||||
class Relationships;
|
||||
|
||||
class XLSX_AUTOTEST_EXPORT Package
|
||||
{
|
||||
public:
|
||||
Package(Workbook *workbook);
|
||||
Package(Document *document);
|
||||
|
||||
bool parsePackage(QIODevice *packageDevice, Document *document);
|
||||
bool createPackage(const QString &packageName);
|
||||
bool parsePackage(QIODevice *packageDevice);
|
||||
bool createPackage(QIODevice *package);
|
||||
|
||||
private:
|
||||
Relationships readRelsFile(ZipReader &reader, const QString &filePath);
|
||||
|
||||
void writeWorksheetFiles(ZipWriter &zipWriter);
|
||||
// void writeChartsheetFiles(ZipWriter &zipWriter);
|
||||
void writeWorkbookFile(ZipWriter &zipWriter);
|
||||
@@ -66,7 +70,8 @@ private:
|
||||
void writeImageFiles(ZipWriter &zipWriter);
|
||||
// void writeVbaProjectFiles(ZipWriter &zipWriter);
|
||||
|
||||
Workbook * m_workbook;
|
||||
Document *m_document;
|
||||
Workbook *m_workbook;
|
||||
int m_worksheet_count;
|
||||
int m_chartsheet_count;
|
||||
};
|
||||
|
||||
@@ -32,6 +32,8 @@
|
||||
#include "xlsxxmlwriter_p.h"
|
||||
#include "xlsxworksheet_p.h"
|
||||
|
||||
#include <QFile>
|
||||
|
||||
namespace QXlsx {
|
||||
|
||||
WorkbookPrivate::WorkbookPrivate(Workbook *q) :
|
||||
@@ -52,31 +54,6 @@ WorkbookPrivate::WorkbookPrivate(Workbook *q) :
|
||||
table_count = 0;
|
||||
}
|
||||
|
||||
/*!
|
||||
\fn void Workbook::setProperty(const char *key, const QString &value)
|
||||
|
||||
Set the document properties such as Title, Author etc.
|
||||
|
||||
The method can be used to set the document properties of the Excel
|
||||
file created by Qt Xlsx. These properties are visible when you use the
|
||||
Office Button -> Prepare -> Properties option in Excel and are also
|
||||
available to external applications that read or index windows files.
|
||||
|
||||
The properties \a key that can be set are:
|
||||
|
||||
\list
|
||||
\li title
|
||||
\li subject
|
||||
\li creator
|
||||
\li manager
|
||||
\li company
|
||||
\li category
|
||||
\li keywords
|
||||
\li description
|
||||
\li status
|
||||
\endlist
|
||||
*/
|
||||
|
||||
Workbook::Workbook(QObject *parent) :
|
||||
QObject(parent), d_ptr(new WorkbookPrivate(this))
|
||||
{
|
||||
@@ -88,26 +65,6 @@ Workbook::~Workbook()
|
||||
delete d_ptr;
|
||||
}
|
||||
|
||||
bool Workbook::save(const QString &name)
|
||||
{
|
||||
Q_D(Workbook);
|
||||
|
||||
//Add a default worksheet if non have been added.
|
||||
if (d->worksheets.size() == 0)
|
||||
addWorksheet();
|
||||
|
||||
//Ensure that at least one worksheet has been selected.
|
||||
int actIndex = d->activesheet;
|
||||
if (actIndex < 0 || actIndex >= d->worksheets.size())
|
||||
actIndex = 0;
|
||||
d->worksheets[actIndex]->setHidden(false);
|
||||
d->worksheets[actIndex]->setSelected(true);
|
||||
|
||||
//Create the package based on current workbook
|
||||
Package package(this);
|
||||
return package.createPackage(name);
|
||||
}
|
||||
|
||||
bool Workbook::isDate1904() const
|
||||
{
|
||||
Q_D(const Workbook);
|
||||
@@ -183,6 +140,7 @@ Worksheet *Workbook::insertWorkSheet(int index, const QString &name)
|
||||
|
||||
Worksheet *sheet = new Worksheet(worksheetName, this);
|
||||
d->worksheets.insert(index, sheet);
|
||||
d->activesheet = index;
|
||||
return sheet;
|
||||
}
|
||||
|
||||
@@ -195,6 +153,10 @@ int Workbook::activedWorksheet() const
|
||||
void Workbook::setActivedWorksheet(int index)
|
||||
{
|
||||
Q_D(Workbook);
|
||||
if (index < 0 || index >= d->worksheets.size()) {
|
||||
//warning
|
||||
return;
|
||||
}
|
||||
d->activesheet = index;
|
||||
}
|
||||
|
||||
|
||||
@@ -63,12 +63,6 @@ public:
|
||||
bool isStringsToNumbersEnabled() const;
|
||||
void setStringsToNumbersEnabled(bool enable=true);
|
||||
|
||||
#ifdef Q_QDOC
|
||||
bool setProperty(const char *key, const QString &value);
|
||||
#endif
|
||||
|
||||
bool save(const QString &name);
|
||||
|
||||
private:
|
||||
friend class Package;
|
||||
friend class Worksheet;
|
||||
|
||||
@@ -35,6 +35,13 @@ ZipWriter::ZipWriter(const QString &filePath, QObject *parent) :
|
||||
m_writer->setCompressionPolicy(QZipWriter::NeverCompress);
|
||||
}
|
||||
|
||||
ZipWriter::ZipWriter(QIODevice *device, QObject *parent) :
|
||||
QObject(parent)
|
||||
{
|
||||
m_writer = new QZipWriter(device);
|
||||
m_writer->setCompressionPolicy(QZipWriter::NeverCompress);
|
||||
}
|
||||
|
||||
ZipWriter::~ZipWriter()
|
||||
{
|
||||
delete m_writer;
|
||||
|
||||
@@ -36,6 +36,7 @@ class ZipWriter : public QObject
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit ZipWriter(const QString &filePath, QObject *parent = 0);
|
||||
explicit ZipWriter(QIODevice *device, QObject *parent = 0);
|
||||
~ZipWriter();
|
||||
|
||||
void addFile(const QString &filePath, QIODevice *device);
|
||||
|
||||
Reference in New Issue
Block a user