PageRenderTime 63ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 1ms

/lib/PHPExcel/Writer/Excel5/Worksheet.php

https://github.com/yuweijun/blog
PHP | 4240 lines | 2809 code | 441 blank | 990 comment | 374 complexity | 4cb0aacbca1e5465262e24da720656ab MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * PHPExcel_Writer_Excel5_Worksheet
  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_Excel5
  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. // Original file header of PEAR::Spreadsheet_Excel_Writer_Worksheet (used as the base for this class):
  28. // -----------------------------------------------------------------------------------------
  29. // /*
  30. // * Module written/ported by Xavier Noguer <xnoguer@rezebra.com>
  31. // *
  32. // * The majority of this is _NOT_ my code. I simply ported it from the
  33. // * PERL Spreadsheet::WriteExcel module.
  34. // *
  35. // * The author of the Spreadsheet::WriteExcel module is John McNamara
  36. // * <jmcnamara@cpan.org>
  37. // *
  38. // * I _DO_ maintain this code, and John McNamara has nothing to do with the
  39. // * porting of this code to PHP. Any questions directly related to this
  40. // * class library should be directed to me.
  41. // *
  42. // * License Information:
  43. // *
  44. // * Spreadsheet_Excel_Writer: A library for generating Excel Spreadsheets
  45. // * Copyright (c) 2002-2003 Xavier Noguer xnoguer@rezebra.com
  46. // *
  47. // * This library is free software; you can redistribute it and/or
  48. // * modify it under the terms of the GNU Lesser General Public
  49. // * License as published by the Free Software Foundation; either
  50. // * version 2.1 of the License, or (at your option) any later version.
  51. // *
  52. // * This library is distributed in the hope that it will be useful,
  53. // * but WITHOUT ANY WARRANTY; without even the implied warranty of
  54. // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  55. // * Lesser General Public License for more details.
  56. // *
  57. // * You should have received a copy of the GNU Lesser General Public
  58. // * License along with this library; if not, write to the Free Software
  59. // * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  60. // */
  61. class PHPExcel_Writer_Excel5_Worksheet extends PHPExcel_Writer_Excel5_BIFFwriter
  62. {
  63. /**
  64. * Formula parser
  65. *
  66. * @var PHPExcel_Writer_Excel5_Parser
  67. */
  68. private $parser;
  69. /**
  70. * Maximum number of characters for a string (LABEL record in BIFF5)
  71. * @var integer
  72. */
  73. private $xlsStringMaxLength;
  74. /**
  75. * Array containing format information for columns
  76. * @var array
  77. */
  78. private $columnInfo;
  79. /**
  80. * Array containing the selected area for the worksheet
  81. * @var array
  82. */
  83. private $selection;
  84. /**
  85. * The active pane for the worksheet
  86. * @var integer
  87. */
  88. private $activePane;
  89. /**
  90. * Whether to use outline.
  91. * @var integer
  92. */
  93. private $outlineOn;
  94. /**
  95. * Auto outline styles.
  96. * @var bool
  97. */
  98. private $outlineStyle;
  99. /**
  100. * Whether to have outline summary below.
  101. * @var bool
  102. */
  103. private $outlineBelow;
  104. /**
  105. * Whether to have outline summary at the right.
  106. * @var bool
  107. */
  108. private $outlineRight;
  109. /**
  110. * Reference to the total number of strings in the workbook
  111. * @var integer
  112. */
  113. private $stringTotal;
  114. /**
  115. * Reference to the number of unique strings in the workbook
  116. * @var integer
  117. */
  118. private $stringUnique;
  119. /**
  120. * Reference to the array containing all the unique strings in the workbook
  121. * @var array
  122. */
  123. private $stringTable;
  124. /**
  125. * Color cache
  126. */
  127. private $colors;
  128. /**
  129. * Index of first used row (at least 0)
  130. * @var int
  131. */
  132. private $firstRowIndex;
  133. /**
  134. * Index of last used row. (no used rows means -1)
  135. * @var int
  136. */
  137. private $lastRowIndex;
  138. /**
  139. * Index of first used column (at least 0)
  140. * @var int
  141. */
  142. private $firstColumnIndex;
  143. /**
  144. * Index of last used column (no used columns means -1)
  145. * @var int
  146. */
  147. private $lastColumnIndex;
  148. /**
  149. * Sheet object
  150. * @var PHPExcel_Worksheet
  151. */
  152. public $phpSheet;
  153. /**
  154. * Count cell style Xfs
  155. *
  156. * @var int
  157. */
  158. private $countCellStyleXfs;
  159. /**
  160. * Escher object corresponding to MSODRAWING
  161. *
  162. * @var PHPExcel_Shared_Escher
  163. */
  164. private $escher;
  165. /**
  166. * Array of font hashes associated to FONT records index
  167. *
  168. * @var array
  169. */
  170. public $fontHashIndex;
  171. /**
  172. * Constructor
  173. *
  174. * @param int &$str_total Total number of strings
  175. * @param int &$str_unique Total number of unique strings
  176. * @param array &$str_table String Table
  177. * @param array &$colors Colour Table
  178. * @param mixed $parser The formula parser created for the Workbook
  179. * @param boolean $preCalculateFormulas Flag indicating whether formulas should be calculated or just written
  180. * @param string $phpSheet The worksheet to write
  181. * @param PHPExcel_Worksheet $phpSheet
  182. */
  183. public function __construct(&$str_total, &$str_unique, &$str_table, &$colors, $parser, $preCalculateFormulas, $phpSheet)
  184. {
  185. // It needs to call its parent's constructor explicitly
  186. parent::__construct();
  187. // change BIFFwriter limit for CONTINUE records
  188. // $this->_limit = 8224;
  189. $this->_preCalculateFormulas = $preCalculateFormulas;
  190. $this->stringTotal = &$str_total;
  191. $this->stringUnique = &$str_unique;
  192. $this->stringTable = &$str_table;
  193. $this->colors = &$colors;
  194. $this->parser = $parser;
  195. $this->phpSheet = $phpSheet;
  196. //$this->ext_sheets = array();
  197. //$this->offset = 0;
  198. $this->xlsStringMaxLength = 255;
  199. $this->columnInfo = array();
  200. $this->selection = array(0,0,0,0);
  201. $this->activePane = 3;
  202. $this->_print_headers = 0;
  203. $this->outlineStyle = 0;
  204. $this->outlineBelow = 1;
  205. $this->outlineRight = 1;
  206. $this->outlineOn = 1;
  207. $this->fontHashIndex = array();
  208. // calculate values for DIMENSIONS record
  209. $minR = 1;
  210. $minC = 'A';
  211. $maxR = $this->phpSheet->getHighestRow();
  212. $maxC = $this->phpSheet->getHighestColumn();
  213. // Determine lowest and highest column and row
  214. // $this->firstRowIndex = ($minR > 65535) ? 65535 : $minR;
  215. $this->lastRowIndex = ($maxR > 65535) ? 65535 : $maxR ;
  216. $this->firstColumnIndex = PHPExcel_Cell::columnIndexFromString($minC);
  217. $this->lastColumnIndex = PHPExcel_Cell::columnIndexFromString($maxC);
  218. // if ($this->firstColumnIndex > 255) $this->firstColumnIndex = 255;
  219. if ($this->lastColumnIndex > 255) {
  220. $this->lastColumnIndex = 255;
  221. }
  222. $this->countCellStyleXfs = count($phpSheet->getParent()->getCellStyleXfCollection());
  223. }
  224. /**
  225. * Add data to the beginning of the workbook (note the reverse order)
  226. * and to the end of the workbook.
  227. *
  228. * @access public
  229. * @see PHPExcel_Writer_Excel5_Workbook::storeWorkbook()
  230. */
  231. public function close()
  232. {
  233. $phpSheet = $this->phpSheet;
  234. $num_sheets = $phpSheet->getParent()->getSheetCount();
  235. // Write BOF record
  236. $this->storeBof(0x0010);
  237. // Write PRINTHEADERS
  238. $this->writePrintHeaders();
  239. // Write PRINTGRIDLINES
  240. $this->writePrintGridlines();
  241. // Write GRIDSET
  242. $this->writeGridset();
  243. // Calculate column widths
  244. $phpSheet->calculateColumnWidths();
  245. // Column dimensions
  246. if (($defaultWidth = $phpSheet->getDefaultColumnDimension()->getWidth()) < 0) {
  247. $defaultWidth = PHPExcel_Shared_Font::getDefaultColumnWidthByFont($phpSheet->getParent()->getDefaultStyle()->getFont());
  248. }
  249. $columnDimensions = $phpSheet->getColumnDimensions();
  250. $maxCol = $this->lastColumnIndex -1;
  251. for ($i = 0; $i <= $maxCol; ++$i) {
  252. $hidden = 0;
  253. $level = 0;
  254. $xfIndex = 15; // there are 15 cell style Xfs
  255. $width = $defaultWidth;
  256. $columnLetter = PHPExcel_Cell::stringFromColumnIndex($i);
  257. if (isset($columnDimensions[$columnLetter])) {
  258. $columnDimension = $columnDimensions[$columnLetter];
  259. if ($columnDimension->getWidth() >= 0) {
  260. $width = $columnDimension->getWidth();
  261. }
  262. $hidden = $columnDimension->getVisible() ? 0 : 1;
  263. $level = $columnDimension->getOutlineLevel();
  264. $xfIndex = $columnDimension->getXfIndex() + 15; // there are 15 cell style Xfs
  265. }
  266. // Components of columnInfo:
  267. // $firstcol first column on the range
  268. // $lastcol last column on the range
  269. // $width width to set
  270. // $xfIndex The optional cell style Xf index to apply to the columns
  271. // $hidden The optional hidden atribute
  272. // $level The optional outline level
  273. $this->columnInfo[] = array($i, $i, $width, $xfIndex, $hidden, $level);
  274. }
  275. // Write GUTS
  276. $this->writeGuts();
  277. // Write DEFAULTROWHEIGHT
  278. $this->writeDefaultRowHeight();
  279. // Write WSBOOL
  280. $this->writeWsbool();
  281. // Write horizontal and vertical page breaks
  282. $this->writeBreaks();
  283. // Write page header
  284. $this->writeHeader();
  285. // Write page footer
  286. $this->writeFooter();
  287. // Write page horizontal centering
  288. $this->writeHcenter();
  289. // Write page vertical centering
  290. $this->writeVcenter();
  291. // Write left margin
  292. $this->writeMarginLeft();
  293. // Write right margin
  294. $this->writeMarginRight();
  295. // Write top margin
  296. $this->writeMarginTop();
  297. // Write bottom margin
  298. $this->writeMarginBottom();
  299. // Write page setup
  300. $this->writeSetup();
  301. // Write sheet protection
  302. $this->writeProtect();
  303. // Write SCENPROTECT
  304. $this->writeScenProtect();
  305. // Write OBJECTPROTECT
  306. $this->writeObjectProtect();
  307. // Write sheet password
  308. $this->writePassword();
  309. // Write DEFCOLWIDTH record
  310. $this->writeDefcol();
  311. // Write the COLINFO records if they exist
  312. if (!empty($this->columnInfo)) {
  313. $colcount = count($this->columnInfo);
  314. for ($i = 0; $i < $colcount; ++$i) {
  315. $this->writeColinfo($this->columnInfo[$i]);
  316. }
  317. }
  318. $autoFilterRange = $phpSheet->getAutoFilter()->getRange();
  319. if (!empty($autoFilterRange)) {
  320. // Write AUTOFILTERINFO
  321. $this->writeAutoFilterInfo();
  322. }
  323. // Write sheet dimensions
  324. $this->writeDimensions();
  325. // Row dimensions
  326. foreach ($phpSheet->getRowDimensions() as $rowDimension) {
  327. $xfIndex = $rowDimension->getXfIndex() + 15; // there are 15 cellXfs
  328. $this->writeRow($rowDimension->getRowIndex() - 1, $rowDimension->getRowHeight(), $xfIndex, ($rowDimension->getVisible() ? '0' : '1'), $rowDimension->getOutlineLevel());
  329. }
  330. // Write Cells
  331. foreach ($phpSheet->getCellCollection() as $cellID) {
  332. $cell = $phpSheet->getCell($cellID);
  333. $row = $cell->getRow() - 1;
  334. $column = PHPExcel_Cell::columnIndexFromString($cell->getColumn()) - 1;
  335. // Don't break Excel!
  336. // if ($row + 1 > 65536 or $column + 1 > 256) {
  337. if ($row > 65535 || $column > 255) {
  338. break;
  339. }
  340. // Write cell value
  341. $xfIndex = $cell->getXfIndex() + 15; // there are 15 cell style Xfs
  342. $cVal = $cell->getValue();
  343. if ($cVal instanceof PHPExcel_RichText) {
  344. // $this->writeString($row, $column, $cVal->getPlainText(), $xfIndex);
  345. $arrcRun = array();
  346. $str_len = PHPExcel_Shared_String::CountCharacters($cVal->getPlainText(), 'UTF-8');
  347. $str_pos = 0;
  348. $elements = $cVal->getRichTextElements();
  349. foreach ($elements as $element) {
  350. // FONT Index
  351. if ($element instanceof PHPExcel_RichText_Run) {
  352. $str_fontidx = $this->fontHashIndex[$element->getFont()->getHashCode()];
  353. } else {
  354. $str_fontidx = 0;
  355. }
  356. $arrcRun[] = array('strlen' => $str_pos, 'fontidx' => $str_fontidx);
  357. // Position FROM
  358. $str_pos += PHPExcel_Shared_String::CountCharacters($element->getText(), 'UTF-8');
  359. }
  360. $this->writeRichTextString($row, $column, $cVal->getPlainText(), $xfIndex, $arrcRun);
  361. } else {
  362. switch ($cell->getDatatype()) {
  363. case PHPExcel_Cell_DataType::TYPE_STRING:
  364. case PHPExcel_Cell_DataType::TYPE_NULL:
  365. if ($cVal === '' || $cVal === null) {
  366. $this->writeBlank($row, $column, $xfIndex);
  367. } else {
  368. $this->writeString($row, $column, $cVal, $xfIndex);
  369. }
  370. break;
  371. case PHPExcel_Cell_DataType::TYPE_NUMERIC:
  372. $this->writeNumber($row, $column, $cVal, $xfIndex);
  373. break;
  374. case PHPExcel_Cell_DataType::TYPE_FORMULA:
  375. $calculatedValue = $this->_preCalculateFormulas ?
  376. $cell->getCalculatedValue() : null;
  377. $this->writeFormula($row, $column, $cVal, $xfIndex, $calculatedValue);
  378. break;
  379. case PHPExcel_Cell_DataType::TYPE_BOOL:
  380. $this->writeBoolErr($row, $column, $cVal, 0, $xfIndex);
  381. break;
  382. case PHPExcel_Cell_DataType::TYPE_ERROR:
  383. $this->writeBoolErr($row, $column, self::mapErrorCode($cVal), 1, $xfIndex);
  384. break;
  385. }
  386. }
  387. }
  388. // Append
  389. $this->writeMsoDrawing();
  390. // Write WINDOW2 record
  391. $this->writeWindow2();
  392. // Write PLV record
  393. $this->writePageLayoutView();
  394. // Write ZOOM record
  395. $this->writeZoom();
  396. if ($phpSheet->getFreezePane()) {
  397. $this->writePanes();
  398. }
  399. // Write SELECTION record
  400. $this->writeSelection();
  401. // Write MergedCellsTable Record
  402. $this->writeMergedCells();
  403. // Hyperlinks
  404. foreach ($phpSheet->getHyperLinkCollection() as $coordinate => $hyperlink) {
  405. list($column, $row) = PHPExcel_Cell::coordinateFromString($coordinate);
  406. $url = $hyperlink->getUrl();
  407. if (strpos($url, 'sheet://') !== false) {
  408. // internal to current workbook
  409. $url = str_replace('sheet://', 'internal:', $url);
  410. } elseif (preg_match('/^(http:|https:|ftp:|mailto:)/', $url)) {
  411. // URL
  412. // $url = $url;
  413. } else {
  414. // external (local file)
  415. $url = 'external:' . $url;
  416. }
  417. $this->writeUrl($row - 1, PHPExcel_Cell::columnIndexFromString($column) - 1, $url);
  418. }
  419. $this->writeDataValidity();
  420. $this->writeSheetLayout();
  421. // Write SHEETPROTECTION record
  422. $this->writeSheetProtection();
  423. $this->writeRangeProtection();
  424. $arrConditionalStyles = $phpSheet->getConditionalStylesCollection();
  425. if (!empty($arrConditionalStyles)) {
  426. $arrConditional = array();
  427. // @todo CFRule & CFHeader
  428. // Write CFHEADER record
  429. $this->writeCFHeader();
  430. // Write ConditionalFormattingTable records
  431. foreach ($arrConditionalStyles as $cellCoordinate => $conditionalStyles) {
  432. foreach ($conditionalStyles as $conditional) {
  433. if ($conditional->getConditionType() == PHPExcel_Style_Conditional::CONDITION_EXPRESSION
  434. || $conditional->getConditionType() == PHPExcel_Style_Conditional::CONDITION_CELLIS) {
  435. if (!in_array($conditional->getHashCode(), $arrConditional)) {
  436. $arrConditional[] = $conditional->getHashCode();
  437. // Write CFRULE record
  438. $this->writeCFRule($conditional);
  439. }
  440. }
  441. }
  442. }
  443. }
  444. $this->storeEof();
  445. }
  446. /**
  447. * Write a cell range address in BIFF8
  448. * always fixed range
  449. * See section 2.5.14 in OpenOffice.org's Documentation of the Microsoft Excel File Format
  450. *
  451. * @param string $range E.g. 'A1' or 'A1:B6'
  452. * @return string Binary data
  453. */
  454. private function writeBIFF8CellRangeAddressFixed($range = 'A1')
  455. {
  456. $explodes = explode(':', $range);
  457. // extract first cell, e.g. 'A1'
  458. $firstCell = $explodes[0];
  459. // extract last cell, e.g. 'B6'
  460. if (count($explodes) == 1) {
  461. $lastCell = $firstCell;
  462. } else {
  463. $lastCell = $explodes[1];
  464. }
  465. $firstCellCoordinates = PHPExcel_Cell::coordinateFromString($firstCell); // e.g. array(0, 1)
  466. $lastCellCoordinates = PHPExcel_Cell::coordinateFromString($lastCell); // e.g. array(1, 6)
  467. return pack('vvvv', $firstCellCoordinates[1] - 1, $lastCellCoordinates[1] - 1, PHPExcel_Cell::columnIndexFromString($firstCellCoordinates[0]) - 1, PHPExcel_Cell::columnIndexFromString($lastCellCoordinates[0]) - 1);
  468. }
  469. /**
  470. * Retrieves data from memory in one chunk, or from disk in $buffer
  471. * sized chunks.
  472. *
  473. * @return string The data
  474. */
  475. public function getData()
  476. {
  477. $buffer = 4096;
  478. // Return data stored in memory
  479. if (isset($this->_data)) {
  480. $tmp = $this->_data;
  481. unset($this->_data);
  482. return $tmp;
  483. }
  484. // No data to return
  485. return false;
  486. }
  487. /**
  488. * Set the option to print the row and column headers on the printed page.
  489. *
  490. * @access public
  491. * @param integer $print Whether to print the headers or not. Defaults to 1 (print).
  492. */
  493. public function printRowColHeaders($print = 1)
  494. {
  495. $this->_print_headers = $print;
  496. }
  497. /**
  498. * This method sets the properties for outlining and grouping. The defaults
  499. * correspond to Excel's defaults.
  500. *
  501. * @param bool $visible
  502. * @param bool $symbols_below
  503. * @param bool $symbols_right
  504. * @param bool $auto_style
  505. */
  506. public function setOutline($visible = true, $symbols_below = true, $symbols_right = true, $auto_style = false)
  507. {
  508. $this->outlineOn = $visible;
  509. $this->outlineBelow = $symbols_below;
  510. $this->outlineRight = $symbols_right;
  511. $this->outlineStyle = $auto_style;
  512. // Ensure this is a boolean vale for Window2
  513. if ($this->outlineOn) {
  514. $this->outlineOn = 1;
  515. }
  516. }
  517. /**
  518. * Write a double to the specified row and column (zero indexed).
  519. * An integer can be written as a double. Excel will display an
  520. * integer. $format is optional.
  521. *
  522. * Returns 0 : normal termination
  523. * -2 : row or column out of range
  524. *
  525. * @param integer $row Zero indexed row
  526. * @param integer $col Zero indexed column
  527. * @param float $num The number to write
  528. * @param mixed $xfIndex The optional XF format
  529. * @return integer
  530. */
  531. private function writeNumber($row, $col, $num, $xfIndex)
  532. {
  533. $record = 0x0203; // Record identifier
  534. $length = 0x000E; // Number of bytes to follow
  535. $header = pack("vv", $record, $length);
  536. $data = pack("vvv", $row, $col, $xfIndex);
  537. $xl_double = pack("d", $num);
  538. if (self::getByteOrder()) { // if it's Big Endian
  539. $xl_double = strrev($xl_double);
  540. }
  541. $this->append($header.$data.$xl_double);
  542. return(0);
  543. }
  544. /**
  545. * Write a LABELSST record or a LABEL record. Which one depends on BIFF version
  546. *
  547. * @param int $row Row index (0-based)
  548. * @param int $col Column index (0-based)
  549. * @param string $str The string
  550. * @param int $xfIndex Index to XF record
  551. */
  552. private function writeString($row, $col, $str, $xfIndex)
  553. {
  554. $this->writeLabelSst($row, $col, $str, $xfIndex);
  555. }
  556. /**
  557. * Write a LABELSST record or a LABEL record. Which one depends on BIFF version
  558. * It differs from writeString by the writing of rich text strings.
  559. * @param int $row Row index (0-based)
  560. * @param int $col Column index (0-based)
  561. * @param string $str The string
  562. * @param mixed $xfIndex The XF format index for the cell
  563. * @param array $arrcRun Index to Font record and characters beginning
  564. */
  565. private function writeRichTextString($row, $col, $str, $xfIndex, $arrcRun)
  566. {
  567. $record = 0x00FD; // Record identifier
  568. $length = 0x000A; // Bytes to follow
  569. $str = PHPExcel_Shared_String::UTF8toBIFF8UnicodeShort($str, $arrcRun);
  570. /* check if string is already present */
  571. if (!isset($this->stringTable[$str])) {
  572. $this->stringTable[$str] = $this->stringUnique++;
  573. }
  574. $this->stringTotal++;
  575. $header = pack('vv', $record, $length);
  576. $data = pack('vvvV', $row, $col, $xfIndex, $this->stringTable[$str]);
  577. $this->append($header.$data);
  578. }
  579. /**
  580. * Write a string to the specified row and column (zero indexed).
  581. * NOTE: there is an Excel 5 defined limit of 255 characters.
  582. * $format is optional.
  583. * Returns 0 : normal termination
  584. * -2 : row or column out of range
  585. * -3 : long string truncated to 255 chars
  586. *
  587. * @access public
  588. * @param integer $row Zero indexed row
  589. * @param integer $col Zero indexed column
  590. * @param string $str The string to write
  591. * @param mixed $xfIndex The XF format index for the cell
  592. * @return integer
  593. */
  594. private function writeLabel($row, $col, $str, $xfIndex)
  595. {
  596. $strlen = strlen($str);
  597. $record = 0x0204; // Record identifier
  598. $length = 0x0008 + $strlen; // Bytes to follow
  599. $str_error = 0;
  600. if ($strlen > $this->xlsStringMaxLength) { // LABEL must be < 255 chars
  601. $str = substr($str, 0, $this->xlsStringMaxLength);
  602. $length = 0x0008 + $this->xlsStringMaxLength;
  603. $strlen = $this->xlsStringMaxLength;
  604. $str_error = -3;
  605. }
  606. $header = pack("vv", $record, $length);
  607. $data = pack("vvvv", $row, $col, $xfIndex, $strlen);
  608. $this->append($header . $data . $str);
  609. return($str_error);
  610. }
  611. /**
  612. * Write a string to the specified row and column (zero indexed).
  613. * This is the BIFF8 version (no 255 chars limit).
  614. * $format is optional.
  615. * Returns 0 : normal termination
  616. * -2 : row or column out of range
  617. * -3 : long string truncated to 255 chars
  618. *
  619. * @access public
  620. * @param integer $row Zero indexed row
  621. * @param integer $col Zero indexed column
  622. * @param string $str The string to write
  623. * @param mixed $xfIndex The XF format index for the cell
  624. * @return integer
  625. */
  626. private function writeLabelSst($row, $col, $str, $xfIndex)
  627. {
  628. $record = 0x00FD; // Record identifier
  629. $length = 0x000A; // Bytes to follow
  630. $str = PHPExcel_Shared_String::UTF8toBIFF8UnicodeLong($str);
  631. /* check if string is already present */
  632. if (!isset($this->stringTable[$str])) {
  633. $this->stringTable[$str] = $this->stringUnique++;
  634. }
  635. $this->stringTotal++;
  636. $header = pack('vv', $record, $length);
  637. $data = pack('vvvV', $row, $col, $xfIndex, $this->stringTable[$str]);
  638. $this->append($header.$data);
  639. }
  640. /**
  641. * Writes a note associated with the cell given by the row and column.
  642. * NOTE records don't have a length limit.
  643. *
  644. * @param integer $row Zero indexed row
  645. * @param integer $col Zero indexed column
  646. * @param string $note The note to write
  647. */
  648. private function writeNote($row, $col, $note)
  649. {
  650. $note_length = strlen($note);
  651. $record = 0x001C; // Record identifier
  652. $max_length = 2048; // Maximun length for a NOTE record
  653. // Length for this record is no more than 2048 + 6
  654. $length = 0x0006 + min($note_length, 2048);
  655. $header = pack("vv", $record, $length);
  656. $data = pack("vvv", $row, $col, $note_length);
  657. $this->append($header . $data . substr($note, 0, 2048));
  658. for ($i = $max_length; $i < $note_length; $i += $max_length) {
  659. $chunk = substr($note, $i, $max_length);
  660. $length = 0x0006 + strlen($chunk);
  661. $header = pack("vv", $record, $length);
  662. $data = pack("vvv", -1, 0, strlen($chunk));
  663. $this->append($header.$data.$chunk);
  664. }
  665. return(0);
  666. }
  667. /**
  668. * Write a blank cell to the specified row and column (zero indexed).
  669. * A blank cell is used to specify formatting without adding a string
  670. * or a number.
  671. *
  672. * A blank cell without a format serves no purpose. Therefore, we don't write
  673. * a BLANK record unless a format is specified.
  674. *
  675. * Returns 0 : normal termination (including no format)
  676. * -1 : insufficient number of arguments
  677. * -2 : row or column out of range
  678. *
  679. * @param integer $row Zero indexed row
  680. * @param integer $col Zero indexed column
  681. * @param mixed $xfIndex The XF format index
  682. */
  683. public function writeBlank($row, $col, $xfIndex)
  684. {
  685. $record = 0x0201; // Record identifier
  686. $length = 0x0006; // Number of bytes to follow
  687. $header = pack("vv", $record, $length);
  688. $data = pack("vvv", $row, $col, $xfIndex);
  689. $this->append($header . $data);
  690. return 0;
  691. }
  692. /**
  693. * Write a boolean or an error type to the specified row and column (zero indexed)
  694. *
  695. * @param int $row Row index (0-based)
  696. * @param int $col Column index (0-based)
  697. * @param int $value
  698. * @param boolean $isError Error or Boolean?
  699. * @param int $xfIndex
  700. */
  701. private function writeBoolErr($row, $col, $value, $isError, $xfIndex)
  702. {
  703. $record = 0x0205;
  704. $length = 8;
  705. $header = pack("vv", $record, $length);
  706. $data = pack("vvvCC", $row, $col, $xfIndex, $value, $isError);
  707. $this->append($header . $data);
  708. return 0;
  709. }
  710. /**
  711. * Write a formula to the specified row and column (zero indexed).
  712. * The textual representation of the formula is passed to the parser in
  713. * Parser.php which returns a packed binary string.
  714. *
  715. * Returns 0 : normal termination
  716. * -1 : formula errors (bad formula)
  717. * -2 : row or column out of range
  718. *
  719. * @param integer $row Zero indexed row
  720. * @param integer $col Zero indexed column
  721. * @param string $formula The formula text string
  722. * @param mixed $xfIndex The XF format index
  723. * @param mixed $calculatedValue Calculated value
  724. * @return integer
  725. */
  726. private function writeFormula($row, $col, $formula, $xfIndex, $calculatedValue)
  727. {
  728. $record = 0x0006; // Record identifier
  729. // Initialize possible additional value for STRING record that should be written after the FORMULA record?
  730. $stringValue = null;
  731. // calculated value
  732. if (isset($calculatedValue)) {
  733. // Since we can't yet get the data type of the calculated value,
  734. // we use best effort to determine data type
  735. if (is_bool($calculatedValue)) {
  736. // Boolean value
  737. $num = pack('CCCvCv', 0x01, 0x00, (int)$calculatedValue, 0x00, 0x00, 0xFFFF);
  738. } elseif (is_int($calculatedValue) || is_float($calculatedValue)) {
  739. // Numeric value
  740. $num = pack('d', $calculatedValue);
  741. } elseif (is_string($calculatedValue)) {
  742. if (array_key_exists($calculatedValue, PHPExcel_Cell_DataType::getErrorCodes())) {
  743. // Error value
  744. $num = pack('CCCvCv', 0x02, 0x00, self::mapErrorCode($calculatedValue), 0x00, 0x00, 0xFFFF);
  745. } elseif ($calculatedValue === '') {
  746. // Empty string (and BIFF8)
  747. $num = pack('CCCvCv', 0x03, 0x00, 0x00, 0x00, 0x00, 0xFFFF);
  748. } else {
  749. // Non-empty string value (or empty string BIFF5)
  750. $stringValue = $calculatedValue;
  751. $num = pack('CCCvCv', 0x00, 0x00, 0x00, 0x00, 0x00, 0xFFFF);
  752. }
  753. } else {
  754. // We are really not supposed to reach here
  755. $num = pack('d', 0x00);
  756. }
  757. } else {
  758. $num = pack('d', 0x00);
  759. }
  760. $grbit = 0x03; // Option flags
  761. $unknown = 0x0000; // Must be zero
  762. // Strip the '=' or '@' sign at the beginning of the formula string
  763. if ($formula{0} == '=') {
  764. $formula = substr($formula, 1);
  765. } else {
  766. // Error handling
  767. $this->writeString($row, $col, 'Unrecognised character for formula');
  768. return -1;
  769. }
  770. // Parse the formula using the parser in Parser.php
  771. try {
  772. $error = $this->parser->parse($formula);
  773. $formula = $this->parser->toReversePolish();
  774. $formlen = strlen($formula); // Length of the binary string
  775. $length = 0x16 + $formlen; // Length of the record data
  776. $header = pack("vv", $record, $length);
  777. $data = pack("vvv", $row, $col, $xfIndex)
  778. . $num
  779. . pack("vVv", $grbit, $unknown, $formlen);
  780. $this->append($header . $data . $formula);
  781. // Append also a STRING record if necessary
  782. if ($stringValue !== null) {
  783. $this->writeStringRecord($stringValue);
  784. }
  785. return 0;
  786. } catch (PHPExcel_Exception $e) {
  787. // do nothing
  788. }
  789. }
  790. /**
  791. * Write a STRING record. This
  792. *
  793. * @param string $stringValue
  794. */
  795. private function writeStringRecord($stringValue)
  796. {
  797. $record = 0x0207; // Record identifier
  798. $data = PHPExcel_Shared_String::UTF8toBIFF8UnicodeLong($stringValue);
  799. $length = strlen($data);
  800. $header = pack('vv', $record, $length);
  801. $this->append($header . $data);
  802. }
  803. /**
  804. * Write a hyperlink.
  805. * This is comprised of two elements: the visible label and
  806. * the invisible link. The visible label is the same as the link unless an
  807. * alternative string is specified. The label is written using the
  808. * writeString() method. Therefore the 255 characters string limit applies.
  809. * $string and $format are optional.
  810. *
  811. * The hyperlink can be to a http, ftp, mail, internal sheet (not yet), or external
  812. * directory url.
  813. *
  814. * Returns 0 : normal termination
  815. * -2 : row or column out of range
  816. * -3 : long string truncated to 255 chars
  817. *
  818. * @param integer $row Row
  819. * @param integer $col Column
  820. * @param string $url URL string
  821. * @return integer
  822. */
  823. private function writeUrl($row, $col, $url)
  824. {
  825. // Add start row and col to arg list
  826. return($this->writeUrlRange($row, $col, $row, $col, $url));
  827. }
  828. /**
  829. * This is the more general form of writeUrl(). It allows a hyperlink to be
  830. * written to a range of cells. This function also decides the type of hyperlink
  831. * to be written. These are either, Web (http, ftp, mailto), Internal
  832. * (Sheet1!A1) or external ('c:\temp\foo.xls#Sheet1!A1').
  833. *
  834. * @access private
  835. * @see writeUrl()
  836. * @param integer $row1 Start row
  837. * @param integer $col1 Start column
  838. * @param integer $row2 End row
  839. * @param integer $col2 End column
  840. * @param string $url URL string
  841. * @return integer
  842. */
  843. public function writeUrlRange($row1, $col1, $row2, $col2, $url)
  844. {
  845. // Check for internal/external sheet links or default to web link
  846. if (preg_match('[^internal:]', $url)) {
  847. return($this->writeUrlInternal($row1, $col1, $row2, $col2, $url));
  848. }
  849. if (preg_match('[^external:]', $url)) {
  850. return($this->writeUrlExternal($row1, $col1, $row2, $col2, $url));
  851. }
  852. return($this->writeUrlWeb($row1, $col1, $row2, $col2, $url));
  853. }
  854. /**
  855. * Used to write http, ftp and mailto hyperlinks.
  856. * The link type ($options) is 0x03 is the same as absolute dir ref without
  857. * sheet. However it is differentiated by the $unknown2 data stream.
  858. *
  859. * @access private
  860. * @see writeUrl()
  861. * @param integer $row1 Start row
  862. * @param integer $col1 Start column
  863. * @param integer $row2 End row
  864. * @param integer $col2 End column
  865. * @param string $url URL string
  866. * @return integer
  867. */
  868. public function writeUrlWeb($row1, $col1, $row2, $col2, $url)
  869. {
  870. $record = 0x01B8; // Record identifier
  871. $length = 0x00000; // Bytes to follow
  872. // Pack the undocumented parts of the hyperlink stream
  873. $unknown1 = pack("H*", "D0C9EA79F9BACE118C8200AA004BA90B02000000");
  874. $unknown2 = pack("H*", "E0C9EA79F9BACE118C8200AA004BA90B");
  875. // Pack the option flags
  876. $options = pack("V", 0x03);
  877. // Convert URL to a null terminated wchar string
  878. $url = join("\0", preg_split("''", $url, -1, PREG_SPLIT_NO_EMPTY));
  879. $url = $url . "\0\0\0";
  880. // Pack the length of the URL
  881. $url_len = pack("V", strlen($url));
  882. // Calculate the data length
  883. $length = 0x34 + strlen($url);
  884. // Pack the header data
  885. $header = pack("vv", $record, $length);
  886. $data = pack("vvvv", $row1, $row2, $col1, $col2);
  887. // Write the packed data
  888. $this->append($header . $data .
  889. $unknown1 . $options .
  890. $unknown2 . $url_len . $url);
  891. return 0;
  892. }
  893. /**
  894. * Used to write internal reference hyperlinks such as "Sheet1!A1".
  895. *
  896. * @access private
  897. * @see writeUrl()
  898. * @param integer $row1 Start row
  899. * @param integer $col1 Start column
  900. * @param integer $row2 End row
  901. * @param integer $col2 End column
  902. * @param string $url URL string
  903. * @return integer
  904. */
  905. public function writeUrlInternal($row1, $col1, $row2, $col2, $url)
  906. {
  907. $record = 0x01B8; // Record identifier
  908. $length = 0x00000; // Bytes to follow
  909. // Strip URL type
  910. $url = preg_replace('/^internal:/', '', $url);
  911. // Pack the undocumented parts of the hyperlink stream
  912. $unknown1 = pack("H*", "D0C9EA79F9BACE118C8200AA004BA90B02000000");
  913. // Pack the option flags
  914. $options = pack("V", 0x08);
  915. // Convert the URL type and to a null terminated wchar string
  916. $url .= "\0";
  917. // character count
  918. $url_len = PHPExcel_Shared_String::CountCharacters($url);
  919. $url_len = pack('V', $url_len);
  920. $url = PHPExcel_Shared_String::ConvertEncoding($url, 'UTF-16LE', 'UTF-8');
  921. // Calculate the data length
  922. $length = 0x24 + strlen($url);
  923. // Pack the header data
  924. $header = pack("vv", $record, $length);
  925. $data = pack("vvvv", $row1, $row2, $col1, $col2);
  926. // Write the packed data
  927. $this->append($header . $data .
  928. $unknown1 . $options .
  929. $url_len . $url);
  930. return 0;
  931. }
  932. /**
  933. * Write links to external directory names such as 'c:\foo.xls',
  934. * c:\foo.xls#Sheet1!A1', '../../foo.xls'. and '../../foo.xls#Sheet1!A1'.
  935. *
  936. * Note: Excel writes some relative links with the $dir_long string. We ignore
  937. * these cases for the sake of simpler code.
  938. *
  939. * @access private
  940. * @see writeUrl()
  941. * @param integer $row1 Start row
  942. * @param integer $col1 Start column
  943. * @param integer $row2 End row
  944. * @param integer $col2 End column
  945. * @param string $url URL string
  946. * @return integer
  947. */
  948. public function writeUrlExternal($row1, $col1, $row2, $col2, $url)
  949. {
  950. // Network drives are different. We will handle them separately
  951. // MS/Novell network drives and shares start with \\
  952. if (preg_match('[^external:\\\\]', $url)) {
  953. return; //($this->writeUrlExternal_net($row1, $col1, $row2, $col2, $url, $str, $format));
  954. }
  955. $record = 0x01B8; // Record identifier
  956. $length = 0x00000; // Bytes to follow
  957. // Strip URL type and change Unix dir separator to Dos style (if needed)
  958. //
  959. $url = preg_replace('/^external:/', '', $url);
  960. $url = preg_replace('/\//', "\\", $url);
  961. // Determine if the link is relative or absolute:
  962. // relative if link contains no dir separator, "somefile.xls"
  963. // relative if link starts with up-dir, "..\..\somefile.xls"
  964. // otherwise, absolute
  965. $absolute = 0x00; // relative path
  966. if (preg_match('/^[A-Z]:/', $url)) {
  967. $absolute = 0x02; // absolute path on Windows, e.g. C:\...
  968. }
  969. $link_type = 0x01 | $absolute;
  970. // Determine if the link contains a sheet reference and change some of the
  971. // parameters accordingly.
  972. // Split the dir name and sheet name (if it exists)
  973. $dir_long = $url;
  974. if (preg_match("/\#/", $url)) {
  975. $link_type |= 0x08;
  976. }
  977. // Pack the link type
  978. $link_type = pack("V", $link_type);
  979. // Calculate the up-level dir count e.g.. (..\..\..\ == 3)
  980. $up_count = preg_match_all("/\.\.\\\/", $dir_long, $useless);
  981. $up_count = pack("v", $up_count);
  982. // Store the short dos dir name (null terminated)
  983. $dir_short = preg_replace("/\.\.\\\/", '', $dir_long) . "\0";
  984. // Store the long dir name as a wchar string (non-null terminated)
  985. $dir_long = $dir_long . "\0";
  986. // Pack the lengths of the dir strings
  987. $dir_short_len = pack("V", strlen($dir_short));
  988. $dir_long_len = pack("V", strlen($dir_long));
  989. $stream_len = pack("V", 0); //strlen($dir_long) + 0x06);
  990. // Pack the undocumented parts of the hyperlink stream
  991. $unknown1 = pack("H*", 'D0C9EA79F9BACE118C8200AA004BA90B02000000');
  992. $unknown2 = pack("H*", '0303000000000000C000000000000046');
  993. $unknown3 = pack("H*", 'FFFFADDE000000000000000000000000000000000000000');
  994. $unknown4 = pack("v", 0x03);
  995. // Pack the main data stream
  996. $data = pack("vvvv", $row1, $row2, $col1, $col2) .
  997. $unknown1 .
  998. $link_type .
  999. $unknown2 .
  1000. $up_count .
  1001. $dir_short_len.
  1002. $dir_short .
  1003. $unknown3 .
  1004. $stream_len ;/*.
  1005. $dir_long_len .
  1006. $unknown4 .
  1007. $dir_long .
  1008. $sheet_len .
  1009. $sheet ;*/
  1010. // Pack the header data
  1011. $length = strlen($data);
  1012. $header = pack("vv", $record, $length);
  1013. // Write the packed data
  1014. $this->append($header. $data);
  1015. return 0;
  1016. }
  1017. /**
  1018. * This method is used to set the height and format for a row.
  1019. *
  1020. * @param integer $row The row to set
  1021. * @param integer $height Height we are giving to the row.
  1022. * Use null to set XF without setting height
  1023. * @param integer $xfIndex The optional cell style Xf index to apply to the columns
  1024. * @param bool $hidden The optional hidden attribute
  1025. * @param integer $level The optional outline level for row, in range [0,7]
  1026. */
  1027. private function writeRow($row, $height, $xfIndex, $hidden = false, $level = 0)
  1028. {
  1029. $record = 0x0208; // Record identifier
  1030. $length = 0x0010; // Number of bytes to follow
  1031. $colMic = 0x0000; // First defined column
  1032. $colMac = 0x0000; // Last defined column
  1033. $irwMac = 0x0000; // Used by Excel to optimise loading
  1034. $reserved = 0x0000; // Reserved
  1035. $grbit = 0x0000; // Option flags
  1036. $ixfe = $xfIndex;
  1037. if ($height < 0) {
  1038. $height = null;
  1039. }
  1040. // Use writeRow($row, null, $XF) to set XF format without setting height
  1041. if ($height != null) {
  1042. $miyRw = $height * 20; // row height
  1043. } else {
  1044. $miyRw = 0xff; // default row height is 256
  1045. }
  1046. // Set the options flags. fUnsynced is used to show that the font and row
  1047. // heights are not compatible. This is usually the case for WriteExcel.
  1048. // The collapsed flag 0x10 doesn't seem to be used to indicate that a row
  1049. // is collapsed. Instead it is used to indicate that the previous row is
  1050. // collapsed. The zero height flag, 0x20, is used to collapse a row.
  1051. $grbit |= $level;
  1052. if ($hidden) {
  1053. $grbit |= 0x0030;
  1054. }
  1055. if ($height !== null) {
  1056. $grbit |= 0x0040; // fUnsynced
  1057. }
  1058. if ($xfIndex !== 0xF) {
  1059. $grbit |= 0x0080;
  1060. }
  1061. $grbit |= 0x0100;
  1062. $header = pack("vv", $record, $length);
  1063. $data = pack("vvvvvvvv", $row, $colMic, $colMac, $miyRw, $irwMac, $reserved, $grbit, $ixfe);
  1064. $this->append($header.$data);
  1065. }
  1066. /**
  1067. * Writes Excel DIMENSIONS to define the area in which there is data.
  1068. */
  1069. private function writeDimensions()
  1070. {
  1071. $record = 0x0200; // Record identifier
  1072. $length = 0x000E;
  1073. $data = pack('VVvvv', $this->firstRowIndex, $this->lastRowIndex + 1, $this->firstColumnIndex, $this->lastColumnIndex + 1, 0x0000); // reserved
  1074. $header = pack("vv", $record, $length);
  1075. $this->append($header.$data);
  1076. }
  1077. /**
  1078. * Write BIFF record Window2.
  1079. */
  1080. private function writeWindow2()
  1081. {
  1082. $record = 0x023E; // Record identifier
  1083. $length = 0x0012;
  1084. $grbit = 0x00B6; // Option flags
  1085. $rwTop = 0x0000; // Top row visible in window
  1086. $colLeft = 0x0000; // Leftmost column visible in window
  1087. // The options flags that comprise $grbit
  1088. $fDspFmla = 0; // 0 - bit
  1089. $fDspGrid = $this->phpSheet->getShowGridlines() ? 1 : 0; // 1
  1090. $fDspRwCol = $this->phpSheet->getShowRowColHeaders() ? 1 : 0; // 2
  1091. $fFrozen = $this->phpSheet->getFreezePane() ? 1 : 0; // 3
  1092. $fDspZeros = 1; // 4
  1093. $fDefaultHdr = 1; // 5
  1094. $fArabic = $this->phpSheet->getRightToLeft() ? 1 : 0; // 6
  1095. $fDspGuts = $this->outlineOn; // 7
  1096. $fFrozenNoSplit = 0; // 0 - bit
  1097. // no support in PHPExcel for selected sheet, therefore sheet is only selected if it is the active sheet
  1098. $fSelected = ($this->phpSheet === $this->phpSheet->getParent()->getActiveSheet()) ? 1 : 0;
  1099. $fPaged = 1; // 2
  1100. $fPageBreakPreview = $this->phpSheet->getSheetView()->getView() === PHPExcel_Worksheet_SheetView::SHEETVIEW_PAGE_BREAK_PREVIEW;
  1101. $grbit = $fDspFmla;
  1102. $grbit |= $fDspGrid << 1;
  1103. $grbit |= $fDspRwCol << 2;
  1104. $grbit |= $fFrozen << 3;
  1105. $grbit |= $fDspZeros << 4;
  1106. $grbit |= $fDefaultHdr << 5;
  1107. $grbit |= $fArabic << 6;
  1108. $grbit |= $fDspGuts << 7;
  1109. $grbit |= $fFrozenNoSplit << 8;
  1110. $grbit |= $fSelected << 9;
  1111. $grbit |= $fPaged << 10;
  1112. $grbit |= $fPageBreakPreview << 11;
  1113. $header = pack("vv", $record, $length);
  1114. $data = pack("vvv", $grbit, $rwTop, $colLeft);
  1115. // FIXME !!!
  1116. $rgbHdr = 0x0040; // Row/column heading and gridline color index
  1117. $zoom_factor_page_break = ($fPageBreakPreview ? $this->phpSheet->getSheetView()->getZoomScale() : 0x0000);
  1118. $zoom_factor_normal = $this->phpSheet->getSheetView()->getZoomScaleNormal();
  1119. $data .= pack("vvvvV", $rgbHdr, 0x0000, $zoom_factor_page_break, $zoom_factor_normal, 0x00000000);
  1120. $this->append($header.$data);
  1121. }
  1122. /**
  1123. * Write BIFF record DEFAULTROWHEIGHT.
  1124. */
  1125. private function writeDefaultRowHeight()
  1126. {
  1127. $defaultRowHeight = $this->phpSheet->getDefaultRowDimension()->getRowHeight();
  1128. if ($defaultRowHeight < 0) {
  1129. return;
  1130. }
  1131. // convert to twips
  1132. $defaultRowHeight = (int) 20 * $defaultRowHeight;
  1133. $record = 0x0225; // Record identifier
  1134. $length = 0x0004; // Number of bytes to follow
  1135. $header = pack("vv", $record, $length);
  1136. $data = pack("vv", 1, $defaultRowHeight);
  1137. $this->append($header . $data);
  1138. }
  1139. /**
  1140. * Write BIFF record DEFCOLWIDTH if COLINFO records are in use.
  1141. */
  1142. private function writeDefcol()
  1143. {
  1144. $defaultColWidth = 8;
  1145. $record = 0x0055; // Record identifier
  1146. $length = 0x0002; // Number of bytes to follow
  1147. $header = pack("vv", $record, $length);
  1148. $data = pack("v", $defaultColWidth);
  1149. $this->append($header . $data);
  1150. }
  1151. /**
  1152. * Write BIFF record COLINFO to define column widths
  1153. *
  1154. * Note: The SDK says the record length is 0x0B but Excel writes a 0x0C
  1155. * length record.
  1156. *
  1157. * @param array $col_array This is the only parameter received and is composed of the following:
  1158. * 0 => First formatted column,
  1159. * 1 => Last formatted column,
  1160. * 2 => Col width (8.43 is Excel default),
  1161. * 3 => The optional XF format of the column,
  1162. * 4 => Option flags.
  1163. * 5 => Optional outline level
  1164. */
  1165. private function writeColinfo($col_array)
  1166. {
  1167. if (isset($col_array[0])) {
  1168. $colFirst = $col_array[0];
  1169. }
  1170. if (isset($col_array[1])) {
  1171. $colLast = $col_array[1];
  1172. }
  1173. if (isset($col_array[2])) {
  1174. $coldx = $col_array[2];
  1175. } else {
  1176. $coldx = 8.43;
  1177. }
  1178. if (isset($col_array[3])) {
  1179. $xfIndex = $col_array[3];
  1180. } else {
  1181. $xfIndex = 15;
  1182. }
  1183. if (isset($col_array[4])) {
  1184. $grbit = $col_array[4];
  1185. } else {
  1186. $grbit = 0;
  1187. }
  1188. if (isset($col_array[5])) {
  1189. $level = $col_array[5];
  1190. } else {
  1191. $level = 0;
  1192. }
  1193. $record = 0x007D; // Record identifier
  1194. $length = 0x000C; // Number of bytes to follow
  1195. $coldx *= 256; // Convert to units of 1/256 of a char
  1196. $ixfe = $xfIndex;
  1197. $reserved = 0x0000; // Reserved
  1198. $level = max(0, min($level, 7));
  1199. $grbit |= $level << 8;
  1200. $header = pack("vv", $record, $length);
  1201. $data = pack("vvvvvv", $colFirst, $colLast, $coldx, $ixfe, $grbit, $reserved);
  1202. $this->append($header.$data);

Large files files are truncated, but you can click here to view the full file