c4e348d9772aac2a9eb88a06ac6536d0e83e6d5a.svn-base 44 KB

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