Using QXlsx::Document instead of QXlsx::Workbook
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
#include <QtCore>
|
#include <QtCore>
|
||||||
#include "xlsxworkbook.h"
|
#include "xlsxdocument.h"
|
||||||
|
|
||||||
#ifdef Q_OS_MAC
|
#ifdef Q_OS_MAC
|
||||||
# define DATA_PATH "../../../"
|
# define DATA_PATH "../../../"
|
||||||
@@ -9,20 +9,20 @@
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
QXlsx::Workbook workbook;
|
QXlsx::Document xlsx;
|
||||||
/*
|
/*
|
||||||
These properties are visible when you use the
|
These properties are visible when you use the
|
||||||
Office Button -> Prepare -> Properties option in Excel and are also
|
Office Button -> Prepare -> Properties option in Excel and are also
|
||||||
available to external applications that read or index windows files
|
available to external applications that read or index windows files
|
||||||
*/
|
*/
|
||||||
workbook.setProperty("title", "This is an example spreadsheet");
|
xlsx.setDocumentProperty("title", "This is an example spreadsheet");
|
||||||
workbook.setProperty("subject", "With document properties");
|
xlsx.setDocumentProperty("subject", "With document properties");
|
||||||
workbook.setProperty("creator", "Debao Zhang");
|
xlsx.setDocumentProperty("creator", "Debao Zhang");
|
||||||
workbook.setProperty("company", "HMICN");
|
xlsx.setDocumentProperty("company", "HMICN");
|
||||||
workbook.setProperty("category", "Example spreadsheets");
|
xlsx.setDocumentProperty("category", "Example spreadsheets");
|
||||||
workbook.setProperty("keywords", "Sample, Example, Properties");
|
xlsx.setDocumentProperty("keywords", "Sample, Example, Properties");
|
||||||
workbook.setProperty("description", "Created with Qt Xlsx");
|
xlsx.setDocumentProperty("description", "Created with Qt Xlsx");
|
||||||
|
|
||||||
workbook.save(DATA_PATH"Test.xlsx");
|
xlsx.saveAs(DATA_PATH"Test.xlsx");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
#include <QtCore>
|
#include <QtCore>
|
||||||
#include "xlsxworkbook.h"
|
#include "xlsxdocument.h"
|
||||||
#include "xlsxworksheet.h"
|
|
||||||
|
|
||||||
#ifdef Q_OS_MAC
|
#ifdef Q_OS_MAC
|
||||||
# define DATA_PATH "../../../"
|
# define DATA_PATH "../../../"
|
||||||
@@ -10,31 +9,30 @@
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
QXlsx::Workbook workbook;
|
QXlsx::Document xlsx;
|
||||||
QXlsx::Worksheet *sheet = workbook.addWorksheet();
|
|
||||||
sheet->write("A1", "Hello Qt!");
|
|
||||||
sheet->write("B3", 12345);
|
|
||||||
sheet->write("C5", "=44+33");
|
|
||||||
sheet->write("D7", true);
|
|
||||||
sheet->write("E1", "http://qt-project.org");
|
|
||||||
|
|
||||||
QXlsx::Worksheet *sheet2 = workbook.addWorksheet();
|
//Write to first worksheet.
|
||||||
|
xlsx.write("A1", "Hello Qt!");
|
||||||
|
xlsx.write("B3", 12345);
|
||||||
|
xlsx.write("C5", "=44+33");
|
||||||
|
xlsx.write("D7", true);
|
||||||
|
xlsx.write("E1", "http://qt-project.org");
|
||||||
|
|
||||||
|
//Create another worksheet.
|
||||||
|
xlsx.addWorksheet();
|
||||||
//Rows and columns are zero indexed.
|
//Rows and columns are zero indexed.
|
||||||
//The first cell in a worksheet, "A1", is (0, 0).
|
//The first cell in a worksheet, "A1", is (0, 0).
|
||||||
sheet2->write(0, 0, "First");
|
xlsx.write(0, 0, "First");
|
||||||
sheet2->write(1, 0, "Second");
|
xlsx.write(1, 0, "Second");
|
||||||
sheet2->write(2, 0, "Third");
|
xlsx.write(2, 0, "Third");
|
||||||
sheet2->write(3, 0, "Fourth");
|
xlsx.write(3, 0, "Fourth");
|
||||||
sheet2->write(4, 0, "Total");
|
xlsx.write(4, 0, "Total");
|
||||||
sheet2->write(0, 1, 100);
|
xlsx.write(0, 1, 100);
|
||||||
sheet2->write(1, 1, 200);
|
xlsx.write(1, 1, 200);
|
||||||
sheet2->write(2, 1, 300);
|
xlsx.write(2, 1, 300);
|
||||||
sheet2->write(3, 1, 400);
|
xlsx.write(3, 1, 400);
|
||||||
sheet2->write(4, 1, "=SUM(B1:B4)");
|
xlsx.write(4, 1, "=SUM(B1:B4)");
|
||||||
|
|
||||||
workbook.setActivedWorksheet(1);
|
xlsx.saveAs(DATA_PATH"Test.xlsx");
|
||||||
|
|
||||||
workbook.save(DATA_PATH"Test.xlsx");
|
|
||||||
workbook.save(DATA_PATH"Test.zip");
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
#include <QtGui>
|
#include <QtGui>
|
||||||
#include "xlsxworkbook.h"
|
#include "xlsxdocument.h"
|
||||||
#include "xlsxworksheet.h"
|
|
||||||
|
|
||||||
#ifdef Q_OS_MAC
|
#ifdef Q_OS_MAC
|
||||||
# define DATA_PATH "../../../"
|
# define DATA_PATH "../../../"
|
||||||
@@ -12,14 +11,13 @@ int main(int argc, char** argv)
|
|||||||
{
|
{
|
||||||
QGuiApplication(argc, argv);
|
QGuiApplication(argc, argv);
|
||||||
|
|
||||||
QXlsx::Workbook workbook;
|
QXlsx::Document xlsx;
|
||||||
QXlsx::Worksheet *sheet = workbook.addWorksheet();
|
|
||||||
QImage image(400, 300, QImage::Format_RGB32);
|
QImage image(400, 300, QImage::Format_RGB32);
|
||||||
image.fill(Qt::green);
|
image.fill(Qt::green);
|
||||||
sheet->insertImage(5, 5, image);
|
xlsx.insertImage(5, 5, image);
|
||||||
|
|
||||||
workbook.save(DATA_PATH"Test.xlsx");
|
xlsx.saveAs(DATA_PATH"Test.xlsx");
|
||||||
// workbook.save(DATA_PATH"Test2.zip");
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
#include <QtGui>
|
#include <QtGui>
|
||||||
#include "xlsxworkbook.h"
|
#include "xlsxdocument.h"
|
||||||
#include "xlsxworksheet.h"
|
|
||||||
|
|
||||||
#ifdef Q_OS_MAC
|
#ifdef Q_OS_MAC
|
||||||
# define DATA_PATH "../../../"
|
# define DATA_PATH "../../../"
|
||||||
@@ -12,16 +11,15 @@ int main(int argc, char** argv)
|
|||||||
{
|
{
|
||||||
QGuiApplication(argc, argv);
|
QGuiApplication(argc, argv);
|
||||||
|
|
||||||
QXlsx::Workbook workbook;
|
QXlsx::Document xlsx;
|
||||||
QXlsx::Worksheet *sheet = workbook.addWorksheet();
|
|
||||||
|
|
||||||
sheet->write("B1", "Merge Cells");
|
xlsx.write("B1", "Merge Cells");
|
||||||
sheet->mergeCells("B1:B5");
|
xlsx.mergeCells("B1:B5");
|
||||||
|
|
||||||
sheet->write("E2", "Merge Cells 2");
|
xlsx.write("E2", "Merge Cells 2");
|
||||||
sheet->mergeCells("E2:G4");
|
xlsx.mergeCells("E2:G4");
|
||||||
|
|
||||||
workbook.save(DATA_PATH"Test.xlsx");
|
xlsx.saveAs(DATA_PATH"Test.xlsx");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
#include <QtCore>
|
#include <QtCore>
|
||||||
#include "xlsxworkbook.h"
|
#include "xlsxdocument.h"
|
||||||
#include "xlsxworksheet.h"
|
|
||||||
#include "xlsxformat.h"
|
#include "xlsxformat.h"
|
||||||
|
|
||||||
#ifdef Q_OS_MAC
|
#ifdef Q_OS_MAC
|
||||||
@@ -11,44 +10,42 @@
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
QXlsx::Workbook workbook;
|
QXlsx::Document xlsx;
|
||||||
QXlsx::Worksheet *sheet = workbook.addWorksheet();
|
QXlsx::Format *format1 = xlsx.createFormat();
|
||||||
|
|
||||||
QXlsx::Format *format1 = workbook.addFormat();
|
|
||||||
format1->setFontColor(QColor(Qt::red));
|
format1->setFontColor(QColor(Qt::red));
|
||||||
format1->setFontSize(15);
|
format1->setFontSize(15);
|
||||||
format1->setHorizontalAlignment(QXlsx::Format::AlignHCenter);
|
format1->setHorizontalAlignment(QXlsx::Format::AlignHCenter);
|
||||||
format1->setBorderStyle(QXlsx::Format::BorderDashDotDot);
|
format1->setBorderStyle(QXlsx::Format::BorderDashDotDot);
|
||||||
sheet->write("A1", "Hello Qt!", format1);
|
xlsx.write("A1", "Hello Qt!", format1);
|
||||||
sheet->write("B3", 12345, format1);
|
xlsx.write("B3", 12345, format1);
|
||||||
|
|
||||||
QXlsx::Format *format2 = workbook.addFormat();
|
QXlsx::Format *format2 = xlsx.createFormat();
|
||||||
format2->setFontBold(true);
|
format2->setFontBold(true);
|
||||||
format2->setFontUnderline(QXlsx::Format::FontUnderlineDouble);
|
format2->setFontUnderline(QXlsx::Format::FontUnderlineDouble);
|
||||||
format2->setFillPattern(QXlsx::Format::PatternLightUp);
|
format2->setFillPattern(QXlsx::Format::PatternLightUp);
|
||||||
sheet->write("C5", "=44+33", format2);
|
xlsx.write("C5", "=44+33", format2);
|
||||||
sheet->write("D7", true, format2);
|
xlsx.write("D7", true, format2);
|
||||||
|
|
||||||
QXlsx::Format *format3 = workbook.addFormat();
|
QXlsx::Format *format3 = xlsx.createFormat();
|
||||||
format3->setFontBold(true);
|
format3->setFontBold(true);
|
||||||
format3->setFontColor(QColor(Qt::blue));
|
format3->setFontColor(QColor(Qt::blue));
|
||||||
format3->setFontSize(20);
|
format3->setFontSize(20);
|
||||||
sheet->write(10, 0, "Hello Row Style");
|
xlsx.write(10, 0, "Hello Row Style");
|
||||||
sheet->write(10, 5, "Blue Color");
|
xlsx.write(10, 5, "Blue Color");
|
||||||
sheet->setRow(10, 40, format3);
|
xlsx.setRow(10, 40, format3);
|
||||||
|
|
||||||
QXlsx::Format *format4 = workbook.addFormat();
|
QXlsx::Format *format4 = xlsx.createFormat();
|
||||||
format4->setFontBold(true);
|
format4->setFontBold(true);
|
||||||
format4->setFontColor(QColor(Qt::magenta));
|
format4->setFontColor(QColor(Qt::magenta));
|
||||||
for (int row=20; row<40; row++)
|
for (int row=20; row<40; row++)
|
||||||
for (int col=8; col<15; col++)
|
for (int col=8; col<15; col++)
|
||||||
sheet->write(row, col, row+col);
|
xlsx.write(row, col, row+col);
|
||||||
sheet->setColumn(8, 15, 5.0, format4);
|
xlsx.setColumn(8, 15, 5.0, format4);
|
||||||
|
|
||||||
QXlsx::Format *format5 = workbook.addFormat();
|
QXlsx::Format *format5 = xlsx.createFormat();
|
||||||
format5->setNumberFormat(22);
|
format5->setNumberFormat(22);
|
||||||
sheet->write("A5", QDate(2013, 8, 29), format5);
|
xlsx.write("A5", QDate(2013, 8, 29), format5);
|
||||||
|
|
||||||
workbook.save(DATA_PATH"TestStyle.xlsx");
|
xlsx.saveAs(DATA_PATH"TestStyle.xlsx");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+150
-4
@@ -2,20 +2,27 @@
|
|||||||
#include "xlsxdocument_p.h"
|
#include "xlsxdocument_p.h"
|
||||||
#include "xlsxworkbook.h"
|
#include "xlsxworkbook.h"
|
||||||
#include "xlsxworksheet.h"
|
#include "xlsxworksheet.h"
|
||||||
|
#include "xlsxpackage_p.h"
|
||||||
|
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
|
#include <QPointF>
|
||||||
|
|
||||||
namespace QXlsx {
|
namespace QXlsx {
|
||||||
|
|
||||||
DocumentPrivate::DocumentPrivate(Document *p) :
|
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)
|
bool DocumentPrivate::loadPackage(QIODevice *device)
|
||||||
{
|
{
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,6 +35,7 @@ bool DocumentPrivate::loadPackage(QIODevice *device)
|
|||||||
Document::Document(QObject *parent) :
|
Document::Document(QObject *parent) :
|
||||||
QObject(parent), d_ptr(new DocumentPrivate(this))
|
QObject(parent), d_ptr(new DocumentPrivate(this))
|
||||||
{
|
{
|
||||||
|
d_ptr->init();
|
||||||
}
|
}
|
||||||
|
|
||||||
Document::Document(const QString &name, QObject *parent) :
|
Document::Document(const QString &name, QObject *parent) :
|
||||||
@@ -39,6 +47,7 @@ Document::Document(const QString &name, QObject *parent) :
|
|||||||
if (xlsx.open(QFile::ReadOnly))
|
if (xlsx.open(QFile::ReadOnly))
|
||||||
d_ptr->loadPackage(&xlsx);
|
d_ptr->loadPackage(&xlsx);
|
||||||
}
|
}
|
||||||
|
d_ptr->init();
|
||||||
}
|
}
|
||||||
|
|
||||||
Document::Document(QIODevice *device, QObject *parent) :
|
Document::Document(QIODevice *device, QObject *parent) :
|
||||||
@@ -46,21 +55,158 @@ Document::Document(QIODevice *device, QObject *parent) :
|
|||||||
{
|
{
|
||||||
if (device && device->isReadable())
|
if (device && device->isReadable())
|
||||||
d_ptr->loadPackage(device);
|
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()
|
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)
|
bool Document::saveAs(const QString &name)
|
||||||
{
|
{
|
||||||
|
QFile file(name);
|
||||||
|
if (file.open(QIODevice::WriteOnly))
|
||||||
|
return saveAs(&file);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Document::saveAs(QIODevice *device)
|
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()
|
Document::~Document()
|
||||||
|
|||||||
+27
-2
@@ -4,11 +4,15 @@
|
|||||||
#include "xlsxglobal.h"
|
#include "xlsxglobal.h"
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
class QIODevice;
|
class QIODevice;
|
||||||
|
class QImage;
|
||||||
|
|
||||||
namespace QXlsx {
|
namespace QXlsx {
|
||||||
|
|
||||||
class Workbook;
|
class Workbook;
|
||||||
class Worksheet;
|
class Worksheet;
|
||||||
|
class Package;
|
||||||
|
class Format;
|
||||||
|
|
||||||
class DocumentPrivate;
|
class DocumentPrivate;
|
||||||
class Q_XLSX_EXPORT Document : public QObject
|
class Q_XLSX_EXPORT Document : public QObject
|
||||||
{
|
{
|
||||||
@@ -17,15 +21,36 @@ class Q_XLSX_EXPORT Document : public QObject
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Document(QObject *parent = 0);
|
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(QIODevice *device, QObject *parent=0);
|
||||||
~Document();
|
~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 save();
|
||||||
bool saveAs(const QString &name);
|
bool saveAs(const QString &xlsXname);
|
||||||
bool saveAs(QIODevice *device);
|
bool saveAs(QIODevice *device);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
friend class Package;
|
||||||
Q_DISABLE_COPY(Document)
|
Q_DISABLE_COPY(Document)
|
||||||
DocumentPrivate * const d_ptr;
|
DocumentPrivate * const d_ptr;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,6 +2,9 @@
|
|||||||
#define XLSXDOCUMENT_P_H
|
#define XLSXDOCUMENT_P_H
|
||||||
|
|
||||||
#include "xlsxdocument.h"
|
#include "xlsxdocument.h"
|
||||||
|
#include "xlsxworkbook.h"
|
||||||
|
|
||||||
|
#include <QMap>
|
||||||
|
|
||||||
namespace QXlsx {
|
namespace QXlsx {
|
||||||
|
|
||||||
@@ -10,6 +13,7 @@ class DocumentPrivate
|
|||||||
Q_DECLARE_PUBLIC(Document)
|
Q_DECLARE_PUBLIC(Document)
|
||||||
public:
|
public:
|
||||||
DocumentPrivate(Document *p);
|
DocumentPrivate(Document *p);
|
||||||
|
void init();
|
||||||
|
|
||||||
bool loadPackage(QIODevice *device);
|
bool loadPackage(QIODevice *device);
|
||||||
|
|
||||||
@@ -17,6 +21,8 @@ public:
|
|||||||
const QString defaultPackageName; //default name when package name not specified
|
const QString defaultPackageName; //default name when package name not specified
|
||||||
QString packageName; //name of the .xlsx file
|
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.
|
elements of the package and writes them into the XLSX file.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Package::Package(Workbook *workbook) :
|
Package::Package(Document *document) :
|
||||||
m_workbook(workbook)
|
m_document(document)
|
||||||
{
|
{
|
||||||
|
m_workbook = m_document->workbook();
|
||||||
m_worksheet_count = 0;
|
m_worksheet_count = 0;
|
||||||
m_chartsheet_count = 0;
|
m_chartsheet_count = 0;
|
||||||
foreach (Worksheet *sheet, m_workbook->worksheets()) {
|
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);
|
ZipReader zipReader(packageDevice);
|
||||||
QStringList filePaths = zipReader.filePaths();
|
QStringList filePaths = zipReader.filePaths();
|
||||||
|
|
||||||
|
if (!filePaths.contains(QLatin1String("_rels/.rel")))
|
||||||
|
return false;
|
||||||
|
Relationships rootRels = readRelsFile(zipReader, QStringLiteral("_rels/.rel"));
|
||||||
|
|
||||||
|
|
||||||
return false;
|
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())
|
if (zipWriter.error())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@@ -208,8 +225,8 @@ void Package::writeDocPropsAppFile(ZipWriter &zipWriter)
|
|||||||
{
|
{
|
||||||
DocPropsApp props;
|
DocPropsApp props;
|
||||||
|
|
||||||
foreach (QByteArray name, m_workbook->dynamicPropertyNames())
|
foreach (QString name, m_document->documentPropertyNames())
|
||||||
props.setProperty(name.data(), m_workbook->property(name.data()));
|
props.setProperty(name.toUtf8().data(), m_document->documentProperty(name));
|
||||||
|
|
||||||
if (m_worksheet_count)
|
if (m_worksheet_count)
|
||||||
props.addHeadingPair(QStringLiteral("Worksheets"), m_worksheet_count);
|
props.addHeadingPair(QStringLiteral("Worksheets"), m_worksheet_count);
|
||||||
@@ -239,8 +256,8 @@ void Package::writeDocPropsCoreFile(ZipWriter &zipWriter)
|
|||||||
{
|
{
|
||||||
DocPropsCore props;
|
DocPropsCore props;
|
||||||
|
|
||||||
foreach (QByteArray name, m_workbook->dynamicPropertyNames())
|
foreach (QString name, m_document->documentPropertyNames())
|
||||||
props.setProperty(name.data(), m_workbook->property(name.data()));
|
props.setProperty(name.toUtf8().data(), m_document->documentProperty(name));
|
||||||
|
|
||||||
QByteArray data;
|
QByteArray data;
|
||||||
QBuffer buffer(&data);
|
QBuffer buffer(&data);
|
||||||
|
|||||||
@@ -33,17 +33,21 @@ namespace QXlsx {
|
|||||||
|
|
||||||
class Workbook;
|
class Workbook;
|
||||||
class ZipWriter;
|
class ZipWriter;
|
||||||
|
class ZipReader;
|
||||||
class Document;
|
class Document;
|
||||||
|
class Relationships;
|
||||||
|
|
||||||
class XLSX_AUTOTEST_EXPORT Package
|
class XLSX_AUTOTEST_EXPORT Package
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Package(Workbook *workbook);
|
Package(Document *document);
|
||||||
|
|
||||||
bool parsePackage(QIODevice *packageDevice, Document *document);
|
bool parsePackage(QIODevice *packageDevice);
|
||||||
bool createPackage(const QString &packageName);
|
bool createPackage(QIODevice *package);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
Relationships readRelsFile(ZipReader &reader, const QString &filePath);
|
||||||
|
|
||||||
void writeWorksheetFiles(ZipWriter &zipWriter);
|
void writeWorksheetFiles(ZipWriter &zipWriter);
|
||||||
// void writeChartsheetFiles(ZipWriter &zipWriter);
|
// void writeChartsheetFiles(ZipWriter &zipWriter);
|
||||||
void writeWorkbookFile(ZipWriter &zipWriter);
|
void writeWorkbookFile(ZipWriter &zipWriter);
|
||||||
@@ -66,6 +70,7 @@ private:
|
|||||||
void writeImageFiles(ZipWriter &zipWriter);
|
void writeImageFiles(ZipWriter &zipWriter);
|
||||||
// void writeVbaProjectFiles(ZipWriter &zipWriter);
|
// void writeVbaProjectFiles(ZipWriter &zipWriter);
|
||||||
|
|
||||||
|
Document *m_document;
|
||||||
Workbook *m_workbook;
|
Workbook *m_workbook;
|
||||||
int m_worksheet_count;
|
int m_worksheet_count;
|
||||||
int m_chartsheet_count;
|
int m_chartsheet_count;
|
||||||
|
|||||||
@@ -32,6 +32,8 @@
|
|||||||
#include "xlsxxmlwriter_p.h"
|
#include "xlsxxmlwriter_p.h"
|
||||||
#include "xlsxworksheet_p.h"
|
#include "xlsxworksheet_p.h"
|
||||||
|
|
||||||
|
#include <QFile>
|
||||||
|
|
||||||
namespace QXlsx {
|
namespace QXlsx {
|
||||||
|
|
||||||
WorkbookPrivate::WorkbookPrivate(Workbook *q) :
|
WorkbookPrivate::WorkbookPrivate(Workbook *q) :
|
||||||
@@ -52,31 +54,6 @@ WorkbookPrivate::WorkbookPrivate(Workbook *q) :
|
|||||||
table_count = 0;
|
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) :
|
Workbook::Workbook(QObject *parent) :
|
||||||
QObject(parent), d_ptr(new WorkbookPrivate(this))
|
QObject(parent), d_ptr(new WorkbookPrivate(this))
|
||||||
{
|
{
|
||||||
@@ -88,26 +65,6 @@ Workbook::~Workbook()
|
|||||||
delete d_ptr;
|
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
|
bool Workbook::isDate1904() const
|
||||||
{
|
{
|
||||||
Q_D(const Workbook);
|
Q_D(const Workbook);
|
||||||
@@ -183,6 +140,7 @@ Worksheet *Workbook::insertWorkSheet(int index, const QString &name)
|
|||||||
|
|
||||||
Worksheet *sheet = new Worksheet(worksheetName, this);
|
Worksheet *sheet = new Worksheet(worksheetName, this);
|
||||||
d->worksheets.insert(index, sheet);
|
d->worksheets.insert(index, sheet);
|
||||||
|
d->activesheet = index;
|
||||||
return sheet;
|
return sheet;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -195,6 +153,10 @@ int Workbook::activedWorksheet() const
|
|||||||
void Workbook::setActivedWorksheet(int index)
|
void Workbook::setActivedWorksheet(int index)
|
||||||
{
|
{
|
||||||
Q_D(Workbook);
|
Q_D(Workbook);
|
||||||
|
if (index < 0 || index >= d->worksheets.size()) {
|
||||||
|
//warning
|
||||||
|
return;
|
||||||
|
}
|
||||||
d->activesheet = index;
|
d->activesheet = index;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -63,12 +63,6 @@ public:
|
|||||||
bool isStringsToNumbersEnabled() const;
|
bool isStringsToNumbersEnabled() const;
|
||||||
void setStringsToNumbersEnabled(bool enable=true);
|
void setStringsToNumbersEnabled(bool enable=true);
|
||||||
|
|
||||||
#ifdef Q_QDOC
|
|
||||||
bool setProperty(const char *key, const QString &value);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
bool save(const QString &name);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class Package;
|
friend class Package;
|
||||||
friend class Worksheet;
|
friend class Worksheet;
|
||||||
|
|||||||
@@ -35,6 +35,13 @@ ZipWriter::ZipWriter(const QString &filePath, QObject *parent) :
|
|||||||
m_writer->setCompressionPolicy(QZipWriter::NeverCompress);
|
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()
|
ZipWriter::~ZipWriter()
|
||||||
{
|
{
|
||||||
delete m_writer;
|
delete m_writer;
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ class ZipWriter : public QObject
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
explicit ZipWriter(const QString &filePath, QObject *parent = 0);
|
explicit ZipWriter(const QString &filePath, QObject *parent = 0);
|
||||||
|
explicit ZipWriter(QIODevice *device, QObject *parent = 0);
|
||||||
~ZipWriter();
|
~ZipWriter();
|
||||||
|
|
||||||
void addFile(const QString &filePath, QIODevice *device);
|
void addFile(const QString &filePath, QIODevice *device);
|
||||||
|
|||||||
Reference in New Issue
Block a user