📄 workbook.php
字号:
/** * Change the RGB components of the elements in the colour palette. * * @access public * @param integer $index colour index * @param integer $red red RGB value [0-255] * @param integer $green green RGB value [0-255] * @param integer $blue blue RGB value [0-255] * @return integer The palette index for the custom color */ function setCustomColor($index, $red, $green, $blue) { // Match a HTML #xxyyzz style parameter /*if (defined $_[1] and $_[1] =~ /^#(\w\w)(\w\w)(\w\w)/ ) { @_ = ($_[0], hex $1, hex $2, hex $3); }*/ // Check that the colour index is the right range if ($index < 8 or $index > 64) { // TODO: assign real error codes return $this->raiseError("Color index $index outside range: 8 <= index <= 64"); } // Check that the colour components are in the right range if (($red < 0 or $red > 255) || ($green < 0 or $green > 255) || ($blue < 0 or $blue > 255)) { return $this->raiseError("Color component outside range: 0 <= color <= 255"); } $index -= 8; // Adjust colour index (wingless dragonfly) // Set the RGB value $this->_palette[$index] = array($red, $green, $blue, 0); return($index + 8); } /** * Sets the colour palette to the Excel 97+ default. * * @access private */ function _setPaletteXl97() { $this->_palette = array( array(0x00, 0x00, 0x00, 0x00), // 8 array(0xff, 0xff, 0xff, 0x00), // 9 array(0xff, 0x00, 0x00, 0x00), // 10 array(0x00, 0xff, 0x00, 0x00), // 11 array(0x00, 0x00, 0xff, 0x00), // 12 array(0xff, 0xff, 0x00, 0x00), // 13 array(0xff, 0x00, 0xff, 0x00), // 14 array(0x00, 0xff, 0xff, 0x00), // 15 array(0x80, 0x00, 0x00, 0x00), // 16 array(0x00, 0x80, 0x00, 0x00), // 17 array(0x00, 0x00, 0x80, 0x00), // 18 array(0x80, 0x80, 0x00, 0x00), // 19 array(0x80, 0x00, 0x80, 0x00), // 20 array(0x00, 0x80, 0x80, 0x00), // 21 array(0xc0, 0xc0, 0xc0, 0x00), // 22 array(0x80, 0x80, 0x80, 0x00), // 23 array(0x99, 0x99, 0xff, 0x00), // 24 array(0x99, 0x33, 0x66, 0x00), // 25 array(0xff, 0xff, 0xcc, 0x00), // 26 array(0xcc, 0xff, 0xff, 0x00), // 27 array(0x66, 0x00, 0x66, 0x00), // 28 array(0xff, 0x80, 0x80, 0x00), // 29 array(0x00, 0x66, 0xcc, 0x00), // 30 array(0xcc, 0xcc, 0xff, 0x00), // 31 array(0x00, 0x00, 0x80, 0x00), // 32 array(0xff, 0x00, 0xff, 0x00), // 33 array(0xff, 0xff, 0x00, 0x00), // 34 array(0x00, 0xff, 0xff, 0x00), // 35 array(0x80, 0x00, 0x80, 0x00), // 36 array(0x80, 0x00, 0x00, 0x00), // 37 array(0x00, 0x80, 0x80, 0x00), // 38 array(0x00, 0x00, 0xff, 0x00), // 39 array(0x00, 0xcc, 0xff, 0x00), // 40 array(0xcc, 0xff, 0xff, 0x00), // 41 array(0xcc, 0xff, 0xcc, 0x00), // 42 array(0xff, 0xff, 0x99, 0x00), // 43 array(0x99, 0xcc, 0xff, 0x00), // 44 array(0xff, 0x99, 0xcc, 0x00), // 45 array(0xcc, 0x99, 0xff, 0x00), // 46 array(0xff, 0xcc, 0x99, 0x00), // 47 array(0x33, 0x66, 0xff, 0x00), // 48 array(0x33, 0xcc, 0xcc, 0x00), // 49 array(0x99, 0xcc, 0x00, 0x00), // 50 array(0xff, 0xcc, 0x00, 0x00), // 51 array(0xff, 0x99, 0x00, 0x00), // 52 array(0xff, 0x66, 0x00, 0x00), // 53 array(0x66, 0x66, 0x99, 0x00), // 54 array(0x96, 0x96, 0x96, 0x00), // 55 array(0x00, 0x33, 0x66, 0x00), // 56 array(0x33, 0x99, 0x66, 0x00), // 57 array(0x00, 0x33, 0x00, 0x00), // 58 array(0x33, 0x33, 0x00, 0x00), // 59 array(0x99, 0x33, 0x00, 0x00), // 60 array(0x99, 0x33, 0x66, 0x00), // 61 array(0x33, 0x33, 0x99, 0x00), // 62 array(0x33, 0x33, 0x33, 0x00), // 63 ); } /** * Assemble worksheets into a workbook and send the BIFF data to an OLE * storage. * * @access private * @return mixed true on success. PEAR_Error on failure */ function _storeWorkbook() { // Ensure that at least one worksheet has been selected. if ($this->_activesheet == 0) { $this->_worksheets[0]->selected = 1; } // Calculate the number of selected worksheet tabs and call the finalization // methods for each worksheet $total_worksheets = count($this->_worksheets); for ($i = 0; $i < $total_worksheets; $i++) { if ($this->_worksheets[$i]->selected) { $this->_selected++; } $this->_worksheets[$i]->close($this->_sheetnames); } // Add Workbook globals $this->_storeBof(0x0005); $this->_storeCodepage(); if ($this->_BIFF_version == 0x0600) { $this->_storeWindow1(); } if ($this->_BIFF_version == 0x0500) { $this->_storeExterns(); // For print area and repeat rows } $this->_storeNames(); // For print area and repeat rows if ($this->_BIFF_version == 0x0500) { $this->_storeWindow1(); } $this->_storeDatemode(); $this->_storeAllFonts(); $this->_storeAllNumFormats(); $this->_storeAllXfs(); $this->_storeAllStyles(); $this->_storePalette(); $this->_calcSheetOffsets(); // Add BOUNDSHEET records for ($i = 0; $i < $total_worksheets; $i++) { $this->_storeBoundsheet($this->_worksheets[$i]->name,$this->_worksheets[$i]->offset); } if ($this->_country_code != -1) { $this->_storeCountry(); } if ($this->_BIFF_version == 0x0600) { //$this->_storeSupbookInternal(); /* TODO: store external SUPBOOK records and XCT and CRN records in case of external references for BIFF8 */ //$this->_storeExternsheetBiff8(); $this->_storeSharedStringsTable(); } // End Workbook globals $this->_storeEof(); // Store the workbook in an OLE container $res = $this->_storeOLEFile(); if ($this->isError($res)) { return $this->raiseError($res->getMessage()); } return true; } /** * Sets the temp dir used for storing the OLE file * * @access public * @param string $dir The dir to be used as temp dir * @return true if given dir is valid, false otherwise */ function setTempDir($dir) { if (is_dir($dir)) { $this->_tmp_dir = $dir; return true; } return false; } /** * Store the workbook in an OLE container * * @access private * @return mixed true on success. PEAR_Error on failure */ function _storeOLEFile() { $OLE = new OLE_PPS_File(OLE::Asc2Ucs('Book')); if ($this->_tmp_dir != '') { $OLE->setTempDir($this->_tmp_dir); } $res = $OLE->init(); if ($this->isError($res)) { return $this->raiseError("OLE Error: ".$res->getMessage()); } $OLE->append($this->_data); $total_worksheets = count($this->_worksheets); for ($i = 0; $i < $total_worksheets; $i++) { while ($tmp = $this->_worksheets[$i]->getData()) { $OLE->append($tmp); } } $root = new OLE_PPS_Root(time(), time(), array($OLE)); if ($this->_tmp_dir != '') { $root->setTempDir($this->_tmp_dir); } $res = $root->save($this->_filename); if ($this->isError($res)) { return $this->raiseError("OLE Error: ".$res->getMessage()); } return true; } /** * Calculate offsets for Worksheet BOF records. * * @access private */ function _calcSheetOffsets() { if ($this->_BIFF_version == 0x0600) { $boundsheet_length = 12; // fixed length for a BOUNDSHEET record } else { $boundsheet_length = 11; } $EOF = 4; $offset = $this->_datasize; if ($this->_BIFF_version == 0x0600) { // add the length of the SST /* TODO: check this works for a lot of strings (> 8224 bytes) */ $offset += $this->_calculateSharedStringsSizes(); if ($this->_country_code != -1) { $offset += 8; // adding COUNTRY record } // add the lenght of SUPBOOK, EXTERNSHEET and NAME records //$offset += 8; // FIXME: calculate real value when storing the records } $total_worksheets = count($this->_worksheets); // add the length of the BOUNDSHEET records for ($i = 0; $i < $total_worksheets; $i++) { $offset += $boundsheet_length + strlen($this->_worksheets[$i]->name); } $offset += $EOF; for ($i = 0; $i < $total_worksheets; $i++) { $this->_worksheets[$i]->offset = $offset; $offset += $this->_worksheets[$i]->_datasize; } $this->_biffsize = $offset; } /** * Store the Excel FONT records. * * @access private */ function _storeAllFonts() { // tmp_format is added by the constructor. We use this to write the default XF's $format = $this->_tmp_format; $font = $format->getFont(); // Note: Fonts are 0-indexed. According to the SDK there is no index 4, // so the following fonts are 0, 1, 2, 3, 5 // for ($i = 1; $i <= 5; $i++){ $this->_append($font); } // Iterate through the XF objects and write a FONT record if it isn't the // same as the default FONT and if it hasn't already been used. // $fonts = array(); $index = 6; // The first user defined FONT $key = $format->getFontKey(); // The default font from _tmp_format $fonts[$key] = 0; // Index of the default font $total_formats = count($this->_formats); for ($i = 0; $i < $total_formats; $i++) { $key = $this->_formats[$i]->getFontKey(); if (isset($fonts[$key])) { // FONT has already been used $this->_formats[$i]->font_index = $fonts[$key]; } else { // Add a new FONT record $fonts[$key] = $index; $this->_formats[$i]->font_index = $index; $index++; $font = $this->_formats[$i]->getFont(); $this->_append($font); } } } /** * Store user defined numerical formats i.e. FORMAT records * * @access private */ function _storeAllNumFormats() { // Leaning num_format syndrome $hash_num_formats = array(); $num_formats = array(); $index = 164; // Iterate through the XF objects and write a FORMAT record if it isn't a // built-in format type and if the FORMAT string hasn't already been used. $total_formats = count($this->_formats); for ($i = 0; $i < $total_formats; $i++) { $num_format = $this->_formats[$i]->_num_format; // Check if $num_format is an index to a built-in format. // Also check for a string of zeros, which is a valid format string // but would evaluate to zero. // if (!preg_match("/^0+\d/", $num_format)) { if (preg_match("/^\d+$/", $num_format)) { // built-in format continue; } } if (isset($hash_num_formats[$num_format])) { // FORMAT has already been used $this->_formats[$i]->_num_format = $hash_num_formats[$num_format]; } else{ // Add a new FORMAT $hash_num_formats[$num_format] = $index; $this->_formats[$i]->_num_format = $index; array_push($num_formats,$num_format); $index++; } } // Write the new FORMAT records starting from 0xA4 $index = 164; foreach ($num_formats as $num_format) { $this->_storeNumFormat($num_format,$index); $index++; } } /** * Write all XF records. * * @access private */ function _storeAllXfs() { // _tmp_format is added by the constructor. We use this to write the default XF's // The default font index is 0 // $format = $this->_tmp_format; for ($i = 0; $i <= 14; $i++) { $xf = $format->getXf('style'); // Style XF $this->_append($xf); } $xf = $format->getXf('cell'); // Cell XF $this->_append($xf); // User defined XFs $total_formats = count($this->_formats);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -