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__) . '/../../../');
37
/** PHPExcel_Writer_Excel2007 */
38
require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/Excel2007.php';
40
/** PHPExcel_Writer_Excel2007_WriterPart */
41
require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/Excel2007/WriterPart.php';
43
/** PHPExcel_Cell_DataType */
44
require_once PHPEXCEL_ROOT . 'PHPExcel/Cell/DataType.php';
46
/** PHPExcel_Shared_XMLWriter */
47
require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/XMLWriter.php';
49
/** PHPExcel_Shared_String */
50
require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/String.php';
54
* PHPExcel_Writer_Excel2007_StringTable
57
* @package PHPExcel_Writer_Excel2007
58
* @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
60
class PHPExcel_Writer_Excel2007_StringTable extends PHPExcel_Writer_Excel2007_WriterPart
63
* Create worksheet stringtable
65
* @param PHPExcel_Worksheet $pSheet Worksheet
66
* @param string[] $pExistingTable Existing table to eventually merge with
67
* @return string[] String table for worksheet
70
public function createStringTable($pSheet = null, $pExistingTable = null)
72
if (!is_null($pSheet)) {
73
// Create string lookup table
74
$aStringTable = array();
75
$cellCollection = null;
76
$aFlippedStringTable = null; // For faster lookup
78
// Is an existing table given?
79
if (!is_null($pExistingTable) && is_array($pExistingTable)) {
80
$aStringTable = $pExistingTable;
84
$aFlippedStringTable = $this->flipStringTable($aStringTable);
87
$cellCollection = $pSheet->getCellCollection();
88
foreach ($cellCollection as $cell) {
89
if (!is_object($cell->getValue()) &&
90
!isset($aFlippedStringTable[$cell->getValue()]) &&
91
!is_null($cell->getValue()) &&
92
$cell->getValue() !== '' &&
93
($cell->getDataType() == PHPExcel_Cell_DataType::TYPE_STRING || $cell->getDataType() == PHPExcel_Cell_DataType::TYPE_NULL)
95
$aStringTable[] = $cell->getValue();
96
$aFlippedStringTable[$cell->getValue()] = 1;
98
} else if ($cell->getValue() instanceof PHPExcel_RichText &&
99
!isset($aFlippedStringTable[$cell->getValue()->getHashCode()]) &&
100
!is_null($cell->getValue())
102
$aStringTable[] = $cell->getValue();
103
$aFlippedStringTable[$cell->getValue()->getHashCode()] = 1;
108
return $aStringTable;
110
throw new Exception("Invalid PHPExcel_Worksheet object passed.");
115
* Write string table to XML format
117
* @param string[] $pStringTable
118
* @return string XML Output
121
public function writeStringTable($pStringTable = null)
123
if (!is_null($pStringTable)) {
126
if ($this->getParentWriter()->getUseDiskCaching()) {
127
$objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
129
$objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
133
$objWriter->startDocument('1.0','UTF-8','yes');
136
$objWriter->startElement('sst');
137
$objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
138
$objWriter->writeAttribute('uniqueCount', count($pStringTable));
140
// Loop trough string table
141
foreach ($pStringTable as $textElement) {
142
$objWriter->startElement('si');
144
if (! $textElement instanceof PHPExcel_RichText) {
145
$textToWrite = PHPExcel_Shared_String::ControlCharacterPHP2OOXML( $textElement );
146
$objWriter->startElement('t');
147
if ($textToWrite !== trim($textToWrite)) {
148
$objWriter->writeAttribute('xml:space', 'preserve');
150
$objWriter->writeRaw($textToWrite);
151
$objWriter->endElement();
152
} else if ($textElement instanceof PHPExcel_RichText) {
153
$this->writeRichText($objWriter, $textElement);
156
$objWriter->endElement();
159
$objWriter->endElement();
162
return $objWriter->getData();
164
throw new Exception("Invalid string table array passed.");
171
* @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
172
* @param PHPExcel_RichText $pRichText Rich text
175
public function writeRichText(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_RichText $pRichText = null)
177
// Loop trough rich text elements
178
$elements = $pRichText->getRichTextElements();
179
foreach ($elements as $element) {
181
$objWriter->startElement('r');
184
if ($element instanceof PHPExcel_RichText_Run) {
186
$objWriter->startElement('rPr');
189
$objWriter->startElement('rFont');
190
$objWriter->writeAttribute('val', $element->getFont()->getName());
191
$objWriter->endElement();
194
$objWriter->startElement('b');
195
$objWriter->writeAttribute('val', ($element->getFont()->getBold() ? 'true' : 'false'));
196
$objWriter->endElement();
199
$objWriter->startElement('i');
200
$objWriter->writeAttribute('val', ($element->getFont()->getItalic() ? 'true' : 'false'));
201
$objWriter->endElement();
203
// Superscript / subscript
204
if ($element->getFont()->getSuperScript() || $element->getFont()->getSubScript()) {
205
$objWriter->startElement('vertAlign');
206
if ($element->getFont()->getSuperScript()) {
207
$objWriter->writeAttribute('val', 'superscript');
208
} else if ($element->getFont()->getSubScript()) {
209
$objWriter->writeAttribute('val', 'subscript');
211
$objWriter->endElement();
215
$objWriter->startElement('strike');
216
$objWriter->writeAttribute('val', ($element->getFont()->getStrikethrough() ? 'true' : 'false'));
217
$objWriter->endElement();
220
$objWriter->startElement('color');
221
$objWriter->writeAttribute('rgb', $element->getFont()->getColor()->getARGB());
222
$objWriter->endElement();
225
$objWriter->startElement('sz');
226
$objWriter->writeAttribute('val', $element->getFont()->getSize());
227
$objWriter->endElement();
230
$objWriter->startElement('u');
231
$objWriter->writeAttribute('val', $element->getFont()->getUnderline());
232
$objWriter->endElement();
234
$objWriter->endElement();
238
$objWriter->startElement('t');
239
$objWriter->writeAttribute('xml:space', 'preserve');
240
$objWriter->writeRaw(PHPExcel_Shared_String::ControlCharacterPHP2OOXML( htmlspecialchars($element->getText()) ));
241
$objWriter->endElement();
243
$objWriter->endElement();
248
* Flip string table (for index searching)
250
* @param array $stringTable Stringtable
253
public function flipStringTable($stringTable = array()) {
255
$returnValue = array();
257
// Loop trough stringtable and add flipped items to $returnValue
258
foreach ($stringTable as $key => $value) {
259
if (! $value instanceof PHPExcel_RichText) {
260
$returnValue[$value] = $key;
261
} else if ($value instanceof PHPExcel_RichText) {
262
$returnValue[$value->getHashCode()] = $key;