Worksheet.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2013 PHPExcel
  6. *
  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.
  11. *
  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.
  16. *
  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
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Writer_Excel2007
  23. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.9, 2013-06-02
  26. */
  27. /**
  28. * PHPExcel_Writer_Excel2007_Worksheet
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Writer_Excel2007
  32. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Writer_Excel2007_Worksheet extends PHPExcel_Writer_Excel2007_WriterPart
  35. {
  36. /**
  37. * Write worksheet to XML format
  38. *
  39. * @param PHPExcel_Worksheet $pSheet
  40. * @param string[] $pStringTable
  41. * @param boolean $includeCharts Flag indicating if we should write charts
  42. * @return string XML Output
  43. * @throws PHPExcel_Writer_Exception
  44. */
  45. public function writeWorksheet($pSheet = null, $pStringTable = null, $includeCharts = FALSE)
  46. {
  47. if (!is_null($pSheet)) {
  48. // Create XML writer
  49. $objWriter = null;
  50. if ($this->getParentWriter()->getUseDiskCaching()) {
  51. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  52. } else {
  53. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  54. }
  55. // XML header
  56. $objWriter->startDocument('1.0','UTF-8','yes');
  57. // Worksheet
  58. $objWriter->startElement('worksheet');
  59. $objWriter->writeAttribute('xml:space', 'preserve');
  60. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
  61. $objWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
  62. // sheetPr
  63. $this->_writeSheetPr($objWriter, $pSheet);
  64. // Dimension
  65. $this->_writeDimension($objWriter, $pSheet);
  66. // sheetViews
  67. $this->_writeSheetViews($objWriter, $pSheet);
  68. // sheetFormatPr
  69. $this->_writeSheetFormatPr($objWriter, $pSheet);
  70. // cols
  71. $this->_writeCols($objWriter, $pSheet);
  72. // sheetData
  73. $this->_writeSheetData($objWriter, $pSheet, $pStringTable);
  74. // sheetProtection
  75. $this->_writeSheetProtection($objWriter, $pSheet);
  76. // protectedRanges
  77. $this->_writeProtectedRanges($objWriter, $pSheet);
  78. // autoFilter
  79. $this->_writeAutoFilter($objWriter, $pSheet);
  80. // mergeCells
  81. $this->_writeMergeCells($objWriter, $pSheet);
  82. // conditionalFormatting
  83. $this->_writeConditionalFormatting($objWriter, $pSheet);
  84. // dataValidations
  85. $this->_writeDataValidations($objWriter, $pSheet);
  86. // hyperlinks
  87. $this->_writeHyperlinks($objWriter, $pSheet);
  88. // Print options
  89. $this->_writePrintOptions($objWriter, $pSheet);
  90. // Page margins
  91. $this->_writePageMargins($objWriter, $pSheet);
  92. // Page setup
  93. $this->_writePageSetup($objWriter, $pSheet);
  94. // Header / footer
  95. $this->_writeHeaderFooter($objWriter, $pSheet);
  96. // Breaks
  97. $this->_writeBreaks($objWriter, $pSheet);
  98. // Drawings and/or Charts
  99. $this->_writeDrawings($objWriter, $pSheet, $includeCharts);
  100. // LegacyDrawing
  101. $this->_writeLegacyDrawing($objWriter, $pSheet);
  102. // LegacyDrawingHF
  103. $this->_writeLegacyDrawingHF($objWriter, $pSheet);
  104. $objWriter->endElement();
  105. // Return
  106. return $objWriter->getData();
  107. } else {
  108. throw new PHPExcel_Writer_Exception("Invalid PHPExcel_Worksheet object passed.");
  109. }
  110. }
  111. /**
  112. * Write SheetPr
  113. *
  114. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  115. * @param PHPExcel_Worksheet $pSheet Worksheet
  116. * @throws PHPExcel_Writer_Exception
  117. */
  118. private function _writeSheetPr(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null)
  119. {
  120. // sheetPr
  121. $objWriter->startElement('sheetPr');
  122. //$objWriter->writeAttribute('codeName', $pSheet->getTitle());
  123. $autoFilterRange = $pSheet->getAutoFilter()->getRange();
  124. if (!empty($autoFilterRange)) {
  125. $objWriter->writeAttribute('filterMode', 1);
  126. $pSheet->getAutoFilter()->showHideRows();
  127. }
  128. // tabColor
  129. if ($pSheet->isTabColorSet()) {
  130. $objWriter->startElement('tabColor');
  131. $objWriter->writeAttribute('rgb', $pSheet->getTabColor()->getARGB());
  132. $objWriter->endElement();
  133. }
  134. // outlinePr
  135. $objWriter->startElement('outlinePr');
  136. $objWriter->writeAttribute('summaryBelow', ($pSheet->getShowSummaryBelow() ? '1' : '0'));
  137. $objWriter->writeAttribute('summaryRight', ($pSheet->getShowSummaryRight() ? '1' : '0'));
  138. $objWriter->endElement();
  139. // pageSetUpPr
  140. if ($pSheet->getPageSetup()->getFitToPage()) {
  141. $objWriter->startElement('pageSetUpPr');
  142. $objWriter->writeAttribute('fitToPage', '1');
  143. $objWriter->endElement();
  144. }
  145. $objWriter->endElement();
  146. }
  147. /**
  148. * Write Dimension
  149. *
  150. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  151. * @param PHPExcel_Worksheet $pSheet Worksheet
  152. * @throws PHPExcel_Writer_Exception
  153. */
  154. private function _writeDimension(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null)
  155. {
  156. // dimension
  157. $objWriter->startElement('dimension');
  158. $objWriter->writeAttribute('ref', $pSheet->calculateWorksheetDimension());
  159. $objWriter->endElement();
  160. }
  161. /**
  162. * Write SheetViews
  163. *
  164. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  165. * @param PHPExcel_Worksheet $pSheet Worksheet
  166. * @throws PHPExcel_Writer_Exception
  167. */
  168. private function _writeSheetViews(PHPExcel_Shared_XMLWriter $objWriter = NULL, PHPExcel_Worksheet $pSheet = NULL)
  169. {
  170. // sheetViews
  171. $objWriter->startElement('sheetViews');
  172. // Sheet selected?
  173. $sheetSelected = false;
  174. if ($this->getParentWriter()->getPHPExcel()->getIndex($pSheet) == $this->getParentWriter()->getPHPExcel()->getActiveSheetIndex())
  175. $sheetSelected = true;
  176. // sheetView
  177. $objWriter->startElement('sheetView');
  178. $objWriter->writeAttribute('tabSelected', $sheetSelected ? '1' : '0');
  179. $objWriter->writeAttribute('workbookViewId', '0');
  180. // Zoom scales
  181. if ($pSheet->getSheetView()->getZoomScale() != 100) {
  182. $objWriter->writeAttribute('zoomScale', $pSheet->getSheetView()->getZoomScale());
  183. }
  184. if ($pSheet->getSheetView()->getZoomScaleNormal() != 100) {
  185. $objWriter->writeAttribute('zoomScaleNormal', $pSheet->getSheetView()->getZoomScaleNormal());
  186. }
  187. // View Layout Type
  188. if ($pSheet->getSheetView()->getView() !== PHPExcel_Worksheet_SheetView::SHEETVIEW_NORMAL) {
  189. $objWriter->writeAttribute('view', $pSheet->getSheetView()->getView());
  190. }
  191. // Gridlines
  192. if ($pSheet->getShowGridlines()) {
  193. $objWriter->writeAttribute('showGridLines', 'true');
  194. } else {
  195. $objWriter->writeAttribute('showGridLines', 'false');
  196. }
  197. // Row and column headers
  198. if ($pSheet->getShowRowColHeaders()) {
  199. $objWriter->writeAttribute('showRowColHeaders', '1');
  200. } else {
  201. $objWriter->writeAttribute('showRowColHeaders', '0');
  202. }
  203. // Right-to-left
  204. if ($pSheet->getRightToLeft()) {
  205. $objWriter->writeAttribute('rightToLeft', 'true');
  206. }
  207. $activeCell = $pSheet->getActiveCell();
  208. // Pane
  209. $pane = '';
  210. $topLeftCell = $pSheet->getFreezePane();
  211. if (($topLeftCell != '') && ($topLeftCell != 'A1')) {
  212. $activeCell = $topLeftCell;
  213. // Calculate freeze coordinates
  214. $xSplit = $ySplit = 0;
  215. list($xSplit, $ySplit) = PHPExcel_Cell::coordinateFromString($topLeftCell);
  216. $xSplit = PHPExcel_Cell::columnIndexFromString($xSplit);
  217. // pane
  218. $pane = 'topRight';
  219. $objWriter->startElement('pane');
  220. if ($xSplit > 1)
  221. $objWriter->writeAttribute('xSplit', $xSplit - 1);
  222. if ($ySplit > 1) {
  223. $objWriter->writeAttribute('ySplit', $ySplit - 1);
  224. $pane = ($xSplit > 1) ? 'bottomRight' : 'bottomLeft';
  225. }
  226. $objWriter->writeAttribute('topLeftCell', $topLeftCell);
  227. $objWriter->writeAttribute('activePane', $pane);
  228. $objWriter->writeAttribute('state', 'frozen');
  229. $objWriter->endElement();
  230. if (($xSplit > 1) && ($ySplit > 1)) {
  231. // Write additional selections if more than two panes (ie both an X and a Y split)
  232. $objWriter->startElement('selection'); $objWriter->writeAttribute('pane', 'topRight'); $objWriter->endElement();
  233. $objWriter->startElement('selection'); $objWriter->writeAttribute('pane', 'bottomLeft'); $objWriter->endElement();
  234. }
  235. }
  236. // Selection
  237. // if ($pane != '') {
  238. // Only need to write selection element if we have a split pane
  239. // We cheat a little by over-riding the active cell selection, setting it to the split cell
  240. $objWriter->startElement('selection');
  241. if ($pane != '') {
  242. $objWriter->writeAttribute('pane', $pane);
  243. }
  244. $objWriter->writeAttribute('activeCell', $activeCell);
  245. $objWriter->writeAttribute('sqref', $activeCell);
  246. $objWriter->endElement();
  247. // }
  248. $objWriter->endElement();
  249. $objWriter->endElement();
  250. }
  251. /**
  252. * Write SheetFormatPr
  253. *
  254. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  255. * @param PHPExcel_Worksheet $pSheet Worksheet
  256. * @throws PHPExcel_Writer_Exception
  257. */
  258. private function _writeSheetFormatPr(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null)
  259. {
  260. // sheetFormatPr
  261. $objWriter->startElement('sheetFormatPr');
  262. // Default row height
  263. if ($pSheet->getDefaultRowDimension()->getRowHeight() >= 0) {
  264. $objWriter->writeAttribute('customHeight', 'true');
  265. $objWriter->writeAttribute('defaultRowHeight', PHPExcel_Shared_String::FormatNumber($pSheet->getDefaultRowDimension()->getRowHeight()));
  266. } else {
  267. $objWriter->writeAttribute('defaultRowHeight', '14.4');
  268. }
  269. // Set Zero Height row
  270. if ((string)$pSheet->getDefaultRowDimension()->getzeroHeight() == '1' ||
  271. strtolower((string)$pSheet->getDefaultRowDimension()->getzeroHeight()) == 'true' ) {
  272. $objWriter->writeAttribute('zeroHeight', '1');
  273. }
  274. // Default column width
  275. if ($pSheet->getDefaultColumnDimension()->getWidth() >= 0) {
  276. $objWriter->writeAttribute('defaultColWidth', PHPExcel_Shared_String::FormatNumber($pSheet->getDefaultColumnDimension()->getWidth()));
  277. }
  278. // Outline level - row
  279. $outlineLevelRow = 0;
  280. foreach ($pSheet->getRowDimensions() as $dimension) {
  281. if ($dimension->getOutlineLevel() > $outlineLevelRow) {
  282. $outlineLevelRow = $dimension->getOutlineLevel();
  283. }
  284. }
  285. $objWriter->writeAttribute('outlineLevelRow', (int)$outlineLevelRow);
  286. // Outline level - column
  287. $outlineLevelCol = 0;
  288. foreach ($pSheet->getColumnDimensions() as $dimension) {
  289. if ($dimension->getOutlineLevel() > $outlineLevelCol) {
  290. $outlineLevelCol = $dimension->getOutlineLevel();
  291. }
  292. }
  293. $objWriter->writeAttribute('outlineLevelCol', (int)$outlineLevelCol);
  294. $objWriter->endElement();
  295. }
  296. /**
  297. * Write Cols
  298. *
  299. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  300. * @param PHPExcel_Worksheet $pSheet Worksheet
  301. * @throws PHPExcel_Writer_Exception
  302. */
  303. private function _writeCols(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null)
  304. {
  305. // cols
  306. if (count($pSheet->getColumnDimensions()) > 0) {
  307. $objWriter->startElement('cols');
  308. $pSheet->calculateColumnWidths();
  309. // Loop through column dimensions
  310. foreach ($pSheet->getColumnDimensions() as $colDimension) {
  311. // col
  312. $objWriter->startElement('col');
  313. $objWriter->writeAttribute('min', PHPExcel_Cell::columnIndexFromString($colDimension->getColumnIndex()));
  314. $objWriter->writeAttribute('max', PHPExcel_Cell::columnIndexFromString($colDimension->getColumnIndex()));
  315. if ($colDimension->getWidth() < 0) {
  316. // No width set, apply default of 10
  317. $objWriter->writeAttribute('width', '9.10');
  318. } else {
  319. // Width set
  320. $objWriter->writeAttribute('width', PHPExcel_Shared_String::FormatNumber($colDimension->getWidth()));
  321. }
  322. // Column visibility
  323. if ($colDimension->getVisible() == false) {
  324. $objWriter->writeAttribute('hidden', 'true');
  325. }
  326. // Auto size?
  327. if ($colDimension->getAutoSize()) {
  328. $objWriter->writeAttribute('bestFit', 'true');
  329. }
  330. // Custom width?
  331. if ($colDimension->getWidth() != $pSheet->getDefaultColumnDimension()->getWidth()) {
  332. $objWriter->writeAttribute('customWidth', 'true');
  333. }
  334. // Collapsed
  335. if ($colDimension->getCollapsed() == true) {
  336. $objWriter->writeAttribute('collapsed', 'true');
  337. }
  338. // Outline level
  339. if ($colDimension->getOutlineLevel() > 0) {
  340. $objWriter->writeAttribute('outlineLevel', $colDimension->getOutlineLevel());
  341. }
  342. // Style
  343. $objWriter->writeAttribute('style', $colDimension->getXfIndex());
  344. $objWriter->endElement();
  345. }
  346. $objWriter->endElement();
  347. }
  348. }
  349. /**
  350. * Write SheetProtection
  351. *
  352. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  353. * @param PHPExcel_Worksheet $pSheet Worksheet
  354. * @throws PHPExcel_Writer_Exception
  355. */
  356. private function _writeSheetProtection(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null)
  357. {
  358. // sheetProtection
  359. $objWriter->startElement('sheetProtection');
  360. if ($pSheet->getProtection()->getPassword() != '') {
  361. $objWriter->writeAttribute('password', $pSheet->getProtection()->getPassword());
  362. }
  363. $objWriter->writeAttribute('sheet', ($pSheet->getProtection()->getSheet() ? 'true' : 'false'));
  364. $objWriter->writeAttribute('objects', ($pSheet->getProtection()->getObjects() ? 'true' : 'false'));
  365. $objWriter->writeAttribute('scenarios', ($pSheet->getProtection()->getScenarios() ? 'true' : 'false'));
  366. $objWriter->writeAttribute('formatCells', ($pSheet->getProtection()->getFormatCells() ? 'true' : 'false'));
  367. $objWriter->writeAttribute('formatColumns', ($pSheet->getProtection()->getFormatColumns() ? 'true' : 'false'));
  368. $objWriter->writeAttribute('formatRows', ($pSheet->getProtection()->getFormatRows() ? 'true' : 'false'));
  369. $objWriter->writeAttribute('insertColumns', ($pSheet->getProtection()->getInsertColumns() ? 'true' : 'false'));
  370. $objWriter->writeAttribute('insertRows', ($pSheet->getProtection()->getInsertRows() ? 'true' : 'false'));
  371. $objWriter->writeAttribute('insertHyperlinks', ($pSheet->getProtection()->getInsertHyperlinks() ? 'true' : 'false'));
  372. $objWriter->writeAttribute('deleteColumns', ($pSheet->getProtection()->getDeleteColumns() ? 'true' : 'false'));
  373. $objWriter->writeAttribute('deleteRows', ($pSheet->getProtection()->getDeleteRows() ? 'true' : 'false'));
  374. $objWriter->writeAttribute('selectLockedCells', ($pSheet->getProtection()->getSelectLockedCells() ? 'true' : 'false'));
  375. $objWriter->writeAttribute('sort', ($pSheet->getProtection()->getSort() ? 'true' : 'false'));
  376. $objWriter->writeAttribute('autoFilter', ($pSheet->getProtection()->getAutoFilter() ? 'true' : 'false'));
  377. $objWriter->writeAttribute('pivotTables', ($pSheet->getProtection()->getPivotTables() ? 'true' : 'false'));
  378. $objWriter->writeAttribute('selectUnlockedCells', ($pSheet->getProtection()->getSelectUnlockedCells() ? 'true' : 'false'));
  379. $objWriter->endElement();
  380. }
  381. /**
  382. * Write ConditionalFormatting
  383. *
  384. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  385. * @param PHPExcel_Worksheet $pSheet Worksheet
  386. * @throws PHPExcel_Writer_Exception
  387. */
  388. private function _writeConditionalFormatting(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null)
  389. {
  390. // Conditional id
  391. $id = 1;
  392. // Loop through styles in the current worksheet
  393. foreach ($pSheet->getConditionalStylesCollection() as $cellCoordinate => $conditionalStyles) {
  394. foreach ($conditionalStyles as $conditional) {
  395. // WHY was this again?
  396. // if ($this->getParentWriter()->getStylesConditionalHashTable()->getIndexForHashCode( $conditional->getHashCode() ) == '') {
  397. // continue;
  398. // }
  399. if ($conditional->getConditionType() != PHPExcel_Style_Conditional::CONDITION_NONE) {
  400. // conditionalFormatting
  401. $objWriter->startElement('conditionalFormatting');
  402. $objWriter->writeAttribute('sqref', $cellCoordinate);
  403. // cfRule
  404. $objWriter->startElement('cfRule');
  405. $objWriter->writeAttribute('type', $conditional->getConditionType());
  406. $objWriter->writeAttribute('dxfId', $this->getParentWriter()->getStylesConditionalHashTable()->getIndexForHashCode( $conditional->getHashCode() ));
  407. $objWriter->writeAttribute('priority', $id++);
  408. if (($conditional->getConditionType() == PHPExcel_Style_Conditional::CONDITION_CELLIS
  409. ||
  410. $conditional->getConditionType() == PHPExcel_Style_Conditional::CONDITION_CONTAINSTEXT)
  411. && $conditional->getOperatorType() != PHPExcel_Style_Conditional::OPERATOR_NONE) {
  412. $objWriter->writeAttribute('operator', $conditional->getOperatorType());
  413. }
  414. if ($conditional->getConditionType() == PHPExcel_Style_Conditional::CONDITION_CONTAINSTEXT
  415. && !is_null($conditional->getText())) {
  416. $objWriter->writeAttribute('text', $conditional->getText());
  417. }
  418. if ($conditional->getConditionType() == PHPExcel_Style_Conditional::CONDITION_CONTAINSTEXT
  419. && $conditional->getOperatorType() == PHPExcel_Style_Conditional::OPERATOR_CONTAINSTEXT
  420. && !is_null($conditional->getText())) {
  421. $objWriter->writeElement('formula', 'NOT(ISERROR(SEARCH("' . $conditional->getText() . '",' . $cellCoordinate . ')))');
  422. } else if ($conditional->getConditionType() == PHPExcel_Style_Conditional::CONDITION_CONTAINSTEXT
  423. && $conditional->getOperatorType() == PHPExcel_Style_Conditional::OPERATOR_BEGINSWITH
  424. && !is_null($conditional->getText())) {
  425. $objWriter->writeElement('formula', 'LEFT(' . $cellCoordinate . ',' . strlen($conditional->getText()) . ')="' . $conditional->getText() . '"');
  426. } else if ($conditional->getConditionType() == PHPExcel_Style_Conditional::CONDITION_CONTAINSTEXT
  427. && $conditional->getOperatorType() == PHPExcel_Style_Conditional::OPERATOR_ENDSWITH
  428. && !is_null($conditional->getText())) {
  429. $objWriter->writeElement('formula', 'RIGHT(' . $cellCoordinate . ',' . strlen($conditional->getText()) . ')="' . $conditional->getText() . '"');
  430. } else if ($conditional->getConditionType() == PHPExcel_Style_Conditional::CONDITION_CONTAINSTEXT
  431. && $conditional->getOperatorType() == PHPExcel_Style_Conditional::OPERATOR_NOTCONTAINS
  432. && !is_null($conditional->getText())) {
  433. $objWriter->writeElement('formula', 'ISERROR(SEARCH("' . $conditional->getText() . '",' . $cellCoordinate . '))');
  434. } else if ($conditional->getConditionType() == PHPExcel_Style_Conditional::CONDITION_CELLIS
  435. || $conditional->getConditionType() == PHPExcel_Style_Conditional::CONDITION_CONTAINSTEXT
  436. || $conditional->getConditionType() == PHPExcel_Style_Conditional::CONDITION_EXPRESSION) {
  437. foreach ($conditional->getConditions() as $formula) {
  438. // Formula
  439. $objWriter->writeElement('formula', $formula);
  440. }
  441. }
  442. $objWriter->endElement();
  443. $objWriter->endElement();
  444. }
  445. }
  446. }
  447. }
  448. /**
  449. * Write DataValidations
  450. *
  451. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  452. * @param PHPExcel_Worksheet $pSheet Worksheet
  453. * @throws PHPExcel_Writer_Exception
  454. */
  455. private function _writeDataValidations(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null)
  456. {
  457. // Datavalidation collection
  458. $dataValidationCollection = $pSheet->getDataValidationCollection();
  459. // Write data validations?
  460. if (!empty($dataValidationCollection)) {
  461. $objWriter->startElement('dataValidations');
  462. $objWriter->writeAttribute('count', count($dataValidationCollection));
  463. foreach ($dataValidationCollection as $coordinate => $dv) {
  464. $objWriter->startElement('dataValidation');
  465. if ($dv->getType() != '') {
  466. $objWriter->writeAttribute('type', $dv->getType());
  467. }
  468. if ($dv->getErrorStyle() != '') {
  469. $objWriter->writeAttribute('errorStyle', $dv->getErrorStyle());
  470. }
  471. if ($dv->getOperator() != '') {
  472. $objWriter->writeAttribute('operator', $dv->getOperator());
  473. }
  474. $objWriter->writeAttribute('allowBlank', ($dv->getAllowBlank() ? '1' : '0'));
  475. $objWriter->writeAttribute('showDropDown', (!$dv->getShowDropDown() ? '1' : '0'));
  476. $objWriter->writeAttribute('showInputMessage', ($dv->getShowInputMessage() ? '1' : '0'));
  477. $objWriter->writeAttribute('showErrorMessage', ($dv->getShowErrorMessage() ? '1' : '0'));
  478. if ($dv->getErrorTitle() !== '') {
  479. $objWriter->writeAttribute('errorTitle', $dv->getErrorTitle());
  480. }
  481. if ($dv->getError() !== '') {
  482. $objWriter->writeAttribute('error', $dv->getError());
  483. }
  484. if ($dv->getPromptTitle() !== '') {
  485. $objWriter->writeAttribute('promptTitle', $dv->getPromptTitle());
  486. }
  487. if ($dv->getPrompt() !== '') {
  488. $objWriter->writeAttribute('prompt', $dv->getPrompt());
  489. }
  490. $objWriter->writeAttribute('sqref', $coordinate);
  491. if ($dv->getFormula1() !== '') {
  492. $objWriter->writeElement('formula1', $dv->getFormula1());
  493. }
  494. if ($dv->getFormula2() !== '') {
  495. $objWriter->writeElement('formula2', $dv->getFormula2());
  496. }
  497. $objWriter->endElement();
  498. }
  499. $objWriter->endElement();
  500. }
  501. }
  502. /**
  503. * Write Hyperlinks
  504. *
  505. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  506. * @param PHPExcel_Worksheet $pSheet Worksheet
  507. * @throws PHPExcel_Writer_Exception
  508. */
  509. private function _writeHyperlinks(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null)
  510. {
  511. // Hyperlink collection
  512. $hyperlinkCollection = $pSheet->getHyperlinkCollection();
  513. // Relation ID
  514. $relationId = 1;
  515. // Write hyperlinks?
  516. if (!empty($hyperlinkCollection)) {
  517. $objWriter->startElement('hyperlinks');
  518. foreach ($hyperlinkCollection as $coordinate => $hyperlink) {
  519. $objWriter->startElement('hyperlink');
  520. $objWriter->writeAttribute('ref', $coordinate);
  521. if (!$hyperlink->isInternal()) {
  522. $objWriter->writeAttribute('r:id', 'rId_hyperlink_' . $relationId);
  523. ++$relationId;
  524. } else {
  525. $objWriter->writeAttribute('location', str_replace('sheet://', '', $hyperlink->getUrl()));
  526. }
  527. if ($hyperlink->getTooltip() != '') {
  528. $objWriter->writeAttribute('tooltip', $hyperlink->getTooltip());
  529. }
  530. $objWriter->endElement();
  531. }
  532. $objWriter->endElement();
  533. }
  534. }
  535. /**
  536. * Write ProtectedRanges
  537. *
  538. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  539. * @param PHPExcel_Worksheet $pSheet Worksheet
  540. * @throws PHPExcel_Writer_Exception
  541. */
  542. private function _writeProtectedRanges(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null)
  543. {
  544. if (count($pSheet->getProtectedCells()) > 0) {
  545. // protectedRanges
  546. $objWriter->startElement('protectedRanges');
  547. // Loop protectedRanges
  548. foreach ($pSheet->getProtectedCells() as $protectedCell => $passwordHash) {
  549. // protectedRange
  550. $objWriter->startElement('protectedRange');
  551. $objWriter->writeAttribute('name', 'p' . md5($protectedCell));
  552. $objWriter->writeAttribute('sqref', $protectedCell);
  553. if (!empty($passwordHash)) {
  554. $objWriter->writeAttribute('password', $passwordHash);
  555. }
  556. $objWriter->endElement();
  557. }
  558. $objWriter->endElement();
  559. }
  560. }
  561. /**
  562. * Write MergeCells
  563. *
  564. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  565. * @param PHPExcel_Worksheet $pSheet Worksheet
  566. * @throws PHPExcel_Writer_Exception
  567. */
  568. private function _writeMergeCells(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null)
  569. {
  570. if (count($pSheet->getMergeCells()) > 0) {
  571. // mergeCells
  572. $objWriter->startElement('mergeCells');
  573. // Loop mergeCells
  574. foreach ($pSheet->getMergeCells() as $mergeCell) {
  575. // mergeCell
  576. $objWriter->startElement('mergeCell');
  577. $objWriter->writeAttribute('ref', $mergeCell);
  578. $objWriter->endElement();
  579. }
  580. $objWriter->endElement();
  581. }
  582. }
  583. /**
  584. * Write PrintOptions
  585. *
  586. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  587. * @param PHPExcel_Worksheet $pSheet Worksheet
  588. * @throws PHPExcel_Writer_Exception
  589. */
  590. private function _writePrintOptions(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null)
  591. {
  592. // printOptions
  593. $objWriter->startElement('printOptions');
  594. $objWriter->writeAttribute('gridLines', ($pSheet->getPrintGridlines() ? 'true': 'false'));
  595. $objWriter->writeAttribute('gridLinesSet', 'true');
  596. if ($pSheet->getPageSetup()->getHorizontalCentered()) {
  597. $objWriter->writeAttribute('horizontalCentered', 'true');
  598. }
  599. if ($pSheet->getPageSetup()->getVerticalCentered()) {
  600. $objWriter->writeAttribute('verticalCentered', 'true');
  601. }
  602. $objWriter->endElement();
  603. }
  604. /**
  605. * Write PageMargins
  606. *
  607. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  608. * @param PHPExcel_Worksheet $pSheet Worksheet
  609. * @throws PHPExcel_Writer_Exception
  610. */
  611. private function _writePageMargins(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null)
  612. {
  613. // pageMargins
  614. $objWriter->startElement('pageMargins');
  615. $objWriter->writeAttribute('left', PHPExcel_Shared_String::FormatNumber($pSheet->getPageMargins()->getLeft()));
  616. $objWriter->writeAttribute('right', PHPExcel_Shared_String::FormatNumber($pSheet->getPageMargins()->getRight()));
  617. $objWriter->writeAttribute('top', PHPExcel_Shared_String::FormatNumber($pSheet->getPageMargins()->getTop()));
  618. $objWriter->writeAttribute('bottom', PHPExcel_Shared_String::FormatNumber($pSheet->getPageMargins()->getBottom()));
  619. $objWriter->writeAttribute('header', PHPExcel_Shared_String::FormatNumber($pSheet->getPageMargins()->getHeader()));
  620. $objWriter->writeAttribute('footer', PHPExcel_Shared_String::FormatNumber($pSheet->getPageMargins()->getFooter()));
  621. $objWriter->endElement();
  622. }
  623. /**
  624. * Write AutoFilter
  625. *
  626. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  627. * @param PHPExcel_Worksheet $pSheet Worksheet
  628. * @throws PHPExcel_Writer_Exception
  629. */
  630. private function _writeAutoFilter(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null)
  631. {
  632. $autoFilterRange = $pSheet->getAutoFilter()->getRange();
  633. if (!empty($autoFilterRange)) {
  634. // autoFilter
  635. $objWriter->startElement('autoFilter');
  636. // Strip any worksheet reference from the filter coordinates
  637. $range = PHPExcel_Cell::splitRange($autoFilterRange);
  638. $range = $range[0];
  639. // Strip any worksheet ref
  640. if (strpos($range[0],'!') !== false) {
  641. list($ws,$range[0]) = explode('!',$range[0]);
  642. }
  643. $range = implode(':', $range);
  644. $objWriter->writeAttribute('ref', str_replace('$','',$range));
  645. $columns = $pSheet->getAutoFilter()->getColumns();
  646. if (count($columns > 0)) {
  647. foreach($columns as $columnID => $column) {
  648. $rules = $column->getRules();
  649. if (count($rules > 0)) {
  650. $objWriter->startElement('filterColumn');
  651. $objWriter->writeAttribute('colId', $pSheet->getAutoFilter()->getColumnOffset($columnID));
  652. $objWriter->startElement( $column->getFilterType());
  653. if ($column->getJoin() == PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_COLUMN_JOIN_AND) {
  654. $objWriter->writeAttribute('and', 1);
  655. }
  656. foreach ($rules as $rule) {
  657. if (($column->getFilterType() === PHPExcel_Worksheet_AutoFilter_Column::AUTOFILTER_FILTERTYPE_FILTER) &&
  658. ($rule->getOperator() === PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_EQUAL) &&
  659. ($rule->getValue() === '')) {
  660. // Filter rule for Blanks
  661. $objWriter->writeAttribute('blank', 1);
  662. } elseif($rule->getRuleType() === PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_DYNAMICFILTER) {
  663. // Dynamic Filter Rule
  664. $objWriter->writeAttribute('type', $rule->getGrouping());
  665. $val = $column->getAttribute('val');
  666. if ($val !== NULL) {
  667. $objWriter->writeAttribute('val', $val);
  668. }
  669. $maxVal = $column->getAttribute('maxVal');
  670. if ($maxVal !== NULL) {
  671. $objWriter->writeAttribute('maxVal', $maxVal);
  672. }
  673. } elseif($rule->getRuleType() === PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_TOPTENFILTER) {
  674. // Top 10 Filter Rule
  675. $objWriter->writeAttribute('val', $rule->getValue());
  676. $objWriter->writeAttribute('percent', (($rule->getOperator() === PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_PERCENT) ? '1' : '0'));
  677. $objWriter->writeAttribute('top', (($rule->getGrouping() === PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_TOP) ? '1': '0'));
  678. } else {
  679. // Filter, DateGroupItem or CustomFilter
  680. $objWriter->startElement($rule->getRuleType());
  681. if ($rule->getOperator() !== PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_COLUMN_RULE_EQUAL) {
  682. $objWriter->writeAttribute('operator', $rule->getOperator());
  683. }
  684. if ($rule->getRuleType() === PHPExcel_Worksheet_AutoFilter_Column_Rule::AUTOFILTER_RULETYPE_DATEGROUP) {
  685. // Date Group filters
  686. foreach($rule->getValue() as $key => $value) {
  687. if ($value > '') $objWriter->writeAttribute($key, $value);
  688. }
  689. $objWriter->writeAttribute('dateTimeGrouping', $rule->getGrouping());
  690. } else {
  691. $objWriter->writeAttribute('val', $rule->getValue());
  692. }
  693. $objWriter->endElement();
  694. }
  695. }
  696. $objWriter->endElement();
  697. $objWriter->endElement();
  698. }
  699. }
  700. }
  701. $objWriter->endElement();
  702. }
  703. }
  704. /**
  705. * Write PageSetup
  706. *
  707. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  708. * @param PHPExcel_Worksheet $pSheet Worksheet
  709. * @throws PHPExcel_Writer_Exception
  710. */
  711. private function _writePageSetup(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null)
  712. {
  713. // pageSetup
  714. $objWriter->startElement('pageSetup');
  715. $objWriter->writeAttribute('paperSize', $pSheet->getPageSetup()->getPaperSize());
  716. $objWriter->writeAttribute('orientation', $pSheet->getPageSetup()->getOrientation());
  717. if (!is_null($pSheet->getPageSetup()->getScale())) {
  718. $objWriter->writeAttribute('scale', $pSheet->getPageSetup()->getScale());
  719. }
  720. if (!is_null($pSheet->getPageSetup()->getFitToHeight())) {
  721. $objWriter->writeAttribute('fitToHeight', $pSheet->getPageSetup()->getFitToHeight());
  722. } else {
  723. $objWriter->writeAttribute('fitToHeight', '0');
  724. }
  725. if (!is_null($pSheet->getPageSetup()->getFitToWidth())) {
  726. $objWriter->writeAttribute('fitToWidth', $pSheet->getPageSetup()->getFitToWidth());
  727. } else {
  728. $objWriter->writeAttribute('fitToWidth', '0');
  729. }
  730. if (!is_null($pSheet->getPageSetup()->getFirstPageNumber())) {
  731. $objWriter->writeAttribute('firstPageNumber', $pSheet->getPageSetup()->getFirstPageNumber());
  732. $objWriter->writeAttribute('useFirstPageNumber', '1');
  733. }
  734. $objWriter->endElement();
  735. }
  736. /**
  737. * Write Header / Footer
  738. *
  739. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  740. * @param PHPExcel_Worksheet $pSheet Worksheet
  741. * @throws PHPExcel_Writer_Exception
  742. */
  743. private function _writeHeaderFooter(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null)
  744. {
  745. // headerFooter
  746. $objWriter->startElement('headerFooter');
  747. $objWriter->writeAttribute('differentOddEven', ($pSheet->getHeaderFooter()->getDifferentOddEven() ? 'true' : 'false'));
  748. $objWriter->writeAttribute('differentFirst', ($pSheet->getHeaderFooter()->getDifferentFirst() ? 'true' : 'false'));
  749. $objWriter->writeAttribute('scaleWithDoc', ($pSheet->getHeaderFooter()->getScaleWithDocument() ? 'true' : 'false'));
  750. $objWriter->writeAttribute('alignWithMargins', ($pSheet->getHeaderFooter()->getAlignWithMargins() ? 'true' : 'false'));
  751. $objWriter->writeElement('oddHeader', $pSheet->getHeaderFooter()->getOddHeader());
  752. $objWriter->writeElement('oddFooter', $pSheet->getHeaderFooter()->getOddFooter());
  753. $objWriter->writeElement('evenHeader', $pSheet->getHeaderFooter()->getEvenHeader());
  754. $objWriter->writeElement('evenFooter', $pSheet->getHeaderFooter()->getEvenFooter());
  755. $objWriter->writeElement('firstHeader', $pSheet->getHeaderFooter()->getFirstHeader());
  756. $objWriter->writeElement('firstFooter', $pSheet->getHeaderFooter()->getFirstFooter());
  757. $objWriter->endElement();
  758. }
  759. /**
  760. * Write Breaks
  761. *
  762. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  763. * @param PHPExcel_Worksheet $pSheet Worksheet
  764. * @throws PHPExcel_Writer_Exception
  765. */
  766. private function _writeBreaks(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null)
  767. {
  768. // Get row and column breaks
  769. $aRowBreaks = array();
  770. $aColumnBreaks = array();
  771. foreach ($pSheet->getBreaks() as $cell => $breakType) {
  772. if ($breakType == PHPExcel_Worksheet::BREAK_ROW) {
  773. $aRowBreaks[] = $cell;
  774. } else if ($breakType == PHPExcel_Worksheet::BREAK_COLUMN) {
  775. $aColumnBreaks[] = $cell;
  776. }
  777. }
  778. // rowBreaks
  779. if (!empty($aRowBreaks)) {
  780. $objWriter->startElement('rowBreaks');
  781. $objWriter->writeAttribute('count', count($aRowBreaks));
  782. $objWriter->writeAttribute('manualBreakCount', count($aRowBreaks));
  783. foreach ($aRowBreaks as $cell) {
  784. $coords = PHPExcel_Cell::coordinateFromString($cell);
  785. $objWriter->startElement('brk');
  786. $objWriter->writeAttribute('id', $coords[1]);
  787. $objWriter->writeAttribute('man', '1');
  788. $objWriter->endElement();
  789. }
  790. $objWriter->endElement();
  791. }
  792. // Second, write column breaks
  793. if (!empty($aColumnBreaks)) {
  794. $objWriter->startElement('colBreaks');
  795. $objWriter->writeAttribute('count', count($aColumnBreaks));
  796. $objWriter->writeAttribute('manualBreakCount', count($aColumnBreaks));
  797. foreach ($aColumnBreaks as $cell) {
  798. $coords = PHPExcel_Cell::coordinateFromString($cell);
  799. $objWriter->startElement('brk');
  800. $objWriter->writeAttribute('id', PHPExcel_Cell::columnIndexFromString($coords[0]) - 1);
  801. $objWriter->writeAttribute('man', '1');
  802. $objWriter->endElement();
  803. }
  804. $objWriter->endElement();
  805. }
  806. }
  807. /**
  808. * Write SheetData
  809. *
  810. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  811. * @param PHPExcel_Worksheet $pSheet Worksheet
  812. * @param string[] $pStringTable String table
  813. * @throws PHPExcel_Writer_Exception
  814. */
  815. private function _writeSheetData(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null, $pStringTable = null)
  816. {
  817. if (is_array($pStringTable)) {
  818. // Flipped stringtable, for faster index searching
  819. $aFlippedStringTable = $this->getParentWriter()->getWriterPart('stringtable')->flipStringTable($pStringTable);
  820. // sheetData
  821. $objWriter->startElement('sheetData');
  822. // Get column count
  823. $colCount = PHPExcel_Cell::columnIndexFromString($pSheet->getHighestColumn());
  824. // Highest row number
  825. $highestRow = $pSheet->getHighestRow();
  826. // Loop through cells
  827. $cellsByRow = array();
  828. foreach ($pSheet->getCellCollection() as $cellID) {
  829. $cellAddress = PHPExcel_Cell::coordinateFromString($cellID);
  830. $cellsByRow[$cellAddress[1]][] = $cellID;
  831. }
  832. $currentRow = 0;
  833. while($currentRow++ < $highestRow) {
  834. // Get row dimension
  835. $rowDimension = $pSheet->getRowDimension($currentRow);
  836. // Write current row?
  837. $writeCurrentRow = isset($cellsByRow[$currentRow]) ||
  838. $rowDimension->getRowHeight() >= 0 ||
  839. $rowDimension->getVisible() == false ||
  840. $rowDimension->getCollapsed() == true ||
  841. $rowDimension->getOutlineLevel() > 0 ||
  842. $rowDimension->getXfIndex() !== null;
  843. if ($writeCurrentRow) {
  844. // Start a new row
  845. $objWriter->startElement('row');
  846. $objWriter->writeAttribute('r', $currentRow);
  847. $objWriter->writeAttribute('spans', '1:' . $colCount);
  848. // Row dimensions
  849. if ($rowDimension->getRowHeight() >= 0) {
  850. $objWriter->writeAttribute('customHeight', '1');
  851. $objWriter->writeAttribute('ht', PHPExcel_Shared_String::FormatNumber($rowDimension->getRowHeight()));
  852. }
  853. // Row visibility
  854. if ($rowDimension->getVisible() == false) {
  855. $objWriter->writeAttribute('hidden', 'true');
  856. }
  857. // Collapsed
  858. if ($rowDimension->getCollapsed() == true) {
  859. $objWriter->writeAttribute('collapsed', 'true');
  860. }
  861. // Outline level
  862. if ($rowDimension->getOutlineLevel() > 0) {
  863. $objWriter->writeAttribute('outlineLevel', $rowDimension->getOutlineLevel());
  864. }
  865. // Style
  866. if ($rowDimension->getXfIndex() !== null) {
  867. $objWriter->writeAttribute('s', $rowDimension->getXfIndex());
  868. $objWriter->writeAttribute('customFormat', '1');
  869. }
  870. // Write cells
  871. if (isset($cellsByRow[$currentRow])) {
  872. foreach($cellsByRow[$currentRow] as $cellAddress) {
  873. // Write cell
  874. $this->_writeCell($objWriter, $pSheet, $cellAddress, $pStringTable, $aFlippedStringTable);
  875. }
  876. }
  877. // End row
  878. $objWriter->endElement();
  879. }
  880. }
  881. $objWriter->endElement();
  882. } else {
  883. throw new PHPExcel_Writer_Exception("Invalid parameters passed.");
  884. }
  885. }
  886. /**
  887. * Write Cell
  888. *
  889. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  890. * @param PHPExcel_Worksheet $pSheet Worksheet
  891. * @param PHPExcel_Cell $pCellAddress Cell Address
  892. * @param string[] $pStringTable String table
  893. * @param string[] $pFlippedStringTable String table (flipped), for faster index searching
  894. * @throws PHPExcel_Writer_Exception
  895. */
  896. private function _writeCell(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null, $pCellAddress = null, $pStringTable = null, $pFlippedStringTable = null)
  897. {
  898. if (is_array($pStringTable) && is_array($pFlippedStringTable)) {
  899. // Cell
  900. $pCell = $pSheet->getCell($pCellAddress);
  901. $objWriter->startElement('c');
  902. $objWriter->writeAttribute('r', $pCellAddress);
  903. // Sheet styles
  904. if ($pCell->getXfIndex() != '') {
  905. $objWriter->writeAttribute('s', $pCell->getXfIndex());
  906. }
  907. // If cell value is supplied, write cell value
  908. $cellValue = $pCell->getValue();
  909. if (is_object($cellValue) || $cellValue !== '') {
  910. // Map type
  911. $mappedType = $pCell->getDataType();
  912. // Write data type depending on its type
  913. switch (strtolower($mappedType)) {
  914. case 'inlinestr': // Inline string
  915. case 's': // String
  916. case 'b': // Boolean
  917. $objWriter->writeAttribute('t', $mappedType);
  918. break;
  919. case 'f': // Formula
  920. $calculatedValue = ($this->getParentWriter()->getPreCalculateFormulas()) ?
  921. $pCell->getCalculatedValue() :
  922. $cellValue;
  923. if (is_string($calculatedValue)) {
  924. $objWriter->writeAttribute('t', 'str');
  925. }
  926. break;
  927. case 'e': // Error
  928. $objWriter->writeAttribute('t', $mappedType);
  929. }
  930. // Write data depending on its type
  931. switch (strtolower($mappedType)) {
  932. case 'inlinestr': // Inline string
  933. if (! $cellValue instanceof PHPExcel_RichText) {
  934. $objWriter->writeElement('t', PHPExcel_Shared_String::ControlCharacterPHP2OOXML( htmlspecialchars($cellValue) ) );
  935. } else if ($cellValue instanceof PHPExcel_RichText) {
  936. $objWriter->startElement('is');
  937. $this->getParentWriter()->getWriterPart('stringtable')->writeRichText($objWriter, $cellValue);
  938. $objWriter->endElement();
  939. }
  940. break;
  941. case 's': // String
  942. if (! $cellValue instanceof PHPExcel_RichText) {
  943. if (isset($pFlippedStringTable[$cellValue])) {
  944. $objWriter->writeElement('v', $pFlippedStringTable[$cellValue]);
  945. }
  946. } else if ($cellValue instanceof PHPExcel_RichText) {
  947. $objWriter->writeElement('v', $pFlippedStringTable[$cellValue->getHashCode()]);
  948. }
  949. break;
  950. case 'f': // Formula
  951. $attributes = $pCell->getFormulaAttributes();
  952. if($attributes['t'] == 'array') {
  953. $objWriter->startElement('f');
  954. $objWriter->writeAttribute('t', 'array');
  955. $objWriter->writeAttribute('ref', $pCellAddress);
  956. $objWriter->writeAttribute('aca', '1');
  957. $objWriter->writeAttribute('ca', '1');
  958. $objWriter->text(substr($cellValue, 1));
  959. $objWriter->endElement();
  960. } else {
  961. $objWriter->writeElement('f', substr($cellValue, 1));
  962. }
  963. if ($this->getParentWriter()->getOffice2003Compatibility() === false) {
  964. if ($this->getParentWriter()->getPreCalculateFormulas()) {
  965. // $calculatedValue = $pCell->getCalculatedValue();
  966. if (!is_array($calculatedValue) && substr($calculatedValue, 0, 1) != '#') {
  967. $objWriter->writeElement('v', PHPExcel_Shared_String::FormatNumber($calculatedValue));
  968. } else {
  969. $objWriter->writeElement('v', '0');
  970. }
  971. } else {
  972. $objWriter->writeElement('v', '0');
  973. }
  974. }
  975. break;
  976. case 'n': // Numeric
  977. // force point as decimal separator in case current locale uses comma
  978. $objWriter->writeElement('v', str_replace(',', '.', $cellValue));
  979. break;
  980. case 'b': // Boolean
  981. $objWriter->writeElement('v', ($cellValue ? '1' : '0'));
  982. break;
  983. case 'e': // Error
  984. if (substr($cellValue, 0, 1) == '=') {
  985. $objWriter->writeElement('f', substr($cellValue, 1));
  986. $objWriter->writeElement('v', substr($cellValue, 1));
  987. } else {
  988. $objWriter->writeElement('v', $cellValue);
  989. }
  990. break;
  991. }
  992. }
  993. $objWriter->endElement();
  994. } else {
  995. throw new PHPExcel_Writer_Exception("Invalid parameters passed.");
  996. }
  997. }
  998. /**
  999. * Write Drawings
  1000. *
  1001. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  1002. * @param PHPExcel_Worksheet $pSheet Worksheet
  1003. * @param boolean $includeCharts Flag indicating if we should include drawing details for charts
  1004. * @throws PHPExcel_Writer_Exception
  1005. */
  1006. private function _writeDrawings(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null, $includeCharts = FALSE)
  1007. {
  1008. $chartCount = ($includeCharts) ? $pSheet->getChartCollection()->count() : 0;
  1009. // If sheet contains drawings, add the relationships
  1010. if (($pSheet->getDrawingCollection()->count() > 0) ||
  1011. ($chartCount > 0)) {
  1012. $objWriter->startElement('drawing');
  1013. $objWriter->writeAttribute('r:id', 'rId1');
  1014. $objWriter->endElement();
  1015. }
  1016. }
  1017. /**
  1018. * Write LegacyDrawing
  1019. *
  1020. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  1021. * @param PHPExcel_Worksheet $pSheet Worksheet
  1022. * @throws PHPExcel_Writer_Exception
  1023. */
  1024. private function _writeLegacyDrawing(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null)
  1025. {
  1026. // If sheet contains comments, add the relationships
  1027. if (count($pSheet->getComments()) > 0) {
  1028. $objWriter->startElement('legacyDrawing');
  1029. $objWriter->writeAttribute('r:id', 'rId_comments_vml1');
  1030. $objWriter->endElement();
  1031. }
  1032. }
  1033. /**
  1034. * Write LegacyDrawingHF
  1035. *
  1036. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  1037. * @param PHPExcel_Worksheet $pSheet Worksheet
  1038. * @throws PHPExcel_Writer_Exception
  1039. */
  1040. private function _writeLegacyDrawingHF(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null)
  1041. {
  1042. // If sheet contains images, add the relationships
  1043. if (count($pSheet->getHeaderFooter()->getImages()) > 0) {
  1044. $objWriter->startElement('legacyDrawingHF');
  1045. $objWriter->writeAttribute('r:id', 'rId_headerfooter_vml1');
  1046. $objWriter->endElement();
  1047. }
  1048. }
  1049. }