Add document property support

Use QObject::setProperty() to do so
This commit is contained in:
Debao Zhang
2013-08-30 15:16:57 +08:00
parent b80286d7b7
commit 9739020b1f
7 changed files with 90 additions and 10 deletions
@@ -0,0 +1,7 @@
TARGET = ducumentproperty
#include(../../../src/xlsx/qtxlsx.pri)
QT+=xlsx
SOURCES += main.cpp
+28
View File
@@ -0,0 +1,28 @@
#include <QtCore>
#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;
}
+2 -1
View File
@@ -1,3 +1,4 @@
TEMPLATE = subdirs TEMPLATE = subdirs
SUBDIRS = hello style SUBDIRS = hello style \
documentproperty
+21 -9
View File
@@ -28,6 +28,7 @@
#include <QDir> #include <QDir>
#include <QFile> #include <QFile>
#include <QDateTime> #include <QDateTime>
#include <QVariant>
namespace QXlsx { namespace QXlsx {
DocProps::DocProps(QObject *parent) : DocProps::DocProps(QObject *parent) :
@@ -83,11 +84,15 @@ void DocProps::saveToXmlFile_App(QIODevice *device)
writer.writeEndElement();//vt:vector writer.writeEndElement();//vt:vector
writer.writeEndElement();//TitlesOfParts 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("LinksUpToDate"), QStringLiteral("false"));
writer.writeTextElement(QStringLiteral("SharedDoc"), QStringLiteral("false")); writer.writeTextElement(QStringLiteral("SharedDoc"), QStringLiteral("false"));
writer.writeTextElement(QStringLiteral("HyperlinksChanged"), QStringLiteral("false")); writer.writeTextElement(QStringLiteral("HyperlinksChanged"), QStringLiteral("false"));
writer.writeTextElement(QStringLiteral("AppVersion"), QStringLiteral("12.0000")); writer.writeTextElement(QStringLiteral("AppVersion"), QStringLiteral("12.0000"));
writer.writeEndElement(); //Properties writer.writeEndElement(); //Properties
writer.writeEndDocument(); 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:dcterms"), QStringLiteral("http://purl.org/dc/terms/"));
writer.writeAttribute(QStringLiteral("xmlns:dcmitype"), QStringLiteral("http://purl.org/dc/dcmitype/")); 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.writeAttribute(QStringLiteral("xmlns:xsi"), QStringLiteral("http://www.w3.org/2001/XMLSchema-instance"));
writer.writeTextElement(QStringLiteral("dc:title"), QStringLiteral("")); if (property("title").isValid())
writer.writeTextElement(QStringLiteral("dc:subject"), QStringLiteral("")); writer.writeTextElement(QStringLiteral("dc:title"), property("title").toString());
writer.writeTextElement(QStringLiteral("dc:creator"), QStringLiteral("QXlsxWriter")); if (property("subject").isValid())
writer.writeTextElement(QStringLiteral("cp:keywords"), QStringLiteral("")); writer.writeTextElement(QStringLiteral("dc:subject"), property("subject").toString());
writer.writeTextElement(QStringLiteral("dc:description"), QStringLiteral("")); writer.writeTextElement(QStringLiteral("dc:creator"), property("creator").isValid() ? property("creator").toString() : QStringLiteral("Qt Xlsx Library"));
writer.writeTextElement(QStringLiteral("cp:lastModifiedBy"), QStringLiteral(""));
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.writeStartElement(QStringLiteral("dcterms:created"));
writer.writeAttribute(QStringLiteral("xsi:type"), QStringLiteral("dcterms:W3CDTF")); 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.writeCharacters(QDateTime::currentDateTime().toString(Qt::ISODate));
writer.writeEndElement();//dcterms:created writer.writeEndElement();//dcterms:created
writer.writeTextElement(QStringLiteral("cp:category"), QStringLiteral("")); if (property("category").isValid())
writer.writeTextElement(QStringLiteral("cp:contentStatus"), QStringLiteral("")); 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.writeEndElement(); //cp:coreProperties
writer.writeEndDocument(); writer.writeEndDocument();
} }
+3
View File
@@ -170,6 +170,9 @@ void Package::writeDocPropsFiles(ZipWriter &zipWriter)
{ {
DocProps props; DocProps props;
foreach (QByteArray name, m_workbook->dynamicPropertyNames())
props.setProperty(name.data(), m_workbook->property(name.data()));
if (m_worksheet_count) if (m_worksheet_count)
props.addHeadingPair(QStringLiteral("Worksheets"), m_worksheet_count); props.addHeadingPair(QStringLiteral("Worksheets"), m_worksheet_count);
if (m_chartsheet_count) if (m_chartsheet_count)
+25
View File
@@ -51,6 +51,31 @@ 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))
{ {
+4
View File
@@ -61,6 +61,10 @@ 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); bool save(const QString &name);
private: private: