xlsxcontenttypes.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // xlsxcontenttypes.cpp
  2. #include <QXmlStreamWriter>
  3. #include <QXmlStreamReader>
  4. #include <QFile>
  5. #include <QMapIterator>
  6. #include <QBuffer>
  7. #include <QDebug>
  8. #include "xlsxcontenttypes_p.h"
  9. QT_BEGIN_NAMESPACE_XLSX
  10. ContentTypes::ContentTypes(CreateFlag flag)
  11. :AbstractOOXmlFile(flag)
  12. {
  13. m_package_prefix = QStringLiteral("application/vnd.openxmlformats-package.");
  14. m_document_prefix = QStringLiteral("application/vnd.openxmlformats-officedocument.");
  15. m_defaults.insert(QStringLiteral("rels"), m_package_prefix + QLatin1String("relationships+xml"));
  16. m_defaults.insert(QStringLiteral("xml"), QStringLiteral("application/xml"));
  17. }
  18. void ContentTypes::addDefault(const QString &key, const QString &value)
  19. {
  20. m_defaults.insert(key, value);
  21. }
  22. void ContentTypes::addOverride(const QString &key, const QString &value)
  23. {
  24. m_overrides.insert(key, value);
  25. }
  26. void ContentTypes::addDocPropApp()
  27. {
  28. addOverride(QStringLiteral("/docProps/app.xml"), m_document_prefix + QLatin1String("extended-properties+xml"));
  29. }
  30. void ContentTypes::addDocPropCore()
  31. {
  32. addOverride(QStringLiteral("/docProps/core.xml"), m_package_prefix + QLatin1String("core-properties+xml"));
  33. }
  34. void ContentTypes::addStyles()
  35. {
  36. addOverride(QStringLiteral("/xl/styles.xml"), m_document_prefix + QLatin1String("spreadsheetml.styles+xml"));
  37. }
  38. void ContentTypes::addTheme()
  39. {
  40. addOverride(QStringLiteral("/xl/theme/theme1.xml"), m_document_prefix + QLatin1String("theme+xml"));
  41. }
  42. void ContentTypes::addWorkbook()
  43. {
  44. addOverride(QStringLiteral("/xl/workbook.xml"), m_document_prefix + QLatin1String("spreadsheetml.sheet.main+xml"));
  45. }
  46. void ContentTypes::addWorksheetName(const QString &name)
  47. {
  48. addOverride(QStringLiteral("/xl/worksheets/%1.xml").arg(name), m_document_prefix + QLatin1String("spreadsheetml.worksheet+xml"));
  49. }
  50. void ContentTypes::addChartsheetName(const QString &name)
  51. {
  52. addOverride(QStringLiteral("/xl/chartsheets/%1.xml").arg(name), m_document_prefix + QLatin1String("spreadsheetml.chartsheet+xml"));
  53. }
  54. void ContentTypes::addDrawingName(const QString &name)
  55. {
  56. addOverride(QStringLiteral("/xl/drawings/%1.xml").arg(name), m_document_prefix + QLatin1String("drawing+xml"));
  57. }
  58. void ContentTypes::addChartName(const QString &name)
  59. {
  60. addOverride(QStringLiteral("/xl/charts/%1.xml").arg(name), m_document_prefix + QLatin1String("drawingml.chart+xml"));
  61. }
  62. void ContentTypes::addCommentName(const QString &name)
  63. {
  64. addOverride(QStringLiteral("/xl/%1.xml").arg(name), m_document_prefix + QLatin1String("spreadsheetml.comments+xml"));
  65. }
  66. void ContentTypes::addTableName(const QString &name)
  67. {
  68. addOverride(QStringLiteral("/xl/tables/%1.xml").arg(name), m_document_prefix + QLatin1String("spreadsheetml.table+xml"));
  69. }
  70. void ContentTypes::addExternalLinkName(const QString &name)
  71. {
  72. addOverride(QStringLiteral("/xl/externalLinks/%1.xml").arg(name), m_document_prefix + QLatin1String("spreadsheetml.externalLink+xml"));
  73. }
  74. void ContentTypes::addSharedString()
  75. {
  76. addOverride(QStringLiteral("/xl/sharedStrings.xml"), m_document_prefix + QLatin1String("spreadsheetml.sharedStrings+xml"));
  77. }
  78. void ContentTypes::addVmlName()
  79. {
  80. addOverride(QStringLiteral("vml"), m_document_prefix + QLatin1String("vmlDrawing"));
  81. }
  82. void ContentTypes::addCalcChain()
  83. {
  84. addOverride(QStringLiteral("/xl/calcChain.xml"), m_document_prefix + QLatin1String("spreadsheetml.calcChain+xml"));
  85. }
  86. void ContentTypes::addVbaProject()
  87. {
  88. //:TODO
  89. addOverride(QStringLiteral("bin"), QStringLiteral("application/vnd.ms-office.vbaProject"));
  90. }
  91. void ContentTypes::clearOverrides()
  92. {
  93. m_overrides.clear();
  94. }
  95. void ContentTypes::saveToXmlFile(QIODevice *device) const
  96. {
  97. QXmlStreamWriter writer(device);
  98. writer.writeStartDocument(QStringLiteral("1.0"), true);
  99. writer.writeStartElement(QStringLiteral("Types"));
  100. writer.writeAttribute(QStringLiteral("xmlns"), QStringLiteral("http://schemas.openxmlformats.org/package/2006/content-types"));
  101. {
  102. QMapIterator<QString, QString> it(m_defaults);
  103. while (it.hasNext()) {
  104. it.next();
  105. writer.writeStartElement(QStringLiteral("Default"));
  106. writer.writeAttribute(QStringLiteral("Extension"), it.key());
  107. writer.writeAttribute(QStringLiteral("ContentType"), it.value());
  108. writer.writeEndElement();//Default
  109. }
  110. }
  111. {
  112. QMapIterator<QString, QString> it(m_overrides);
  113. while (it.hasNext()) {
  114. it.next();
  115. writer.writeStartElement(QStringLiteral("Override"));
  116. writer.writeAttribute(QStringLiteral("PartName"), it.key());
  117. writer.writeAttribute(QStringLiteral("ContentType"), it.value());
  118. writer.writeEndElement(); //Override
  119. }
  120. }
  121. writer.writeEndElement();//Types
  122. writer.writeEndDocument();
  123. }
  124. bool ContentTypes::loadFromXmlFile(QIODevice *device)
  125. {
  126. m_defaults.clear();
  127. m_overrides.clear();
  128. QXmlStreamReader reader(device);
  129. while (!reader.atEnd()) {
  130. QXmlStreamReader::TokenType token = reader.readNext();
  131. if (token == QXmlStreamReader::StartElement) {
  132. if (reader.name() == QLatin1String("Default")) {
  133. QXmlStreamAttributes attrs = reader.attributes();
  134. QString extension = attrs.value(QLatin1String("Extension")).toString();
  135. QString type = attrs.value(QLatin1String("ContentType")).toString();
  136. m_defaults.insert(extension, type);
  137. } else if (reader.name() == QLatin1String("Override")) {
  138. QXmlStreamAttributes attrs = reader.attributes();
  139. QString partName = attrs.value(QLatin1String("PartName")).toString();
  140. QString type = attrs.value(QLatin1String("ContentType")).toString();
  141. m_overrides.insert(partName, type);
  142. }
  143. }
  144. if (reader.hasError()) {
  145. qDebug()<<reader.errorString();
  146. }
  147. }
  148. return true;
  149. }
  150. QT_END_NAMESPACE_XLSX