5
* Copyright (c) 2006 - 2009 PHPExcel
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2.1 of the License, or (at your option) any later version.
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
* @package PHPExcel_Writer_Excel2007
23
* @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
24
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25
* @version 1.7.0, 2009-08-10
29
/** PHPExcel root directory */
30
if (!defined('PHPEXCEL_ROOT')) {
34
define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../../');
38
require_once PHPEXCEL_ROOT . 'PHPExcel.php';
40
/** PHPExcel_Worksheet */
41
require_once PHPEXCEL_ROOT . 'PHPExcel/Worksheet.php';
43
/** PHPExcel_Writer_Excel2007 */
44
require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/Excel2007.php';
46
/** PHPExcel_Writer_Excel2007_WriterPart */
47
require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/Excel2007/WriterPart.php';
49
/** PHPExcel_Shared_XMLWriter */
50
require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/XMLWriter.php';
54
* PHPExcel_Writer_Excel2007_Rels
57
* @package PHPExcel_Writer_Excel2007
58
* @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
60
class PHPExcel_Writer_Excel2007_Rels extends PHPExcel_Writer_Excel2007_WriterPart
63
* Write relationships to XML format
65
* @param PHPExcel $pPHPExcel
66
* @return string XML Output
69
public function writeRelationships(PHPExcel $pPHPExcel = null)
73
if ($this->getParentWriter()->getUseDiskCaching()) {
74
$objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
76
$objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
80
$objWriter->startDocument('1.0','UTF-8','yes');
83
$objWriter->startElement('Relationships');
84
$objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
86
// Relationship docProps/app.xml
87
$this->_writeRelationship(
90
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties',
94
// Relationship docProps/core.xml
95
$this->_writeRelationship(
98
'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties',
102
// Relationship xl/workbook.xml
103
$this->_writeRelationship(
106
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument',
110
$objWriter->endElement();
113
return $objWriter->getData();
117
* Write workbook relationships to XML format
119
* @param PHPExcel $pPHPExcel
120
* @return string XML Output
123
public function writeWorkbookRelationships(PHPExcel $pPHPExcel = null)
127
if ($this->getParentWriter()->getUseDiskCaching()) {
128
$objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
130
$objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
134
$objWriter->startDocument('1.0','UTF-8','yes');
137
$objWriter->startElement('Relationships');
138
$objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
140
// Relationship styles.xml
141
$this->_writeRelationship(
144
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles',
148
// Relationship theme/theme1.xml
149
$this->_writeRelationship(
152
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme',
156
// Relationship sharedStrings.xml
157
$this->_writeRelationship(
160
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings',
164
// Relationships with sheets
165
$sheetCount = $pPHPExcel->getSheetCount();
166
for ($i = 0; $i < $sheetCount; ++$i) {
167
$this->_writeRelationship(
170
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet',
171
'worksheets/sheet' . ($i + 1) . '.xml'
175
$objWriter->endElement();
178
return $objWriter->getData();
182
* Write worksheet relationships to XML format
184
* Numbering is as follows:
186
* rId_hyperlink_x - Hyperlinks
188
* @param PHPExcel_Worksheet $pWorksheet
189
* @param int $pWorksheetId
190
* @return string XML Output
193
public function writeWorksheetRelationships(PHPExcel_Worksheet $pWorksheet = null, $pWorksheetId = 1)
197
if ($this->getParentWriter()->getUseDiskCaching()) {
198
$objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
200
$objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
204
$objWriter->startDocument('1.0','UTF-8','yes');
207
$objWriter->startElement('Relationships');
208
$objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
210
// Write drawing relationships?
211
if ($pWorksheet->getDrawingCollection()->count() > 0) {
212
$this->_writeRelationship(
215
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing',
216
'../drawings/drawing' . $pWorksheetId . '.xml'
220
// Write hyperlink relationships?
222
foreach ($pWorksheet->getCellCollection() as $cell) {
223
if ($cell->hasHyperlink() && !$cell->getHyperlink()->isInternal()) {
224
$this->_writeRelationship(
227
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink',
228
$cell->getHyperlink()->getUrl(),
236
// Write comments relationship?
238
if (count($pWorksheet->getComments()) > 0) {
239
$this->_writeRelationship(
241
'_comments_vml' . $i,
242
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
243
'../drawings/vmlDrawing' . $pWorksheetId . '.vml'
246
$this->_writeRelationship(
249
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments',
250
'../comments' . $pWorksheetId . '.xml'
254
// Write header/footer relationship?
256
if (count($pWorksheet->getHeaderFooter()->getImages()) > 0) {
257
$this->_writeRelationship(
259
'_headerfooter_vml' . $i,
260
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
261
'../drawings/vmlDrawingHF' . $pWorksheetId . '.vml'
265
$objWriter->endElement();
268
return $objWriter->getData();
272
* Write drawing relationships to XML format
274
* @param PHPExcel_Worksheet $pWorksheet
275
* @return string XML Output
278
public function writeDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null)
282
if ($this->getParentWriter()->getUseDiskCaching()) {
283
$objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
285
$objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
289
$objWriter->startDocument('1.0','UTF-8','yes');
292
$objWriter->startElement('Relationships');
293
$objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
295
// Loop trough images and write relationships
297
$iterator = $pWorksheet->getDrawingCollection()->getIterator();
298
while ($iterator->valid()) {
299
if ($iterator->current() instanceof PHPExcel_Worksheet_Drawing
300
|| $iterator->current() instanceof PHPExcel_Worksheet_MemoryDrawing) {
301
// Write relationship for image drawing
302
$this->_writeRelationship(
305
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
306
'../media/' . str_replace(' ', '', $iterator->current()->getIndexedFilename())
314
$objWriter->endElement();
317
return $objWriter->getData();
321
* Write header/footer drawing relationships to XML format
323
* @param PHPExcel_Worksheet $pWorksheet
324
* @return string XML Output
327
public function writeHeaderFooterDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null)
331
if ($this->getParentWriter()->getUseDiskCaching()) {
332
$objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
334
$objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
338
$objWriter->startDocument('1.0','UTF-8','yes');
341
$objWriter->startElement('Relationships');
342
$objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
344
// Loop trough images and write relationships
345
foreach ($pWorksheet->getHeaderFooter()->getImages() as $key => $value) {
346
// Write relationship for image drawing
347
$this->_writeRelationship(
350
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
351
'../media/' . $value->getIndexedFilename()
355
$objWriter->endElement();
358
return $objWriter->getData();
362
* Write Override content type
364
* @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
365
* @param int $pId Relationship ID. rId will be prepended!
366
* @param string $pType Relationship type
367
* @param string $pTarget Relationship target
368
* @param string $pTargetMode Relationship target mode
371
private function _writeRelationship(PHPExcel_Shared_XMLWriter $objWriter = null, $pId = 1, $pType = '', $pTarget = '', $pTargetMode = '')
373
if ($pType != '' && $pTarget != '') {
374
// Write relationship
375
$objWriter->startElement('Relationship');
376
$objWriter->writeAttribute('Id', 'rId' . $pId);
377
$objWriter->writeAttribute('Type', $pType);
378
$objWriter->writeAttribute('Target', $pTarget);
380
if ($pTargetMode != '') {
381
$objWriter->writeAttribute('TargetMode', $pTargetMode);
384
$objWriter->endElement();
386
throw new Exception("Invalid parameters passed.");