Worksheet.php 54 KB

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