From 9739020b1f9e99e54a8a669016d24e09467ab21f Mon Sep 17 00:00:00 2001 From: Debao Zhang Date: Fri, 30 Aug 2013 15:16:57 +0800 Subject: [PATCH] Add document property support Use QObject::setProperty() to do so --- .../documentproperty/documentproperty.pro | 7 +++++ examples/xlsx/documentproperty/main.cpp | 28 +++++++++++++++++ examples/xlsx/xlsx.pro | 3 +- src/xlsx/xlsxdocprops.cpp | 30 +++++++++++++------ src/xlsx/xlsxpackage.cpp | 3 ++ src/xlsx/xlsxworkbook.cpp | 25 ++++++++++++++++ src/xlsx/xlsxworkbook.h | 4 +++ 7 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 examples/xlsx/documentproperty/documentproperty.pro create mode 100644 examples/xlsx/documentproperty/main.cpp diff --git a/examples/xlsx/documentproperty/documentproperty.pro b/examples/xlsx/documentproperty/documentproperty.pro new file mode 100644 index 0000000..cb017ea --- /dev/null +++ b/examples/xlsx/documentproperty/documentproperty.pro @@ -0,0 +1,7 @@ +TARGET = ducumentproperty + +#include(../../../src/xlsx/qtxlsx.pri) +QT+=xlsx + +SOURCES += main.cpp + diff --git a/examples/xlsx/documentproperty/main.cpp b/examples/xlsx/documentproperty/main.cpp new file mode 100644 index 0000000..6cb38cb --- /dev/null +++ b/examples/xlsx/documentproperty/main.cpp @@ -0,0 +1,28 @@ +#include +#include "xlsxworkbook.h" + +#ifdef Q_OS_MAC +# define DATA_PATH "../../../" +#else +# define DATA_PATH "./" +#endif + +int main() +{ + QXlsx::Workbook workbook; +/* + 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 +*/ + workbook.setProperty("title", "This is an example spreadsheet"); + workbook.setProperty("subject", "With document properties"); + workbook.setProperty("creator", "Debao Zhang"); + workbook.setProperty("company", "HMICN"); + workbook.setProperty("category", "Example spreadsheets"); + workbook.setProperty("keywords", "Sample, Example, Properties"); + workbook.setProperty("description", "Created with Qt Xlsx"); + + workbook.save(DATA_PATH"Test.xlsx"); + return 0; +} diff --git a/examples/xlsx/xlsx.pro b/examples/xlsx/xlsx.pro index a3a011e..ce62f3a 100644 --- a/examples/xlsx/xlsx.pro +++ b/examples/xlsx/xlsx.pro @@ -1,3 +1,4 @@ TEMPLATE = subdirs -SUBDIRS = hello style +SUBDIRS = hello style \ + documentproperty diff --git a/src/xlsx/xlsxdocprops.cpp b/src/xlsx/xlsxdocprops.cpp index 234f659..0db4e55 100755 --- a/src/xlsx/xlsxdocprops.cpp +++ b/src/xlsx/xlsxdocprops.cpp @@ -28,6 +28,7 @@ #include #include #include +#include namespace QXlsx { DocProps::DocProps(QObject *parent) : @@ -83,11 +84,15 @@ void DocProps::saveToXmlFile_App(QIODevice *device) writer.writeEndElement();//vt:vector writer.writeEndElement();//TitlesOfParts - writer.writeTextElement(QStringLiteral("Company"), QStringLiteral("")); + if (property("manager").isValid()) + writer.writeTextElement(QStringLiteral("Manager"), property("manager").toString()); + //Not like "manager", "company" always exists for Excel generated file. + writer.writeTextElement(QStringLiteral("Company"), property("company").isValid() ? property("company").toString() : QString()); writer.writeTextElement(QStringLiteral("LinksUpToDate"), QStringLiteral("false")); writer.writeTextElement(QStringLiteral("SharedDoc"), QStringLiteral("false")); writer.writeTextElement(QStringLiteral("HyperlinksChanged"), QStringLiteral("false")); writer.writeTextElement(QStringLiteral("AppVersion"), QStringLiteral("12.0000")); + writer.writeEndElement(); //Properties writer.writeEndDocument(); } @@ -103,12 +108,17 @@ void DocProps::saveToXmlFile_Core(QIODevice *device) writer.writeAttribute(QStringLiteral("xmlns:dcterms"), QStringLiteral("http://purl.org/dc/terms/")); writer.writeAttribute(QStringLiteral("xmlns:dcmitype"), QStringLiteral("http://purl.org/dc/dcmitype/")); writer.writeAttribute(QStringLiteral("xmlns:xsi"), QStringLiteral("http://www.w3.org/2001/XMLSchema-instance")); - writer.writeTextElement(QStringLiteral("dc:title"), QStringLiteral("")); - writer.writeTextElement(QStringLiteral("dc:subject"), QStringLiteral("")); - writer.writeTextElement(QStringLiteral("dc:creator"), QStringLiteral("QXlsxWriter")); - writer.writeTextElement(QStringLiteral("cp:keywords"), QStringLiteral("")); - writer.writeTextElement(QStringLiteral("dc:description"), QStringLiteral("")); - writer.writeTextElement(QStringLiteral("cp:lastModifiedBy"), QStringLiteral("")); + if (property("title").isValid()) + writer.writeTextElement(QStringLiteral("dc:title"), property("title").toString()); + if (property("subject").isValid()) + writer.writeTextElement(QStringLiteral("dc:subject"), property("subject").toString()); + writer.writeTextElement(QStringLiteral("dc:creator"), property("creator").isValid() ? property("creator").toString() : QStringLiteral("Qt Xlsx Library")); + + if (property("keywords").isValid()) + writer.writeTextElement(QStringLiteral("cp:keywords"), property("keywords").toString()); + if (property("description").isValid()) + writer.writeTextElement(QStringLiteral("dc:description"), property("description").toString()); + writer.writeTextElement(QStringLiteral("cp:lastModifiedBy"), property("creator").isValid() ? property("creator").toString() : QStringLiteral("Qt Xlsx Library")); writer.writeStartElement(QStringLiteral("dcterms:created")); writer.writeAttribute(QStringLiteral("xsi:type"), QStringLiteral("dcterms:W3CDTF")); @@ -120,8 +130,10 @@ void DocProps::saveToXmlFile_Core(QIODevice *device) writer.writeCharacters(QDateTime::currentDateTime().toString(Qt::ISODate)); writer.writeEndElement();//dcterms:created - writer.writeTextElement(QStringLiteral("cp:category"), QStringLiteral("")); - writer.writeTextElement(QStringLiteral("cp:contentStatus"), QStringLiteral("")); + if (property("category").isValid()) + writer.writeTextElement(QStringLiteral("cp:category"), property("category").toString()); + if (property("status").isValid()) + writer.writeTextElement(QStringLiteral("cp:contentStatus"), property("status").toString()); writer.writeEndElement(); //cp:coreProperties writer.writeEndDocument(); } diff --git a/src/xlsx/xlsxpackage.cpp b/src/xlsx/xlsxpackage.cpp index 773b656..2ee1ae5 100644 --- a/src/xlsx/xlsxpackage.cpp +++ b/src/xlsx/xlsxpackage.cpp @@ -170,6 +170,9 @@ void Package::writeDocPropsFiles(ZipWriter &zipWriter) { DocProps props; + foreach (QByteArray name, m_workbook->dynamicPropertyNames()) + props.setProperty(name.data(), m_workbook->property(name.data())); + if (m_worksheet_count) props.addHeadingPair(QStringLiteral("Worksheets"), m_worksheet_count); if (m_chartsheet_count) diff --git a/src/xlsx/xlsxworkbook.cpp b/src/xlsx/xlsxworkbook.cpp index 62296e3..bf12ef1 100755 --- a/src/xlsx/xlsxworkbook.cpp +++ b/src/xlsx/xlsxworkbook.cpp @@ -51,6 +51,31 @@ 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)) { diff --git a/src/xlsx/xlsxworkbook.h b/src/xlsx/xlsxworkbook.h index c8fefcf..dae9103 100755 --- a/src/xlsx/xlsxworkbook.h +++ b/src/xlsx/xlsxworkbook.h @@ -61,6 +61,10 @@ 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: