Browse Source

Code refactoring: Remove private class QXlsx::Package

master
Debao Zhang 11 years ago
parent
commit
f92e34f374
  1. 2
      src/xlsx/qtxlsx.pri
  2. 259
      src/xlsx/xlsxdocument.cpp
  3. 2
      src/xlsx/xlsxdocument.h
  4. 1
      src/xlsx/xlsxdocument_p.h
  5. 382
      src/xlsx/xlsxpackage.cpp
  6. 81
      src/xlsx/xlsxpackage_p.h
  7. 1
      src/xlsx/xlsxworkbook.cpp
  8. 2
      src/xlsx/xlsxworkbook.h
  9. 3
      src/xlsx/xlsxworksheet.cpp
  10. 6
      src/xlsx/xlsxworksheet.h

2
src/xlsx/qtxlsx.pri

@ -16,7 +16,6 @@ HEADERS += $$PWD/xlsxdocpropscore_p.h \
$$PWD/xlsxstyles_p.h \ $$PWD/xlsxstyles_p.h \
$$PWD/xlsxworksheet.h \ $$PWD/xlsxworksheet.h \
$$PWD/xlsxzipwriter_p.h \ $$PWD/xlsxzipwriter_p.h \
$$PWD/xlsxpackage_p.h \
$$PWD/xlsxworkbook_p.h \ $$PWD/xlsxworkbook_p.h \
$$PWD/xlsxworksheet_p.h \ $$PWD/xlsxworksheet_p.h \
$$PWD/xlsxformat_p.h \ $$PWD/xlsxformat_p.h \
@ -49,7 +48,6 @@ SOURCES += $$PWD/xlsxdocpropscore.cpp \
$$PWD/xlsxworkbook.cpp \ $$PWD/xlsxworkbook.cpp \
$$PWD/xlsxworksheet.cpp \ $$PWD/xlsxworksheet.cpp \
$$PWD/xlsxzipwriter.cpp \ $$PWD/xlsxzipwriter.cpp \
$$PWD/xlsxpackage.cpp \
$$PWD/xlsxdrawing.cpp \ $$PWD/xlsxdrawing.cpp \
$$PWD/xlsxzipreader.cpp \ $$PWD/xlsxzipreader.cpp \
$$PWD/xlsxdocument.cpp \ $$PWD/xlsxdocument.cpp \

259
src/xlsx/xlsxdocument.cpp

@ -27,13 +27,60 @@
#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 "xlsxcontenttypes_p.h"
#include "xlsxrelationships_p.h"
#include "xlsxstyles_p.h"
#include "xlsxtheme_p.h"
#include "xlsxdocpropsapp_p.h"
#include "xlsxdocpropscore_p.h"
#include "xlsxsharedstrings_p.h"
#include "xlsxutility_p.h"
#include "xlsxworkbook_p.h"
#include "xlsxdrawing_p.h"
#include "xlsxzipreader_p.h"
#include "xlsxzipwriter_p.h"
#include <QFile> #include <QFile>
#include <QPointF> #include <QPointF>
#include <QBuffer>
QT_BEGIN_NAMESPACE_XLSX QT_BEGIN_NAMESPACE_XLSX
/*
From Wikipedia: The Open Packaging Conventions (OPC) is a
container-file technology initially created by Microsoft to store
a combination of XML and non-XML files that together form a single
entity such as an Open XML Paper Specification (OpenXPS)
document. http://en.wikipedia.org/wiki/Open_Packaging_Conventions.
At its simplest an Excel XLSX file contains the following elements:
____ [Content_Types].xml
|
|____ docProps
| |____ app.xml
| |____ core.xml
|
|____ xl
| |____ workbook.xml
| |____ worksheets
| | |____ sheet1.xml
| |
| |____ styles.xml
| |
| |____ theme
| | |____ theme1.xml
| |
| |_____rels
| |____ workbook.xml.rels
|
|_____rels
|____ .rels
The Packager class coordinates the classes that represent the
elements of the package and writes them into the XLSX file.
*/
DocumentPrivate::DocumentPrivate(Document *p) : DocumentPrivate::DocumentPrivate(Document *p) :
q_ptr(p), defaultPackageName(QStringLiteral("Book1.xlsx")) q_ptr(p), defaultPackageName(QStringLiteral("Book1.xlsx"))
{ {
@ -49,8 +96,206 @@ void DocumentPrivate::init()
bool DocumentPrivate::loadPackage(QIODevice *device) bool DocumentPrivate::loadPackage(QIODevice *device)
{ {
Q_Q(Document); Q_Q(Document);
Package package(q); ZipReader zipReader(device);
return package.parsePackage(device); QStringList filePaths = zipReader.filePaths();
if (!filePaths.contains(QLatin1String("_rels/.rels")))
return false;
Relationships rootRels;
rootRels.loadFromXmlData(zipReader.fileData(QStringLiteral("_rels/.rels")));
//load core property
QList<XlsxRelationship> rels_core = rootRels.packageRelationships(QStringLiteral("/metadata/core-properties"));
if (!rels_core.isEmpty()) {
//Get the core property file name if it exists.
//In normal case, this should be "docProps/core.xml"
QString docPropsCore_Name = rels_core[0].target;
DocPropsCore props = DocPropsCore::loadFromXmlData(zipReader.fileData(docPropsCore_Name));
foreach (QString name, props.propertyNames())
q->setDocumentProperty(name, props.property(name));
}
//load app property
QList<XlsxRelationship> rels_app = rootRels.documentRelationships(QStringLiteral("/extended-properties"));
if (!rels_app.isEmpty()) {
//Get the app property file name if it exists.
//In normal case, this should be "docProps/app.xml"
QString docPropsApp_Name = rels_app[0].target;
DocPropsApp props = DocPropsApp::loadFromXmlData(zipReader.fileData(docPropsApp_Name));
foreach (QString name, props.propertyNames())
q->setDocumentProperty(name, props.property(name));
}
//load workbook now, Get the workbook file path from the root rels file
//In normal case, this should be "xl/workbook.xml"
QList<XlsxRelationship> rels_xl = rootRels.documentRelationships(QStringLiteral("/officeDocument"));
if (rels_xl.isEmpty())
return false;
QString xlworkbook_Path = rels_xl[0].target;
QStringList xlworkbook_PathList = splitPath(xlworkbook_Path);
QString xlworkbook_Dir = xlworkbook_PathList[0];
QString xlworkbook_Name = xlworkbook_PathList[1];
workbook->loadFromXmlData(zipReader.fileData(xlworkbook_Path));
QList<XlsxSheetItemInfo> sheetNameIdPairList = workbook->d_func()->sheetItemInfoList;
Relationships xlworkbook_Rels;
xlworkbook_Rels.loadFromXmlData(zipReader.fileData(xlworkbook_Dir+QStringLiteral("/_rels/")+xlworkbook_Name+QStringLiteral(".rels")));
//load styles
QList<XlsxRelationship> rels_styles = xlworkbook_Rels.documentRelationships(QStringLiteral("/styles"));
if (!rels_styles.isEmpty()) {
//In normal case this should be styles.xml which in xl
QString name = rels_styles[0].target;
QString path = xlworkbook_Dir + QLatin1String("/") + name;
QSharedPointer<Styles> styles (new Styles(true));
styles->loadFromXmlData(zipReader.fileData(path));
workbook->d_ptr->styles = styles;
}
//load sharedStrings
QList<XlsxRelationship> rels_sharedStrings = xlworkbook_Rels.documentRelationships(QStringLiteral("/sharedStrings"));
if (!rels_sharedStrings.isEmpty()) {
//In normal case this should be sharedStrings.xml which in xl
QString name = rels_sharedStrings[0].target;
QString path = xlworkbook_Dir + QLatin1String("/") + name;
workbook->d_ptr->sharedStrings->loadFromXmlData(zipReader.fileData(path));
}
//load theme
QList<XlsxRelationship> rels_theme = xlworkbook_Rels.documentRelationships(QStringLiteral("/theme"));
if (!rels_theme.isEmpty()) {
//In normal case this should be theme/theme1.xml which in xl
QString name = rels_theme[0].target;
QString path = xlworkbook_Dir + QLatin1String("/") + name;
workbook->theme()->loadFromXmlData(zipReader.fileData(path));
}
//load worksheets
QList<XlsxRelationship> rels_worksheets = xlworkbook_Rels.documentRelationships(QStringLiteral("/worksheet"));
if (rels_worksheets.isEmpty())
return false;
for (int i=0; i<sheetNameIdPairList.size(); ++i) {
XlsxSheetItemInfo info = sheetNameIdPairList[i];
QString worksheet_path = xlworkbook_Dir + QLatin1String("/") + xlworkbook_Rels.getRelationshipById(info.rId).target;
QString rel_path = getRelFilePath(worksheet_path);
Worksheet *sheet = workbook->addWorksheet(info.name, info.sheetId);
//If the .rel file exists, load it.
if (zipReader.filePaths().contains(rel_path))
sheet->relationships().loadFromXmlData(zipReader.fileData(rel_path));
sheet->loadFromXmlData(zipReader.fileData(worksheet_path));
}
return true;
}
bool DocumentPrivate::savePackage(QIODevice *device)
{
Q_Q(Document);
ZipWriter zipWriter(device);
if (zipWriter.error())
return false;
ContentTypes contentTypes;
DocPropsApp docPropsApp;
DocPropsCore docPropsCore;
workbook->prepareDrawings();
// save worksheet xml files
for (int i=0; i<workbook->worksheetCount(); ++i) {
Worksheet *sheet = workbook->worksheet(i);
contentTypes.addWorksheetName(QStringLiteral("sheet%1").arg(i+1));
docPropsApp.addPartTitle(sheet->sheetName());
zipWriter.addFile(QStringLiteral("xl/worksheets/sheet%1.xml").arg(i+1), sheet->saveToXmlData());
Relationships &rel = sheet->relationships();
if (!rel.isEmpty())
zipWriter.addFile(QStringLiteral("xl/worksheets/_rels/sheet%1.xml.rels").arg(i+1), rel.saveToXmlData());
}
// save workbook xml file
zipWriter.addFile(QStringLiteral("xl/workbook.xml"), workbook->saveToXmlData());
Relationships rels;
for (int i=0; i<workbook->worksheetCount(); ++i)
rels.addDocumentRelationship(QStringLiteral("/worksheet"), QStringLiteral("worksheets/sheet%1.xml").arg(i+1));
rels.addDocumentRelationship(QStringLiteral("/theme"), QStringLiteral("theme/theme1.xml"));
rels.addDocumentRelationship(QStringLiteral("/styles"), QStringLiteral("styles.xml"));
if (!workbook->sharedStrings()->isEmpty())
rels.addDocumentRelationship(QStringLiteral("/sharedStrings"), QStringLiteral("sharedStrings.xml"));
zipWriter.addFile(QStringLiteral("xl/_rels/workbook.xml.rels"), rels.saveToXmlData());
// save drawing xml files
for (int i=0; i<workbook->drawings().size(); ++i) {
contentTypes.addDrawingName(QStringLiteral("drawing%1").arg(i+1));
Drawing *drawing = workbook->drawings()[i];
zipWriter.addFile(QStringLiteral("xl/drawings/drawing%1.xml").arg(i+1), drawing->saveToXmlData());
}
for (int i=0; i<workbook->worksheetCount(); ++i) {
Worksheet *sheet = workbook->worksheet(i);
if (sheet->drawingLinks().size() == 0)
continue;
Relationships rels;
typedef QPair<QString, QString> PairType;
foreach (PairType pair, sheet->drawingLinks())
rels.addDocumentRelationship(pair.first, pair.second);
zipWriter.addFile(QStringLiteral("xl/drawings/_rels/drawing%1.xml.rels").arg(i+1), rels.saveToXmlData());
}
// save docProps app/core xml file
foreach (QString name, q->documentPropertyNames()) {
docPropsApp.setProperty(name, q->documentProperty(name));
docPropsCore.setProperty(name, q->documentProperty(name));
}
if (workbook->worksheetCount())
docPropsApp.addHeadingPair(QStringLiteral("Worksheets"), workbook->worksheetCount());
zipWriter.addFile(QStringLiteral("docProps/app.xml"), docPropsApp.saveToXmlData());
zipWriter.addFile(QStringLiteral("docProps/core.xml"), docPropsCore.saveToXmlData());
// save sharedStrings xml file
if (!workbook->sharedStrings()->isEmpty()) {
contentTypes.addSharedString();
zipWriter.addFile(QStringLiteral("xl/sharedStrings.xml"), workbook->sharedStrings()->saveToXmlData());
}
// save styles xml file
zipWriter.addFile(QStringLiteral("xl/styles.xml"), workbook->styles()->saveToXmlData());
// save theme xml file
zipWriter.addFile(QStringLiteral("xl/theme/theme1.xml"), workbook->theme()->saveToXmlData());
// save image files
if (!workbook->images().isEmpty())
contentTypes.addImageTypes(QStringList()<<QStringLiteral("png"));
for (int i=0; i<workbook->images().size(); ++i) {
QImage image = workbook->images()[i];
QByteArray data;
QBuffer buffer(&data);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "png");
zipWriter.addFile(QStringLiteral("xl/media/image%1.png").arg(i+1), data);
}
// save root .rels xml file
Relationships rootrels;
rootrels.addDocumentRelationship(QStringLiteral("/officeDocument"), QStringLiteral("xl/workbook.xml"));
rootrels.addPackageRelationship(QStringLiteral("/metadata/core-properties"), QStringLiteral("docProps/core.xml"));
rootrels.addDocumentRelationship(QStringLiteral("/extended-properties"), QStringLiteral("docProps/app.xml"));
zipWriter.addFile(QStringLiteral("_rels/.rels"), rootrels.saveToXmlData());
// save content types xml file
zipWriter.addFile(QStringLiteral("[Content_Types].xml"), contentTypes.saveToXmlData());
zipWriter.close();
return true;
} }
@ -505,12 +750,8 @@ bool Document::saveAs(const QString &name)
*/ */
bool Document::saveAs(QIODevice *device) bool Document::saveAs(QIODevice *device)
{ {
// activedWorksheet()->setHidden(false); Q_D(Document);
// activedWorksheet()->setSelected(true); return d->savePackage(device);
//Create the package based on current workbook
Package package(this);
return package.createPackage(device);
} }
/*! /*!

2
src/xlsx/xlsxdocument.h

@ -37,7 +37,6 @@ QT_BEGIN_NAMESPACE_XLSX
class Workbook; class Workbook;
class Worksheet; class Worksheet;
class Package;
class Cell; class Cell;
class CellRange; class CellRange;
class DataValidation; class DataValidation;
@ -103,7 +102,6 @@ public:
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;
}; };

1
src/xlsx/xlsxdocument_p.h

@ -52,6 +52,7 @@ public:
void init(); void init();
bool loadPackage(QIODevice *device); bool loadPackage(QIODevice *device);
bool savePackage(QIODevice *device);
Document *q_ptr; Document *q_ptr;
const QString defaultPackageName; //default name when package name not specified const QString defaultPackageName; //default name when package name not specified

382
src/xlsx/xlsxpackage.cpp

@ -1,382 +0,0 @@
/****************************************************************************
** Copyright (c) 2013-2014 Debao Zhang <hello@debao.me>
** All right reserved.
**
** Permission is hereby granted, free of charge, to any person obtaining
** a copy of this software and associated documentation files (the
** "Software"), to deal in the Software without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Software, and to
** permit persons to whom the Software is furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**
****************************************************************************/
#include "xlsxpackage_p.h"
#include "xlsxworkbook.h"
#include "xlsxworksheet.h"
#include "xlsxworkbook_p.h"
#include "xlsxutility_p.h"
#include "xlsxcontenttypes_p.h"
#include "xlsxsharedstrings_p.h"
#include "xlsxdocpropscore_p.h"
#include "xlsxdocpropsapp_p.h"
#include "xlsxtheme_p.h"
#include "xlsxstyles_p.h"
#include "xlsxrelationships_p.h"
#include "xlsxzipwriter_p.h"
#include "xlsxdrawing_p.h"
#include "xlsxzipreader_p.h"
#include "xlsxdocument.h"
#include "xlsxformat_p.h"
#include <QBuffer>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
namespace QXlsx {
//: TODO This class should be merged to Document class.
/*
From Wikipedia: The Open Packaging Conventions (OPC) is a
container-file technology initially created by Microsoft to store
a combination of XML and non-XML files that together form a single
entity such as an Open XML Paper Specification (OpenXPS)
document. http://en.wikipedia.org/wiki/Open_Packaging_Conventions.
At its simplest an Excel XLSX file contains the following elements:
____ [Content_Types].xml
|
|____ docProps
| |____ app.xml
| |____ core.xml
|
|____ xl
| |____ workbook.xml
| |____ worksheets
| | |____ sheet1.xml
| |
| |____ styles.xml
| |
| |____ theme
| | |____ theme1.xml
| |
| |_____rels
| |____ workbook.xml.rels
|
|_____rels
|____ .rels
The Packager class coordinates the classes that represent the
elements of the package and writes them into the XLSX file.
*/
Package::Package(Document *document) :
m_document(document)
{
m_workbook = m_document->workbook();
m_worksheet_count = 0;
m_chartsheet_count = 0;
for (int i=0; i<m_workbook->worksheetCount(); ++i) {
Worksheet *sheet = m_workbook->worksheet(i);
if (sheet->isChartsheet())
m_chartsheet_count += 1;
else
m_worksheet_count += 1;
}
}
bool Package::parsePackage(QIODevice *packageDevice)
{
ZipReader zipReader(packageDevice);
QStringList filePaths = zipReader.filePaths();
if (!filePaths.contains(QLatin1String("_rels/.rels")))
return false;
Relationships rootRels;
rootRels.loadFromXmlData(zipReader.fileData(QStringLiteral("_rels/.rels")));
//load core property
QList<XlsxRelationship> rels_core = rootRels.packageRelationships(QStringLiteral("/metadata/core-properties"));
if (!rels_core.isEmpty()) {
//Get the core property file name if it exists.
//In normal case, this should be "docProps/core.xml"
QString docPropsCore_Name = rels_core[0].target;
DocPropsCore props = DocPropsCore::loadFromXmlData(zipReader.fileData(docPropsCore_Name));
foreach (QString name, props.propertyNames())
m_document->setDocumentProperty(name, props.property(name));
}
//load app property
QList<XlsxRelationship> rels_app = rootRels.documentRelationships(QStringLiteral("/extended-properties"));
if (!rels_app.isEmpty()) {
//Get the app property file name if it exists.
//In normal case, this should be "docProps/app.xml"
QString docPropsApp_Name = rels_app[0].target;
DocPropsApp props = DocPropsApp::loadFromXmlData(zipReader.fileData(docPropsApp_Name));
foreach (QString name, props.propertyNames())
m_document->setDocumentProperty(name, props.property(name));
}
//load workbook now, Get the workbook file path from the root rels file
//In normal case, this should be "xl/workbook.xml"
QList<XlsxRelationship> rels_xl = rootRels.documentRelationships(QStringLiteral("/officeDocument"));
if (rels_xl.isEmpty())
return false;
QString xlworkbook_Path = rels_xl[0].target;
QStringList xlworkbook_PathList = splitPath(xlworkbook_Path);
QString xlworkbook_Dir = xlworkbook_PathList[0];
QString xlworkbook_Name = xlworkbook_PathList[1];
m_document->workbook()->loadFromXmlData(zipReader.fileData(xlworkbook_Path));
QList<XlsxSheetItemInfo> sheetNameIdPairList = m_document->workbook()->d_func()->sheetItemInfoList;
Relationships xlworkbook_Rels;
xlworkbook_Rels.loadFromXmlData(zipReader.fileData(xlworkbook_Dir+QStringLiteral("/_rels/")+xlworkbook_Name+QStringLiteral(".rels")));
//load styles
QList<XlsxRelationship> rels_styles = xlworkbook_Rels.documentRelationships(QStringLiteral("/styles"));
if (!rels_styles.isEmpty()) {
//In normal case this should be styles.xml which in xl
QString name = rels_styles[0].target;
QString path = xlworkbook_Dir + QLatin1String("/") + name;
QSharedPointer<Styles> styles (new Styles(true));
styles->loadFromXmlData(zipReader.fileData(path));
m_document->workbook()->d_ptr->styles = styles;
}
//load sharedStrings
QList<XlsxRelationship> rels_sharedStrings = xlworkbook_Rels.documentRelationships(QStringLiteral("/sharedStrings"));
if (!rels_sharedStrings.isEmpty()) {
//In normal case this should be sharedStrings.xml which in xl
QString name = rels_sharedStrings[0].target;
QString path = xlworkbook_Dir + QLatin1String("/") + name;
m_document->workbook()->d_ptr->sharedStrings->loadFromXmlData(zipReader.fileData(path));
}
//load theme
QList<XlsxRelationship> rels_theme = xlworkbook_Rels.documentRelationships(QStringLiteral("/theme"));
if (!rels_theme.isEmpty()) {
//In normal case this should be theme/theme1.xml which in xl
QString name = rels_theme[0].target;
QString path = xlworkbook_Dir + QLatin1String("/") + name;
m_document->workbook()->theme()->loadFromXmlData(zipReader.fileData(path));
}
//load worksheets
QList<XlsxRelationship> rels_worksheets = xlworkbook_Rels.documentRelationships(QStringLiteral("/worksheet"));
if (rels_worksheets.isEmpty())
return false;
for (int i=0; i<sheetNameIdPairList.size(); ++i) {
XlsxSheetItemInfo info = sheetNameIdPairList[i];
QString worksheet_path = xlworkbook_Dir + QLatin1String("/") + xlworkbook_Rels.getRelationshipById(info.rId).target;
QString rel_path = getRelFilePath(worksheet_path);
Worksheet *sheet = m_document->workbook()->addWorksheet(info.name, info.sheetId);
//If the .rel file exists, load it.
if (zipReader.filePaths().contains(rel_path))
sheet->relationships().loadFromXmlData(zipReader.fileData(rel_path));
sheet->loadFromXmlData(zipReader.fileData(worksheet_path));
}
return true;
}
bool Package::createPackage(QIODevice *package)
{
ZipWriter zipWriter(package);
if (zipWriter.error())
return false;
m_workbook->prepareDrawings();
writeWorksheetFiles(zipWriter);
zipWriter.addFile(QStringLiteral("xl/workbook.xml"), m_workbook->saveToXmlData());
writeDrawingFiles(zipWriter);
writeDocPropsAppFile(zipWriter);
writeDocPropsCoreFile(zipWriter);
writeContentTypesFile(zipWriter);
if (!m_workbook->sharedStrings()->isEmpty())
zipWriter.addFile(QStringLiteral("xl/sharedStrings.xml"), m_workbook->sharedStrings()->saveToXmlData());
zipWriter.addFile(QStringLiteral("xl/styles.xml"), m_workbook->styles()->saveToXmlData());
zipWriter.addFile(QStringLiteral("xl/theme/theme1.xml"), m_workbook->theme()->saveToXmlData());
writeRootRelsFile(zipWriter);
writeWorkbookRelsFile(zipWriter);
writeDrawingRelsFiles(zipWriter);
writeImageFiles(zipWriter);
zipWriter.close();
return true;
}
void Package::writeWorksheetFiles(ZipWriter &zipWriter)
{
for (int i=0; i<m_workbook->worksheetCount(); ++i) {
Worksheet *sheet = m_workbook->worksheet(i);
if (sheet->isChartsheet())
continue;
zipWriter.addFile(QStringLiteral("xl/worksheets/sheet%1.xml").arg(i+1), sheet->saveToXmlData());
Relationships &rel = sheet->relationships();
if (!rel.isEmpty())
zipWriter.addFile(QStringLiteral("xl/worksheets/_rels/sheet%1.xml.rels").arg(i+1), rel.saveToXmlData());
}
}
void Package::writeDrawingFiles(ZipWriter &zipWriter)
{
for (int i=0; i<m_workbook->drawings().size(); ++i) {
Drawing *drawing = m_workbook->drawings()[i];
zipWriter.addFile(QStringLiteral("xl/drawings/drawing%1.xml").arg(i+1), drawing->saveToXmlData());
}
}
void Package::writeContentTypesFile(ZipWriter &zipWriter)
{
ContentTypes content;
for (int i=0; i<m_workbook->worksheetCount(); ++i) {
Worksheet *sheet = m_workbook->worksheet(i);
if (sheet->isChartsheet()) {
} else {
content.addWorksheetName(QStringLiteral("sheet%1").arg(i+1));
}
}
int drawing_index = 1;
foreach (Drawing *drawing, m_workbook->drawings()) {
Q_UNUSED(drawing);
content.addDrawingName(QStringLiteral("drawing%1").arg(drawing_index));
drawing_index += 1;
}
if (!m_workbook->images().isEmpty())
content.addImageTypes(QStringList()<<QStringLiteral("png"));
if (m_workbook->sharedStrings()->count())
content.addSharedString();
zipWriter.addFile(QStringLiteral("[Content_Types].xml"), content.saveToXmlData());
}
void Package::writeDocPropsAppFile(ZipWriter &zipWriter)
{
DocPropsApp props;
foreach (QString name, m_document->documentPropertyNames())
props.setProperty(name, m_document->documentProperty(name));
if (m_worksheet_count)
props.addHeadingPair(QStringLiteral("Worksheets"), m_worksheet_count);
if (m_chartsheet_count)
props.addHeadingPair(QStringLiteral("Chartsheets"), m_chartsheet_count);
//Add worksheet parts
for (int i=0; i<m_workbook->worksheetCount(); ++i) {
Worksheet *sheet = m_workbook->worksheet(i);
if (!sheet->isChartsheet())
props.addPartTitle(sheet->sheetName());
}
//Add the chartsheet parts
for (int i=0; i<m_workbook->worksheetCount(); ++i) {
Worksheet *sheet = m_workbook->worksheet(i);
if (sheet->isChartsheet())
props.addPartTitle(sheet->sheetName());
}
zipWriter.addFile(QStringLiteral("docProps/app.xml"), props.saveToXmlData());
}
void Package::writeDocPropsCoreFile(ZipWriter &zipWriter)
{
DocPropsCore props;
foreach (QString name, m_document->documentPropertyNames())
props.setProperty(name, m_document->documentProperty(name));
zipWriter.addFile(QStringLiteral("docProps/core.xml"), props.saveToXmlData());
}
void Package::writeRootRelsFile(ZipWriter &zipWriter)
{
Relationships rels;
rels.addDocumentRelationship(QStringLiteral("/officeDocument"), QStringLiteral("xl/workbook.xml"));
rels.addPackageRelationship(QStringLiteral("/metadata/core-properties"), QStringLiteral("docProps/core.xml"));
rels.addDocumentRelationship(QStringLiteral("/extended-properties"), QStringLiteral("docProps/app.xml"));
zipWriter.addFile(QStringLiteral("_rels/.rels"), rels.saveToXmlData());
}
void Package::writeWorkbookRelsFile(ZipWriter &zipWriter)
{
Relationships rels;
int worksheet_index = 1;
int chartsheet_index = 1;
for (int i=0; i<m_workbook->worksheetCount(); ++i) {
Worksheet *sheet = m_workbook->worksheet(i);
if (sheet->isChartsheet()) {
rels.addDocumentRelationship(QStringLiteral("/chartsheet"), QStringLiteral("chartsheets/sheet%1.xml").arg(chartsheet_index));
chartsheet_index += 1;
} else {
rels.addDocumentRelationship(QStringLiteral("/worksheet"), QStringLiteral("worksheets/sheet%1.xml").arg(worksheet_index));
worksheet_index += 1;
}
}
rels.addDocumentRelationship(QStringLiteral("/theme"), QStringLiteral("theme/theme1.xml"));
rels.addDocumentRelationship(QStringLiteral("/styles"), QStringLiteral("styles.xml"));
if (!m_workbook->sharedStrings()->isEmpty())
rels.addDocumentRelationship(QStringLiteral("/sharedStrings"), QStringLiteral("sharedStrings.xml"));
zipWriter.addFile(QStringLiteral("xl/_rels/workbook.xml.rels"), rels.saveToXmlData());
}
void Package::writeDrawingRelsFiles(ZipWriter &zipWriter)
{
for (int i=0; i<m_workbook->worksheetCount(); ++i) {
Worksheet *sheet = m_workbook->worksheet(i);
if (sheet->drawingLinks().size() == 0)
continue;
Relationships rels;
typedef QPair<QString, QString> PairType;
foreach (PairType pair, sheet->drawingLinks())
rels.addDocumentRelationship(pair.first, pair.second);
zipWriter.addFile(QStringLiteral("xl/drawings/_rels/drawing%1.xml.rels").arg(i+1), rels.saveToXmlData());
}
}
void Package::writeImageFiles(ZipWriter &zipWriter)
{
for (int i=0; i<m_workbook->images().size(); ++i) {
QImage image = m_workbook->images()[i];
QByteArray data;
QBuffer buffer(&data);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "png");
zipWriter.addFile(QStringLiteral("xl/media/image%1.png").arg(i+1), data);
}
}
} // namespace QXlsx

81
src/xlsx/xlsxpackage_p.h

@ -1,81 +0,0 @@
/****************************************************************************
** Copyright (c) 2013-2014 Debao Zhang <hello@debao.me>
** All right reserved.
**
** Permission is hereby granted, free of charge, to any person obtaining
** a copy of this software and associated documentation files (the
** "Software"), to deal in the Software without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Software, and to
** permit persons to whom the Software is furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**
****************************************************************************/
#ifndef QXLSX_PACKAGE_H
#define QXLSX_PACKAGE_H
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt Xlsx API. It exists for the convenience
// of the Qt Xlsx. This header file may change from
// version to version without notice, or even be removed.
//
// We mean it.
//
#include "xlsxglobal.h"
#include <QString>
class QIODevice;
namespace QXlsx {
class Workbook;
class ZipWriter;
class ZipReader;
class Document;
class Relationships;
class DocPropsCore;
class DocPropsApp;
class XLSX_AUTOTEST_EXPORT Package
{
public:
Package(Document *document);
bool parsePackage(QIODevice *packageDevice);
bool createPackage(QIODevice *package);
private:
void writeWorksheetFiles(ZipWriter &zipWriter);
void writeDrawingFiles(ZipWriter &zipWriter);
void writeDocPropsAppFile(ZipWriter &zipWriter);
void writeDocPropsCoreFile(ZipWriter &zipWriter);
void writeContentTypesFile(ZipWriter &zipWriter);
void writeRootRelsFile(ZipWriter &zipWriter);
void writeWorkbookRelsFile(ZipWriter &zipWriter);
void writeDrawingRelsFiles(ZipWriter &zipWriter);
void writeImageFiles(ZipWriter &zipWriter);
Document *m_document;
Workbook *m_workbook;
int m_worksheet_count;
int m_chartsheet_count;
};
} // namespace QXlsx
#endif // QXLSX_PACKAGE_H

1
src/xlsx/xlsxworkbook.cpp

@ -28,7 +28,6 @@
#include "xlsxworksheet.h" #include "xlsxworksheet.h"
#include "xlsxstyles_p.h" #include "xlsxstyles_p.h"
#include "xlsxformat.h" #include "xlsxformat.h"
#include "xlsxpackage_p.h"
#include "xlsxworksheet_p.h" #include "xlsxworksheet_p.h"
#include "xlsxformat_p.h" #include "xlsxformat_p.h"

2
src/xlsx/xlsxworkbook.h

@ -37,7 +37,6 @@ QT_BEGIN_NAMESPACE_XLSX
class Worksheet; class Worksheet;
class SharedStrings; class SharedStrings;
class Styles; class Styles;
class Package;
class Drawing; class Drawing;
class Document; class Document;
class Theme; class Theme;
@ -74,7 +73,6 @@ public:
void setDefaultDateFormat(const QString &format); void setDefaultDateFormat(const QString &format);
private: private:
friend class Package;
friend class Worksheet; friend class Worksheet;
friend class WorksheetPrivate; friend class WorksheetPrivate;
friend class Document; friend class Document;

3
src/xlsx/xlsxworksheet.cpp

@ -244,6 +244,9 @@ void Worksheet::setSheetName(const QString &sheetName)
d->name = sheetName; d->name = sheetName;
} }
/*!
* \internal
*/
Relationships &Worksheet::relationships() Relationships &Worksheet::relationships()
{ {
Q_D(Worksheet); Q_D(Worksheet);

6
src/xlsx/xlsxworksheet.h

@ -40,7 +40,7 @@ class QImage;
class WorksheetTest; class WorksheetTest;
QT_BEGIN_NAMESPACE_XLSX QT_BEGIN_NAMESPACE_XLSX
class Package; class DocumentPrivate;
class Workbook; class Workbook;
class Format; class Format;
class Drawing; class Drawing;
@ -129,15 +129,15 @@ public:
QString sheetName() const; QString sheetName() const;
Workbook *workbook() const; Workbook *workbook() const;
Relationships &relationships();
~Worksheet(); ~Worksheet();
private: private:
friend class Package; friend class DocumentPrivate;
friend class Workbook; friend class Workbook;
friend class ::WorksheetTest; friend class ::WorksheetTest;
Worksheet(const QString &sheetName, int sheetId, Workbook *book); Worksheet(const QString &sheetName, int sheetId, Workbook *book);
QSharedPointer<Worksheet> copy(const QString &distName, int distId) const; QSharedPointer<Worksheet> copy(const QString &distName, int distId) const;
void setSheetName(const QString &sheetName); void setSheetName(const QString &sheetName);
Relationships &relationships();
void saveToXmlFile(QIODevice *device) const; void saveToXmlFile(QIODevice *device) const;
QByteArray saveToXmlData() const; QByteArray saveToXmlData() const;

Loading…
Cancel
Save