Browse Source

Add document property support

Use QObject::setProperty() to do so
master
Debao Zhang 11 years ago
parent
commit
9739020b1f
  1. 7
      examples/xlsx/documentproperty/documentproperty.pro
  2. 28
      examples/xlsx/documentproperty/main.cpp
  3. 3
      examples/xlsx/xlsx.pro
  4. 30
      src/xlsx/xlsxdocprops.cpp
  5. 3
      src/xlsx/xlsxpackage.cpp
  6. 25
      src/xlsx/xlsxworkbook.cpp
  7. 4
      src/xlsx/xlsxworkbook.h

7
examples/xlsx/documentproperty/documentproperty.pro

@ -0,0 +1,7 @@
TARGET = ducumentproperty
#include(../../../src/xlsx/qtxlsx.pri)
QT+=xlsx
SOURCES += main.cpp

28
examples/xlsx/documentproperty/main.cpp

@ -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;
}

3
examples/xlsx/xlsx.pro

@ -1,3 +1,4 @@
TEMPLATE = subdirs
SUBDIRS = hello style
SUBDIRS = hello style \
documentproperty

30
src/xlsx/xlsxdocprops.cpp

@ -28,6 +28,7 @@
#include <QDir>
#include <QFile>
#include <QDateTime>
#include <QVariant>
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();
}

3
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)

25
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))
{

4
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:

Loading…
Cancel
Save