📄 worksheet.php
字号:
// Convert a cell reference: 'A1' or 'AD2000' if (preg_match("/\$?([A-I]?[A-Z]\$?\d+)/", $cell)) { list($row1, $col1) = $this->_cellToRowcol($match[1]); return(array($row1, $col1)); } // TODO use real error codes $this->raiseError("Unknown cell reference $cell", 0, PEAR_ERROR_DIE); } /** * Convert an Excel cell reference in A1 notation to a zero based row and column * reference; converts C1 to (0, 2). * * @access private * @param string $cell The cell reference. * @return array containing (row, column) */ function _cellToRowcol($cell) { preg_match("/\$?([A-I]?[A-Z])\$?(\d+)/",$cell,$match); $col = $match[1]; $row = $match[2]; // Convert base26 column string to number $chars = split('', $col); $expn = 0; $col = 0; while ($chars) { $char = array_pop($chars); // LS char first $col += (ord($char) -ord('A') +1) * pow(26,$expn); $expn++; } // Convert 1-index to zero-index $row--; $col--; return(array($row, $col)); } /** * Based on the algorithm provided by Daniel Rentz of OpenOffice. * * @access private * @param string $plaintext The password to be encoded in plaintext. * @return string The encoded password */ function _encodePassword($plaintext) { $password = 0x0000; $i = 1; // char position // split the plain text password in its component characters $chars = preg_split('//', $plaintext, -1, PREG_SPLIT_NO_EMPTY); foreach ($chars as $char) { $value = ord($char) << $i; // shifted ASCII value $rotated_bits = $value >> 15; // rotated bits beyond bit 15 $value &= 0x7fff; // first 15 bits $password ^= ($value | $rotated_bits); $i++; } $password ^= strlen($plaintext); $password ^= 0xCE4B; return($password); } /** * This method sets the properties for outlining and grouping. The defaults * correspond to Excel's defaults. * * @param bool $visible * @param bool $symbols_below * @param bool $symbols_right * @param bool $auto_style */ function setOutline($visible = true, $symbols_below = true, $symbols_right = true, $auto_style = false) { $this->_outline_on = $visible; $this->_outline_below = $symbols_below; $this->_outline_right = $symbols_right; $this->_outline_style = $auto_style; // Ensure this is a boolean vale for Window2 if ($this->_outline_on) { $this->_outline_on = 1; } } /****************************************************************************** ******************************************************************************* * * BIFF RECORDS */ /** * Write a double to the specified row and column (zero indexed). * An integer can be written as a double. Excel will display an * integer. $format is optional. * * Returns 0 : normal termination * -2 : row or column out of range * * @access public * @param integer $row Zero indexed row * @param integer $col Zero indexed column * @param float $num The number to write * @param mixed $format The optional XF format * @return integer */ function writeNumber($row, $col, $num, $format = null) { $record = 0x0203; // Record identifier $length = 0x000E; // Number of bytes to follow $xf = $this->_XF($format); // The cell format // Check that row and col are valid and store max and min values if ($row >= $this->_xls_rowmax) { return(-2); } if ($col >= $this->_xls_colmax) { return(-2); } if ($row < $this->_dim_rowmin) { $this->_dim_rowmin = $row; } if ($row > $this->_dim_rowmax) { $this->_dim_rowmax = $row; } if ($col < $this->_dim_colmin) { $this->_dim_colmin = $col; } if ($col > $this->_dim_colmax) { $this->_dim_colmax = $col; } $header = pack("vv", $record, $length); $data = pack("vvv", $row, $col, $xf); $xl_double = pack("d", $num); if ($this->_byte_order) { // if it's Big Endian $xl_double = strrev($xl_double); } $this->_append($header.$data.$xl_double); return(0); } /** * Write a string to the specified row and column (zero indexed). * NOTE: there is an Excel 5 defined limit of 255 characters. * $format is optional. * Returns 0 : normal termination * -2 : row or column out of range * -3 : long string truncated to 255 chars * * @access public * @param integer $row Zero indexed row * @param integer $col Zero indexed column * @param string $str The string to write * @param mixed $format The XF format for the cell * @return integer */ function writeString($row, $col, $str, $format = null) { if ($this->_BIFF_version == 0x0600) { return $this->writeStringBIFF8($row, $col, $str, $format); } $strlen = strlen($str); $record = 0x0204; // Record identifier $length = 0x0008 + $strlen; // Bytes to follow $xf = $this->_XF($format); // The cell format $str_error = 0; // Check that row and col are valid and store max and min values if ($row >= $this->_xls_rowmax) { return(-2); } if ($col >= $this->_xls_colmax) { return(-2); } if ($row < $this->_dim_rowmin) { $this->_dim_rowmin = $row; } if ($row > $this->_dim_rowmax) { $this->_dim_rowmax = $row; } if ($col < $this->_dim_colmin) { $this->_dim_colmin = $col; } if ($col > $this->_dim_colmax) { $this->_dim_colmax = $col; } if ($strlen > $this->_xls_strmax) { // LABEL must be < 255 chars $str = substr($str, 0, $this->_xls_strmax); $length = 0x0008 + $this->_xls_strmax; $strlen = $this->_xls_strmax; $str_error = -3; } $header = pack("vv", $record, $length); $data = pack("vvvv", $row, $col, $xf, $strlen); $this->_append($header . $data . $str); return($str_error); } /** * Sets Input Encoding for writing strings * * @access public * @param string $encoding The encoding. Ex: 'UTF-16LE', 'utf-8', 'ISO-859-7' */ function setInputEncoding($encoding) { if ($encoding != 'UTF-16LE' && !function_exists('iconv')) { $this->raiseError("Using an input encoding other than UTF-16LE requires PHP support for iconv"); } $this->_input_encoding = $encoding; } /** * Write a string to the specified row and column (zero indexed). * This is the BIFF8 version (no 255 chars limit). * $format is optional. * Returns 0 : normal termination * -2 : row or column out of range * -3 : long string truncated to 255 chars * * @access public * @param integer $row Zero indexed row * @param integer $col Zero indexed column * @param string $str The string to write * @param mixed $format The XF format for the cell * @return integer */ function writeStringBIFF8($row, $col, $str, $format = null) { if ($this->_input_encoding == 'UTF-16LE') { $strlen = function_exists('mb_strlen') ? mb_strlen($str, 'UTF-16LE') : (strlen($str) / 2); $encoding = 0x1; } elseif ($this->_input_encoding != '') { $str = iconv($this->_input_encoding, 'UTF-16LE', $str); $strlen = function_exists('mb_strlen') ? mb_strlen($str, 'UTF-16LE') : (strlen($str) / 2); $encoding = 0x1; } else { $strlen = strlen($str); $encoding = 0x0; } $record = 0x00FD; // Record identifier $length = 0x000A; // Bytes to follow $xf = $this->_XF($format); // The cell format $str_error = 0; // Check that row and col are valid and store max and min values if ($this->_checkRowCol($row, $col) == false) { return -2; } $str = pack('vC', $strlen, $encoding).$str; /* check if string is already present */ if (!isset($this->_str_table[$str])) { $this->_str_table[$str] = $this->_str_unique++; } $this->_str_total++; $header = pack('vv', $record, $length); $data = pack('vvvV', $row, $col, $xf, $this->_str_table[$str]); $this->_append($header.$data); return $str_error; } /** * Check row and col before writing to a cell, and update the sheet's * dimensions accordingly * * @access private * @param integer $row Zero indexed row * @param integer $col Zero indexed column * @return boolean true for success, false if row and/or col are grester * then maximums allowed. */ function _checkRowCol($row, $col) { if ($row >= $this->_xls_rowmax) { return false; } if ($col >= $this->_xls_colmax) { return false; } if ($row < $this->_dim_rowmin) { $this->_dim_rowmin = $row; } if ($row > $this->_dim_rowmax) { $this->_dim_rowmax = $row; } if ($col < $this->_dim_colmin) { $this->_dim_colmin = $col; } if ($col > $this->_dim_colmax) { $this->_dim_colmax = $col; } return true; } /** * Writes a note associated with the cell given by the row and column. * NOTE records don't have a length limit. * * @access public * @param integer $row Zero indexed row * @param integer $col Zero indexed column * @param string $note The note to write */ function writeNote($row, $col, $note) { $note_length = strlen($note); $record = 0x001C; // Record identifier $max_length = 2048; // Maximun length for a NOTE record //$length = 0x0006 + $note_length; // Bytes to follow // Check that row and col are valid and store max and min values if ($row >= $this->_xls_rowmax) { return(-2); } if ($col >= $this->_xls_colmax) { return(-2); } if ($row < $this->_dim_rowmin) { $this->_dim_rowmin = $row; } if ($row > $this->_dim_rowmax) { $this->_dim_rowmax = $row; } if ($col < $this->_dim_colmin) { $this->_dim_colmin = $col; } if ($col > $this->_dim_colmax) { $this->_dim_colmax = $col; } // Length for this record is no more than 2048 + 6 $length = 0x0006 + min($note_length, 2048); $header = pack("vv", $record, $length); $data = pack("vvv", $row, $col, $note_length); $this->_append($header . $data . substr($note, 0, 2048)); for ($i = $max_length; $i < $note_length; $i += $max_length) { $chunk = substr($note, $i, $max_length); $length = 0x0006 + strlen($chunk); $header = pack("vv", $record, $length); $data = pack("vvv", -1, 0, strlen($chunk)); $this->_append($header.$data.$chunk); } return(0); } /** * Write a blank cell to the specified row and column (zero indexed). * A blank cell is used to specify formatting without adding a string * or a number. * * A blank cell without a format serves no purpose. Therefore, we don't write * a BLANK record unless a format is specified. * * Returns 0 : normal termination (including no format) * -1 : insufficient number of arguments * -2 : row or column out of range * * @access public * @param integer $row Zero indexed row * @param integer $col Zero indexed column * @param mixed $format The XF format */ function writeBlank($row, $col, $format) { // Don't write a blank cell unless it has a format if (!$format) { return(0); } $record = 0x0201; // Record identifier $length = 0x0006; // Number of bytes to follow $xf = $this->_XF($format); // The cell format // Check that row and col are valid and store max and min values if ($row >= $this->_xls_rowmax) { return(-2); } if ($col >= $this->_xls_colmax) { return(-2); } if ($row < $this->_dim_rowmin) { $this->_dim_rowmin = $row; } if ($row > $this->_dim_rowmax) { $this->_dim_rowmax = $row; } if ($col < $this->_dim_colmin) { $this->_dim_colmin = $col; } if ($col > $this->_dim_colmax) { $this->_dim_colmax = $col; } $header = pack("vv", $record, $length); $data = pack("vvv", $row, $col, $xf); $this->_append($header . $data); return 0; } /** * Write a formula to the specified row and column (zero indexed). * The textual representation of the formula is passed to the parser in * Parser.php which returns a packed binary string. * * Returns 0 : normal termination * -1 : formula errors (bad formula) * -2 : row or column out of range *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -