Workbook.php 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. // Original file header of PEAR::Spreadsheet_Excel_Writer_Workbook (used as the base for this class):
  8. // -----------------------------------------------------------------------------------------
  9. // /*
  10. // * Module written/ported by Xavier Noguer <xnoguer@rezebra.com>
  11. // *
  12. // * The majority of this is _NOT_ my code. I simply ported it from the
  13. // * PERL Spreadsheet::WriteExcel module.
  14. // *
  15. // * The author of the Spreadsheet::WriteExcel module is John McNamara
  16. // * <jmcnamara@cpan.org>
  17. // *
  18. // * I _DO_ maintain this code, and John McNamara has nothing to do with the
  19. // * porting of this code to PHP. Any questions directly related to this
  20. // * class library should be directed to me.
  21. // *
  22. // * License Information:
  23. // *
  24. // * Spreadsheet_Excel_Writer: A library for generating Excel Spreadsheets
  25. // * Copyright (c) 2002-2003 Xavier Noguer xnoguer@rezebra.com
  26. // *
  27. // * This library is free software; you can redistribute it and/or
  28. // * modify it under the terms of the GNU Lesser General Public
  29. // * License as published by the Free Software Foundation; either
  30. // * version 2.1 of the License, or (at your option) any later version.
  31. // *
  32. // * This library is distributed in the hope that it will be useful,
  33. // * but WITHOUT ANY WARRANTY; without even the implied warranty of
  34. // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  35. // * Lesser General Public License for more details.
  36. // *
  37. // * You should have received a copy of the GNU Lesser General Public
  38. // * License along with this library; if not, write to the Free Software
  39. // * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  40. // */
  41. class PHPExcel_Writer_Excel5_Workbook extends PHPExcel_Writer_Excel5_BIFFwriter
  42. {
  43. /**
  44. * Formula parser
  45. *
  46. * @var PHPExcel_Writer_Excel5_Parser
  47. */
  48. private $parser;
  49. /**
  50. * The BIFF file size for the workbook.
  51. * @var integer
  52. * @see calcSheetOffsets()
  53. */
  54. private $biffSize;
  55. /**
  56. * XF Writers
  57. * @var PHPExcel_Writer_Excel5_Xf[]
  58. */
  59. private $xfWriters = array();
  60. /**
  61. * Array containing the colour palette
  62. * @var array
  63. */
  64. private $palette;
  65. /**
  66. * The codepage indicates the text encoding used for strings
  67. * @var integer
  68. */
  69. private $codepage;
  70. /**
  71. * The country code used for localization
  72. * @var integer
  73. */
  74. private $countryCode;
  75. /**
  76. * Workbook
  77. * @var PHPExcel
  78. */
  79. private $phpExcel;
  80. /**
  81. * Fonts writers
  82. *
  83. * @var PHPExcel_Writer_Excel5_Font[]
  84. */
  85. private $fontWriters = array();
  86. /**
  87. * Added fonts. Maps from font's hash => index in workbook
  88. *
  89. * @var array
  90. */
  91. private $addedFonts = array();
  92. /**
  93. * Shared number formats
  94. *
  95. * @var array
  96. */
  97. private $numberFormats = array();
  98. /**
  99. * Added number formats. Maps from numberFormat's hash => index in workbook
  100. *
  101. * @var array
  102. */
  103. private $addedNumberFormats = array();
  104. /**
  105. * Sizes of the binary worksheet streams
  106. *
  107. * @var array
  108. */
  109. private $worksheetSizes = array();
  110. /**
  111. * Offsets of the binary worksheet streams relative to the start of the global workbook stream
  112. *
  113. * @var array
  114. */
  115. private $worksheetOffsets = array();
  116. /**
  117. * Total number of shared strings in workbook
  118. *
  119. * @var int
  120. */
  121. private $stringTotal;
  122. /**
  123. * Number of unique shared strings in workbook
  124. *
  125. * @var int
  126. */
  127. private $stringUnique;
  128. /**
  129. * Array of unique shared strings in workbook
  130. *
  131. * @var array
  132. */
  133. private $stringTable;
  134. /**
  135. * Color cache
  136. */
  137. private $colors;
  138. /**
  139. * Escher object corresponding to MSODRAWINGGROUP
  140. *
  141. * @var PHPExcel_Shared_Escher
  142. */
  143. private $escher;
  144. /**
  145. * Class constructor
  146. *
  147. * @param PHPExcel $phpExcel The Workbook
  148. * @param int &$str_total Total number of strings
  149. * @param int &$str_unique Total number of unique strings
  150. * @param array &$str_table String Table
  151. * @param array &$colors Colour Table
  152. * @param mixed $parser The formula parser created for the Workbook
  153. */
  154. public function __construct(PHPExcel $phpExcel = null, &$str_total, &$str_unique, &$str_table, &$colors, $parser)
  155. {
  156. // It needs to call its parent's constructor explicitly
  157. parent::__construct();
  158. $this->parser = $parser;
  159. $this->biffSize = 0;
  160. $this->palette = array();
  161. $this->countryCode = -1;
  162. $this->stringTotal = &$str_total;
  163. $this->stringUnique = &$str_unique;
  164. $this->stringTable = &$str_table;
  165. $this->colors = &$colors;
  166. $this->setPaletteXl97();
  167. $this->phpExcel = $phpExcel;
  168. // set BIFFwriter limit for CONTINUE records
  169. // $this->_limit = 8224;
  170. $this->codepage = 0x04B0;
  171. // Add empty sheets and Build color cache
  172. $countSheets = $phpExcel->getSheetCount();
  173. for ($i = 0; $i < $countSheets; ++$i) {
  174. $phpSheet = $phpExcel->getSheet($i);
  175. $this->parser->setExtSheet($phpSheet->getTitle(), $i); // Register worksheet name with parser
  176. $supbook_index = 0x00;
  177. $ref = pack('vvv', $supbook_index, $i, $i);
  178. $this->parser->references[] = $ref; // Register reference with parser
  179. // Sheet tab colors?
  180. if ($phpSheet->isTabColorSet()) {
  181. $this->addColor($phpSheet->getTabColor()->getRGB());
  182. }
  183. }
  184. }
  185. /**
  186. * Add a new XF writer
  187. *
  188. * @param PHPExcel_Style
  189. * @param boolean Is it a style XF?
  190. * @return int Index to XF record
  191. */
  192. public function addXfWriter($style, $isStyleXf = false)
  193. {
  194. $xfWriter = new PHPExcel_Writer_Excel5_Xf($style);
  195. $xfWriter->setIsStyleXf($isStyleXf);
  196. // Add the font if not already added
  197. $fontIndex = $this->addFont($style->getFont());
  198. // Assign the font index to the xf record
  199. $xfWriter->setFontIndex($fontIndex);
  200. // Background colors, best to treat these after the font so black will come after white in custom palette
  201. $xfWriter->setFgColor($this->addColor($style->getFill()->getStartColor()->getRGB()));
  202. $xfWriter->setBgColor($this->addColor($style->getFill()->getEndColor()->getRGB()));
  203. $xfWriter->setBottomColor($this->addColor($style->getBorders()->getBottom()->getColor()->getRGB()));
  204. $xfWriter->setTopColor($this->addColor($style->getBorders()->getTop()->getColor()->getRGB()));
  205. $xfWriter->setRightColor($this->addColor($style->getBorders()->getRight()->getColor()->getRGB()));
  206. $xfWriter->setLeftColor($this->addColor($style->getBorders()->getLeft()->getColor()->getRGB()));
  207. $xfWriter->setDiagColor($this->addColor($style->getBorders()->getDiagonal()->getColor()->getRGB()));
  208. // Add the number format if it is not a built-in one and not already added
  209. if ($style->getNumberFormat()->getBuiltInFormatCode() === false) {
  210. $numberFormatHashCode = $style->getNumberFormat()->getHashCode();
  211. if (isset($this->addedNumberFormats[$numberFormatHashCode])) {
  212. $numberFormatIndex = $this->addedNumberFormats[$numberFormatHashCode];
  213. } else {
  214. $numberFormatIndex = 164 + count($this->numberFormats);
  215. $this->numberFormats[$numberFormatIndex] = $style->getNumberFormat();
  216. $this->addedNumberFormats[$numberFormatHashCode] = $numberFormatIndex;
  217. }
  218. } else {
  219. $numberFormatIndex = (int) $style->getNumberFormat()->getBuiltInFormatCode();
  220. }
  221. // Assign the number format index to xf record
  222. $xfWriter->setNumberFormatIndex($numberFormatIndex);
  223. $this->xfWriters[] = $xfWriter;
  224. $xfIndex = count($this->xfWriters) - 1;
  225. return $xfIndex;
  226. }
  227. /**
  228. * Add a font to added fonts
  229. *
  230. * @param PHPExcel_Style_Font $font
  231. * @return int Index to FONT record
  232. */
  233. public function addFont(PHPExcel_Style_Font $font)
  234. {
  235. $fontHashCode = $font->getHashCode();
  236. if (isset($this->addedFonts[$fontHashCode])) {
  237. $fontIndex = $this->addedFonts[$fontHashCode];
  238. } else {
  239. $countFonts = count($this->fontWriters);
  240. $fontIndex = ($countFonts < 4) ? $countFonts : $countFonts + 1;
  241. $fontWriter = new PHPExcel_Writer_Excel5_Font($font);
  242. $fontWriter->setColorIndex($this->addColor($font->getColor()->getRGB()));
  243. $this->fontWriters[] = $fontWriter;
  244. $this->addedFonts[$fontHashCode] = $fontIndex;
  245. }
  246. return $fontIndex;
  247. }
  248. /**
  249. * Alter color palette adding a custom color
  250. *
  251. * @param string $rgb E.g. 'FF00AA'
  252. * @return int Color index
  253. */
  254. private function addColor($rgb)
  255. {
  256. if (!isset($this->colors[$rgb])) {
  257. if (count($this->colors) < 57) {
  258. // then we add a custom color altering the palette
  259. $colorIndex = 8 + count($this->colors);
  260. $this->palette[$colorIndex] =
  261. array(
  262. hexdec(substr($rgb, 0, 2)),
  263. hexdec(substr($rgb, 2, 2)),
  264. hexdec(substr($rgb, 4)),
  265. 0
  266. );
  267. $this->colors[$rgb] = $colorIndex;
  268. } else {
  269. // no room for more custom colors, just map to black
  270. $colorIndex = 0;
  271. }
  272. } else {
  273. // fetch already added custom color
  274. $colorIndex = $this->colors[$rgb];
  275. }
  276. return $colorIndex;
  277. }
  278. /**
  279. * Sets the colour palette to the Excel 97+ default.
  280. *
  281. * @access private
  282. */
  283. private function setPaletteXl97()
  284. {
  285. $this->palette = array(
  286. 0x08 => array(0x00, 0x00, 0x00, 0x00),
  287. 0x09 => array(0xff, 0xff, 0xff, 0x00),
  288. 0x0A => array(0xff, 0x00, 0x00, 0x00),
  289. 0x0B => array(0x00, 0xff, 0x00, 0x00),
  290. 0x0C => array(0x00, 0x00, 0xff, 0x00),
  291. 0x0D => array(0xff, 0xff, 0x00, 0x00),
  292. 0x0E => array(0xff, 0x00, 0xff, 0x00),
  293. 0x0F => array(0x00, 0xff, 0xff, 0x00),
  294. 0x10 => array(0x80, 0x00, 0x00, 0x00),
  295. 0x11 => array(0x00, 0x80, 0x00, 0x00),
  296. 0x12 => array(0x00, 0x00, 0x80, 0x00),
  297. 0x13 => array(0x80, 0x80, 0x00, 0x00),
  298. 0x14 => array(0x80, 0x00, 0x80, 0x00),
  299. 0x15 => array(0x00, 0x80, 0x80, 0x00),
  300. 0x16 => array(0xc0, 0xc0, 0xc0, 0x00),
  301. 0x17 => array(0x80, 0x80, 0x80, 0x00),
  302. 0x18 => array(0x99, 0x99, 0xff, 0x00),
  303. 0x19 => array(0x99, 0x33, 0x66, 0x00),
  304. 0x1A => array(0xff, 0xff, 0xcc, 0x00),
  305. 0x1B => array(0xcc, 0xff, 0xff, 0x00),
  306. 0x1C => array(0x66, 0x00, 0x66, 0x00),
  307. 0x1D => array(0xff, 0x80, 0x80, 0x00),
  308. 0x1E => array(0x00, 0x66, 0xcc, 0x00),
  309. 0x1F => array(0xcc, 0xcc, 0xff, 0x00),
  310. 0x20 => array(0x00, 0x00, 0x80, 0x00),
  311. 0x21 => array(0xff, 0x00, 0xff, 0x00),
  312. 0x22 => array(0xff, 0xff, 0x00, 0x00),
  313. 0x23 => array(0x00, 0xff, 0xff, 0x00),
  314. 0x24 => array(0x80, 0x00, 0x80, 0x00),
  315. 0x25 => array(0x80, 0x00, 0x00, 0x00),
  316. 0x26 => array(0x00, 0x80, 0x80, 0x00),
  317. 0x27 => array(0x00, 0x00, 0xff, 0x00),
  318. 0x28 => array(0x00, 0xcc, 0xff, 0x00),
  319. 0x29 => array(0xcc, 0xff, 0xff, 0x00),
  320. 0x2A => array(0xcc, 0xff, 0xcc, 0x00),
  321. 0x2B => array(0xff, 0xff, 0x99, 0x00),
  322. 0x2C => array(0x99, 0xcc, 0xff, 0x00),
  323. 0x2D => array(0xff, 0x99, 0xcc, 0x00),
  324. 0x2E => array(0xcc, 0x99, 0xff, 0x00),
  325. 0x2F => array(0xff, 0xcc, 0x99, 0x00),
  326. 0x30 => array(0x33, 0x66, 0xff, 0x00),
  327. 0x31 => array(0x33, 0xcc, 0xcc, 0x00),
  328. 0x32 => array(0x99, 0xcc, 0x00, 0x00),
  329. 0x33 => array(0xff, 0xcc, 0x00, 0x00),
  330. 0x34 => array(0xff, 0x99, 0x00, 0x00),
  331. 0x35 => array(0xff, 0x66, 0x00, 0x00),
  332. 0x36 => array(0x66, 0x66, 0x99, 0x00),
  333. 0x37 => array(0x96, 0x96, 0x96, 0x00),
  334. 0x38 => array(0x00, 0x33, 0x66, 0x00),
  335. 0x39 => array(0x33, 0x99, 0x66, 0x00),
  336. 0x3A => array(0x00, 0x33, 0x00, 0x00),
  337. 0x3B => array(0x33, 0x33, 0x00, 0x00),
  338. 0x3C => array(0x99, 0x33, 0x00, 0x00),
  339. 0x3D => array(0x99, 0x33, 0x66, 0x00),
  340. 0x3E => array(0x33, 0x33, 0x99, 0x00),
  341. 0x3F => array(0x33, 0x33, 0x33, 0x00),
  342. );
  343. }
  344. /**
  345. * Assemble worksheets into a workbook and send the BIFF data to an OLE
  346. * storage.
  347. *
  348. * @param array $pWorksheetSizes The sizes in bytes of the binary worksheet streams
  349. * @return string Binary data for workbook stream
  350. */
  351. public function writeWorkbook($pWorksheetSizes = null)
  352. {
  353. $this->worksheetSizes = $pWorksheetSizes;
  354. // Calculate the number of selected worksheet tabs and call the finalization
  355. // methods for each worksheet
  356. $total_worksheets = $this->phpExcel->getSheetCount();
  357. // Add part 1 of the Workbook globals, what goes before the SHEET records
  358. $this->storeBof(0x0005);
  359. $this->writeCodepage();
  360. $this->writeWindow1();
  361. $this->writeDateMode();
  362. $this->writeAllFonts();
  363. $this->writeAllNumberFormats();
  364. $this->writeAllXfs();
  365. $this->writeAllStyles();
  366. $this->writePalette();
  367. // Prepare part 3 of the workbook global stream, what goes after the SHEET records
  368. $part3 = '';
  369. if ($this->countryCode != -1) {
  370. $part3 .= $this->writeCountry();
  371. }
  372. $part3 .= $this->writeRecalcId();
  373. $part3 .= $this->writeSupbookInternal();
  374. /* TODO: store external SUPBOOK records and XCT and CRN records
  375. in case of external references for BIFF8 */
  376. $part3 .= $this->writeExternalsheetBiff8();
  377. $part3 .= $this->writeAllDefinedNamesBiff8();
  378. $part3 .= $this->writeMsoDrawingGroup();
  379. $part3 .= $this->writeSharedStringsTable();
  380. $part3 .= $this->writeEof();
  381. // Add part 2 of the Workbook globals, the SHEET records
  382. $this->calcSheetOffsets();
  383. for ($i = 0; $i < $total_worksheets; ++$i) {
  384. $this->writeBoundSheet($this->phpExcel->getSheet($i), $this->worksheetOffsets[$i]);
  385. }
  386. // Add part 3 of the Workbook globals
  387. $this->_data .= $part3;
  388. return $this->_data;
  389. }
  390. /**
  391. * Calculate offsets for Worksheet BOF records.
  392. *
  393. * @access private
  394. */
  395. private function calcSheetOffsets()
  396. {
  397. $boundsheet_length = 10; // fixed length for a BOUNDSHEET record
  398. // size of Workbook globals part 1 + 3
  399. $offset = $this->_datasize;
  400. // add size of Workbook globals part 2, the length of the SHEET records
  401. $total_worksheets = count($this->phpExcel->getAllSheets());
  402. foreach ($this->phpExcel->getWorksheetIterator() as $sheet) {
  403. $offset += $boundsheet_length + strlen(PHPExcel_Shared_String::UTF8toBIFF8UnicodeShort($sheet->getTitle()));
  404. }
  405. // add the sizes of each of the Sheet substreams, respectively
  406. for ($i = 0; $i < $total_worksheets; ++$i) {
  407. $this->worksheetOffsets[$i] = $offset;
  408. $offset += $this->worksheetSizes[$i];
  409. }
  410. $this->biffSize = $offset;
  411. }
  412. /**
  413. * Store the Excel FONT records.
  414. */
  415. private function writeAllFonts()
  416. {
  417. foreach ($this->fontWriters as $fontWriter) {
  418. $this->append($fontWriter->writeFont());
  419. }
  420. }
  421. /**
  422. * Store user defined numerical formats i.e. FORMAT records
  423. */
  424. private function writeAllNumberFormats()
  425. {
  426. foreach ($this->numberFormats as $numberFormatIndex => $numberFormat) {
  427. $this->writeNumberFormat($numberFormat->getFormatCode(), $numberFormatIndex);
  428. }
  429. }
  430. /**
  431. * Write all XF records.
  432. */
  433. private function writeAllXfs()
  434. {
  435. foreach ($this->xfWriters as $xfWriter) {
  436. $this->append($xfWriter->writeXf());
  437. }
  438. }
  439. /**
  440. * Write all STYLE records.
  441. */
  442. private function writeAllStyles()
  443. {
  444. $this->writeStyle();
  445. }
  446. /**
  447. * Write the EXTERNCOUNT and EXTERNSHEET records. These are used as indexes for
  448. * the NAME records.
  449. */
  450. private function writeExternals()
  451. {
  452. $countSheets = $this->phpExcel->getSheetCount();
  453. // Create EXTERNCOUNT with number of worksheets
  454. $this->writeExternalCount($countSheets);
  455. // Create EXTERNSHEET for each worksheet
  456. for ($i = 0; $i < $countSheets; ++$i) {
  457. $this->writeExternalSheet($this->phpExcel->getSheet($i)->getTitle());
  458. }
  459. }
  460. /**
  461. * Write the NAME record to define the print area and the repeat rows and cols.
  462. */
  463. private function writeNames()
  464. {
  465. // total number of sheets
  466. $total_worksheets = $this->phpExcel->getSheetCount();
  467. // Create the print area NAME records
  468. for ($i = 0; $i < $total_worksheets; ++$i) {
  469. $sheetSetup = $this->phpExcel->getSheet($i)->getPageSetup();
  470. // Write a Name record if the print area has been defined
  471. if ($sheetSetup->isPrintAreaSet()) {
  472. // Print area
  473. $printArea = PHPExcel_Cell::splitRange($sheetSetup->getPrintArea());
  474. $printArea = $printArea[0];
  475. $printArea[0] = PHPExcel_Cell::coordinateFromString($printArea[0]);
  476. $printArea[1] = PHPExcel_Cell::coordinateFromString($printArea[1]);
  477. $print_rowmin = $printArea[0][1] - 1;
  478. $print_rowmax = $printArea[1][1] - 1;
  479. $print_colmin = PHPExcel_Cell::columnIndexFromString($printArea[0][0]) - 1;
  480. $print_colmax = PHPExcel_Cell::columnIndexFromString($printArea[1][0]) - 1;
  481. $this->writeNameShort(
  482. $i, // sheet index
  483. 0x06, // NAME type
  484. $print_rowmin,
  485. $print_rowmax,
  486. $print_colmin,
  487. $print_colmax
  488. );
  489. }
  490. }
  491. // Create the print title NAME records
  492. for ($i = 0; $i < $total_worksheets; ++$i) {
  493. $sheetSetup = $this->phpExcel->getSheet($i)->getPageSetup();
  494. // simultaneous repeatColumns repeatRows
  495. if ($sheetSetup->isColumnsToRepeatAtLeftSet() && $sheetSetup->isRowsToRepeatAtTopSet()) {
  496. $repeat = $sheetSetup->getColumnsToRepeatAtLeft();
  497. $colmin = PHPExcel_Cell::columnIndexFromString($repeat[0]) - 1;
  498. $colmax = PHPExcel_Cell::columnIndexFromString($repeat[1]) - 1;
  499. $repeat = $sheetSetup->getRowsToRepeatAtTop();
  500. $rowmin = $repeat[0] - 1;
  501. $rowmax = $repeat[1] - 1;
  502. $this->writeNameLong(
  503. $i, // sheet index
  504. 0x07, // NAME type
  505. $rowmin,
  506. $rowmax,
  507. $colmin,
  508. $colmax
  509. );
  510. // (exclusive) either repeatColumns or repeatRows
  511. } elseif ($sheetSetup->isColumnsToRepeatAtLeftSet() || $sheetSetup->isRowsToRepeatAtTopSet()) {
  512. // Columns to repeat
  513. if ($sheetSetup->isColumnsToRepeatAtLeftSet()) {
  514. $repeat = $sheetSetup->getColumnsToRepeatAtLeft();
  515. $colmin = PHPExcel_Cell::columnIndexFromString($repeat[0]) - 1;
  516. $colmax = PHPExcel_Cell::columnIndexFromString($repeat[1]) - 1;
  517. } else {
  518. $colmin = 0;
  519. $colmax = 255;
  520. }
  521. // Rows to repeat
  522. if ($sheetSetup->isRowsToRepeatAtTopSet()) {
  523. $repeat = $sheetSetup->getRowsToRepeatAtTop();
  524. $rowmin = $repeat[0] - 1;
  525. $rowmax = $repeat[1] - 1;
  526. } else {
  527. $rowmin = 0;
  528. $rowmax = 65535;
  529. }
  530. $this->writeNameShort(
  531. $i, // sheet index
  532. 0x07, // NAME type
  533. $rowmin,
  534. $rowmax,
  535. $colmin,
  536. $colmax
  537. );
  538. }
  539. }
  540. }
  541. /**
  542. * Writes all the DEFINEDNAME records (BIFF8).
  543. * So far this is only used for repeating rows/columns (print titles) and print areas
  544. */
  545. private function writeAllDefinedNamesBiff8()
  546. {
  547. $chunk = '';
  548. // Named ranges
  549. if (count($this->phpExcel->getNamedRanges()) > 0) {
  550. // Loop named ranges
  551. $namedRanges = $this->phpExcel->getNamedRanges();
  552. foreach ($namedRanges as $namedRange) {
  553. // Create absolute coordinate
  554. $range = PHPExcel_Cell::splitRange($namedRange->getRange());
  555. for ($i = 0; $i < count($range); $i++) {
  556. $range[$i][0] = '\'' . str_replace("'", "''", $namedRange->getWorksheet()->getTitle()) . '\'!' . PHPExcel_Cell::absoluteCoordinate($range[$i][0]);
  557. if (isset($range[$i][1])) {
  558. $range[$i][1] = PHPExcel_Cell::absoluteCoordinate($range[$i][1]);
  559. }
  560. }
  561. $range = PHPExcel_Cell::buildRange($range); // e.g. Sheet1!$A$1:$B$2
  562. // parse formula
  563. try {
  564. $error = $this->parser->parse($range);
  565. $formulaData = $this->parser->toReversePolish();
  566. // make sure tRef3d is of type tRef3dR (0x3A)
  567. if (isset($formulaData{0}) and ($formulaData{0} == "\x7A" or $formulaData{0} == "\x5A")) {
  568. $formulaData = "\x3A" . substr($formulaData, 1);
  569. }
  570. if ($namedRange->getLocalOnly()) {
  571. // local scope
  572. $scope = $this->phpExcel->getIndex($namedRange->getScope()) + 1;
  573. } else {
  574. // global scope
  575. $scope = 0;
  576. }
  577. $chunk .= $this->writeData($this->writeDefinedNameBiff8($namedRange->getName(), $formulaData, $scope, false));
  578. } catch (PHPExcel_Exception $e) {
  579. // do nothing
  580. }
  581. }
  582. }
  583. // total number of sheets
  584. $total_worksheets = $this->phpExcel->getSheetCount();
  585. // write the print titles (repeating rows, columns), if any
  586. for ($i = 0; $i < $total_worksheets; ++$i) {
  587. $sheetSetup = $this->phpExcel->getSheet($i)->getPageSetup();
  588. // simultaneous repeatColumns repeatRows
  589. if ($sheetSetup->isColumnsToRepeatAtLeftSet() && $sheetSetup->isRowsToRepeatAtTopSet()) {
  590. $repeat = $sheetSetup->getColumnsToRepeatAtLeft();
  591. $colmin = PHPExcel_Cell::columnIndexFromString($repeat[0]) - 1;
  592. $colmax = PHPExcel_Cell::columnIndexFromString($repeat[1]) - 1;
  593. $repeat = $sheetSetup->getRowsToRepeatAtTop();
  594. $rowmin = $repeat[0] - 1;
  595. $rowmax = $repeat[1] - 1;
  596. // construct formula data manually
  597. $formulaData = pack('Cv', 0x29, 0x17); // tMemFunc
  598. $formulaData .= pack('Cvvvvv', 0x3B, $i, 0, 65535, $colmin, $colmax); // tArea3d
  599. $formulaData .= pack('Cvvvvv', 0x3B, $i, $rowmin, $rowmax, 0, 255); // tArea3d
  600. $formulaData .= pack('C', 0x10); // tList
  601. // store the DEFINEDNAME record
  602. $chunk .= $this->writeData($this->writeDefinedNameBiff8(pack('C', 0x07), $formulaData, $i + 1, true));
  603. // (exclusive) either repeatColumns or repeatRows
  604. } elseif ($sheetSetup->isColumnsToRepeatAtLeftSet() || $sheetSetup->isRowsToRepeatAtTopSet()) {
  605. // Columns to repeat
  606. if ($sheetSetup->isColumnsToRepeatAtLeftSet()) {
  607. $repeat = $sheetSetup->getColumnsToRepeatAtLeft();
  608. $colmin = PHPExcel_Cell::columnIndexFromString($repeat[0]) - 1;
  609. $colmax = PHPExcel_Cell::columnIndexFromString($repeat[1]) - 1;
  610. } else {
  611. $colmin = 0;
  612. $colmax = 255;
  613. }
  614. // Rows to repeat
  615. if ($sheetSetup->isRowsToRepeatAtTopSet()) {
  616. $repeat = $sheetSetup->getRowsToRepeatAtTop();
  617. $rowmin = $repeat[0] - 1;
  618. $rowmax = $repeat[1] - 1;
  619. } else {
  620. $rowmin = 0;
  621. $rowmax = 65535;
  622. }
  623. // construct formula data manually because parser does not recognize absolute 3d cell references
  624. $formulaData = pack('Cvvvvv', 0x3B, $i, $rowmin, $rowmax, $colmin, $colmax);
  625. // store the DEFINEDNAME record
  626. $chunk .= $this->writeData($this->writeDefinedNameBiff8(pack('C', 0x07), $formulaData, $i + 1, true));
  627. }
  628. }
  629. // write the print areas, if any
  630. for ($i = 0; $i < $total_worksheets; ++$i) {
  631. $sheetSetup = $this->phpExcel->getSheet($i)->getPageSetup();
  632. if ($sheetSetup->isPrintAreaSet()) {
  633. // Print area, e.g. A3:J6,H1:X20
  634. $printArea = PHPExcel_Cell::splitRange($sheetSetup->getPrintArea());
  635. $countPrintArea = count($printArea);
  636. $formulaData = '';
  637. for ($j = 0; $j < $countPrintArea; ++$j) {
  638. $printAreaRect = $printArea[$j]; // e.g. A3:J6
  639. $printAreaRect[0] = PHPExcel_Cell::coordinateFromString($printAreaRect[0]);
  640. $printAreaRect[1] = PHPExcel_Cell::coordinateFromString($printAreaRect[1]);
  641. $print_rowmin = $printAreaRect[0][1] - 1;
  642. $print_rowmax = $printAreaRect[1][1] - 1;
  643. $print_colmin = PHPExcel_Cell::columnIndexFromString($printAreaRect[0][0]) - 1;
  644. $print_colmax = PHPExcel_Cell::columnIndexFromString($printAreaRect[1][0]) - 1;
  645. // construct formula data manually because parser does not recognize absolute 3d cell references
  646. $formulaData .= pack('Cvvvvv', 0x3B, $i, $print_rowmin, $print_rowmax, $print_colmin, $print_colmax);
  647. if ($j > 0) {
  648. $formulaData .= pack('C', 0x10); // list operator token ','
  649. }
  650. }
  651. // store the DEFINEDNAME record
  652. $chunk .= $this->writeData($this->writeDefinedNameBiff8(pack('C', 0x06), $formulaData, $i + 1, true));
  653. }
  654. }
  655. // write autofilters, if any
  656. for ($i = 0; $i < $total_worksheets; ++$i) {
  657. $sheetAutoFilter = $this->phpExcel->getSheet($i)->getAutoFilter();
  658. $autoFilterRange = $sheetAutoFilter->getRange();
  659. if (!empty($autoFilterRange)) {
  660. $rangeBounds = PHPExcel_Cell::rangeBoundaries($autoFilterRange);
  661. //Autofilter built in name
  662. $name = pack('C', 0x0D);
  663. $chunk .= $this->writeData($this->writeShortNameBiff8($name, $i + 1, $rangeBounds, true));
  664. }
  665. }
  666. return $chunk;
  667. }
  668. /**
  669. * Write a DEFINEDNAME record for BIFF8 using explicit binary formula data
  670. *
  671. * @param string $name The name in UTF-8
  672. * @param string $formulaData The binary formula data
  673. * @param string $sheetIndex 1-based sheet index the defined name applies to. 0 = global
  674. * @param boolean $isBuiltIn Built-in name?
  675. * @return string Complete binary record data
  676. */
  677. private function writeDefinedNameBiff8($name, $formulaData, $sheetIndex = 0, $isBuiltIn = false)
  678. {
  679. $record = 0x0018;
  680. // option flags
  681. $options = $isBuiltIn ? 0x20 : 0x00;
  682. // length of the name, character count
  683. $nlen = PHPExcel_Shared_String::CountCharacters($name);
  684. // name with stripped length field
  685. $name = substr(PHPExcel_Shared_String::UTF8toBIFF8UnicodeLong($name), 2);
  686. // size of the formula (in bytes)
  687. $sz = strlen($formulaData);
  688. // combine the parts
  689. $data = pack('vCCvvvCCCC', $options, 0, $nlen, $sz, 0, $sheetIndex, 0, 0, 0, 0)
  690. . $name . $formulaData;
  691. $length = strlen($data);
  692. $header = pack('vv', $record, $length);
  693. return $header . $data;
  694. }
  695. /**
  696. * Write a short NAME record
  697. *
  698. * @param string $name
  699. * @param string $sheetIndex 1-based sheet index the defined name applies to. 0 = global
  700. * @param integer[][] $rangeBounds range boundaries
  701. * @param boolean $isHidden
  702. * @return string Complete binary record data
  703. * */
  704. private function writeShortNameBiff8($name, $sheetIndex = 0, $rangeBounds, $isHidden = false)
  705. {
  706. $record = 0x0018;
  707. // option flags
  708. $options = ($isHidden ? 0x21 : 0x00);
  709. $extra = pack(
  710. 'Cvvvvv',
  711. 0x3B,
  712. $sheetIndex - 1,
  713. $rangeBounds[0][1] - 1,
  714. $rangeBounds[1][1] - 1,
  715. $rangeBounds[0][0] - 1,
  716. $rangeBounds[1][0] - 1
  717. );
  718. // size of the formula (in bytes)
  719. $sz = strlen($extra);
  720. // combine the parts
  721. $data = pack('vCCvvvCCCCC', $options, 0, 1, $sz, 0, $sheetIndex, 0, 0, 0, 0, 0)
  722. . $name . $extra;
  723. $length = strlen($data);
  724. $header = pack('vv', $record, $length);
  725. return $header . $data;
  726. }
  727. /**
  728. * Stores the CODEPAGE biff record.
  729. */
  730. private function writeCodepage()
  731. {
  732. $record = 0x0042; // Record identifier
  733. $length = 0x0002; // Number of bytes to follow
  734. $cv = $this->codepage; // The code page
  735. $header = pack('vv', $record, $length);
  736. $data = pack('v', $cv);
  737. $this->append($header . $data);
  738. }
  739. /**
  740. * Write Excel BIFF WINDOW1 record.
  741. */
  742. private function writeWindow1()
  743. {
  744. $record = 0x003D; // Record identifier
  745. $length = 0x0012; // Number of bytes to follow
  746. $xWn = 0x0000; // Horizontal position of window
  747. $yWn = 0x0000; // Vertical position of window
  748. $dxWn = 0x25BC; // Width of window
  749. $dyWn = 0x1572; // Height of window
  750. $grbit = 0x0038; // Option flags
  751. // not supported by PHPExcel, so there is only one selected sheet, the active
  752. $ctabsel = 1; // Number of workbook tabs selected
  753. $wTabRatio = 0x0258; // Tab to scrollbar ratio
  754. // not supported by PHPExcel, set to 0
  755. $itabFirst = 0; // 1st displayed worksheet
  756. $itabCur = $this->phpExcel->getActiveSheetIndex(); // Active worksheet
  757. $header = pack("vv", $record, $length);
  758. $data = pack("vvvvvvvvv", $xWn, $yWn, $dxWn, $dyWn, $grbit, $itabCur, $itabFirst, $ctabsel, $wTabRatio);
  759. $this->append($header . $data);
  760. }
  761. /**
  762. * Writes Excel BIFF BOUNDSHEET record.
  763. *
  764. * @param PHPExcel_Worksheet $sheet Worksheet name
  765. * @param integer $offset Location of worksheet BOF
  766. */
  767. private function writeBoundSheet($sheet, $offset)
  768. {
  769. $sheetname = $sheet->getTitle();
  770. $record = 0x0085; // Record identifier
  771. // sheet state
  772. switch ($sheet->getSheetState()) {
  773. case PHPExcel_Worksheet::SHEETSTATE_VISIBLE:
  774. $ss = 0x00;
  775. break;
  776. case PHPExcel_Worksheet::SHEETSTATE_HIDDEN:
  777. $ss = 0x01;
  778. break;
  779. case PHPExcel_Worksheet::SHEETSTATE_VERYHIDDEN:
  780. $ss = 0x02;
  781. break;
  782. default:
  783. $ss = 0x00;
  784. break;
  785. }
  786. // sheet type
  787. $st = 0x00;
  788. $grbit = 0x0000; // Visibility and sheet type
  789. $data = pack("VCC", $offset, $ss, $st);
  790. $data .= PHPExcel_Shared_String::UTF8toBIFF8UnicodeShort($sheetname);
  791. $length = strlen($data);
  792. $header = pack("vv", $record, $length);
  793. $this->append($header . $data);
  794. }
  795. /**
  796. * Write Internal SUPBOOK record
  797. */
  798. private function writeSupbookInternal()
  799. {
  800. $record = 0x01AE; // Record identifier
  801. $length = 0x0004; // Bytes to follow
  802. $header = pack("vv", $record, $length);
  803. $data = pack("vv", $this->phpExcel->getSheetCount(), 0x0401);
  804. return $this->writeData($header . $data);
  805. }
  806. /**
  807. * Writes the Excel BIFF EXTERNSHEET record. These references are used by
  808. * formulas.
  809. *
  810. */
  811. private function writeExternalsheetBiff8()
  812. {
  813. $totalReferences = count($this->parser->references);
  814. $record = 0x0017; // Record identifier
  815. $length = 2 + 6 * $totalReferences; // Number of bytes to follow
  816. $supbook_index = 0; // FIXME: only using internal SUPBOOK record
  817. $header = pack("vv", $record, $length);
  818. $data = pack('v', $totalReferences);
  819. for ($i = 0; $i < $totalReferences; ++$i) {
  820. $data .= $this->parser->references[$i];
  821. }
  822. return $this->writeData($header . $data);
  823. }
  824. /**
  825. * Write Excel BIFF STYLE records.
  826. */
  827. private function writeStyle()
  828. {
  829. $record = 0x0293; // Record identifier
  830. $length = 0x0004; // Bytes to follow
  831. $ixfe = 0x8000; // Index to cell style XF
  832. $BuiltIn = 0x00; // Built-in style
  833. $iLevel = 0xff; // Outline style level
  834. $header = pack("vv", $record, $length);
  835. $data = pack("vCC", $ixfe, $BuiltIn, $iLevel);
  836. $this->append($header . $data);
  837. }
  838. /**
  839. * Writes Excel FORMAT record for non "built-in" numerical formats.
  840. *
  841. * @param string $format Custom format string
  842. * @param integer $ifmt Format index code
  843. */
  844. private function writeNumberFormat($format, $ifmt)
  845. {
  846. $record = 0x041E; // Record identifier
  847. $numberFormatString = PHPExcel_Shared_String::UTF8toBIFF8UnicodeLong($format);
  848. $length = 2 + strlen($numberFormatString); // Number of bytes to follow
  849. $header = pack("vv", $record, $length);
  850. $data = pack("v", $ifmt) . $numberFormatString;
  851. $this->append($header . $data);
  852. }
  853. /**
  854. * Write DATEMODE record to indicate the date system in use (1904 or 1900).
  855. */
  856. private function writeDateMode()
  857. {
  858. $record = 0x0022; // Record identifier
  859. $length = 0x0002; // Bytes to follow
  860. $f1904 = (PHPExcel_Shared_Date::getExcelCalendar() == PHPExcel_Shared_Date::CALENDAR_MAC_1904)
  861. ? 1
  862. : 0; // Flag for 1904 date system
  863. $header = pack("vv", $record, $length);
  864. $data = pack("v", $f1904);
  865. $this->append($header . $data);
  866. }
  867. /**
  868. * Write BIFF record EXTERNCOUNT to indicate the number of external sheet
  869. * references in the workbook.
  870. *
  871. * Excel only stores references to external sheets that are used in NAME.
  872. * The workbook NAME record is required to define the print area and the repeat
  873. * rows and columns.
  874. *
  875. * A similar method is used in Worksheet.php for a slightly different purpose.
  876. *
  877. * @param integer $cxals Number of external references
  878. */
  879. private function writeExternalCount($cxals)
  880. {
  881. $record = 0x0016; // Record identifier
  882. $length = 0x0002; // Number of bytes to follow
  883. $header = pack("vv", $record, $length);
  884. $data = pack("v", $cxals);
  885. $this->append($header . $data);
  886. }
  887. /**
  888. * Writes the Excel BIFF EXTERNSHEET record. These references are used by
  889. * formulas. NAME record is required to define the print area and the repeat
  890. * rows and columns.
  891. *
  892. * A similar method is used in Worksheet.php for a slightly different purpose.
  893. *
  894. * @param string $sheetname Worksheet name
  895. */
  896. private function writeExternalSheet($sheetname)
  897. {
  898. $record = 0x0017; // Record identifier
  899. $length = 0x02 + strlen($sheetname); // Number of bytes to follow
  900. $cch = strlen($sheetname); // Length of sheet name
  901. $rgch = 0x03; // Filename encoding
  902. $header = pack("vv", $record, $length);
  903. $data = pack("CC", $cch, $rgch);
  904. $this->append($header . $data . $sheetname);
  905. }
  906. /**
  907. * Store the NAME record in the short format that is used for storing the print
  908. * area, repeat rows only and repeat columns only.
  909. *
  910. * @param integer $index Sheet index
  911. * @param integer $type Built-in name type
  912. * @param integer $rowmin Start row
  913. * @param integer $rowmax End row
  914. * @param integer $colmin Start colum
  915. * @param integer $colmax End column
  916. */
  917. private function writeNameShort($index, $type, $rowmin, $rowmax, $colmin, $colmax)
  918. {
  919. $record = 0x0018; // Record identifier
  920. $length = 0x0024; // Number of bytes to follow
  921. $grbit = 0x0020; // Option flags
  922. $chKey = 0x00; // Keyboard shortcut
  923. $cch = 0x01; // Length of text name
  924. $cce = 0x0015; // Length of text definition
  925. $ixals = $index + 1; // Sheet index
  926. $itab = $ixals; // Equal to ixals
  927. $cchCustMenu = 0x00; // Length of cust menu text
  928. $cchDescription = 0x00; // Length of description text
  929. $cchHelptopic = 0x00; // Length of help topic text
  930. $cchStatustext = 0x00; // Length of status bar text
  931. $rgch = $type; // Built-in name type
  932. $unknown03 = 0x3b;
  933. $unknown04 = 0xffff - $index;
  934. $unknown05 = 0x0000;
  935. $unknown06 = 0x0000;
  936. $unknown07 = 0x1087;
  937. $unknown08 = 0x8005;
  938. $header = pack("vv", $record, $length);
  939. $data = pack("v", $grbit);
  940. $data .= pack("C", $chKey);
  941. $data .= pack("C", $cch);
  942. $data .= pack("v", $cce);
  943. $data .= pack("v", $ixals);
  944. $data .= pack("v", $itab);
  945. $data .= pack("C", $cchCustMenu);
  946. $data .= pack("C", $cchDescription);
  947. $data .= pack("C", $cchHelptopic);
  948. $data .= pack("C", $cchStatustext);
  949. $data .= pack("C", $rgch);
  950. $data .= pack("C", $unknown03);
  951. $data .= pack("v", $unknown04);
  952. $data .= pack("v", $unknown05);
  953. $data .= pack("v", $unknown06);
  954. $data .= pack("v", $unknown07);
  955. $data .= pack("v", $unknown08);
  956. $data .= pack("v", $index);
  957. $data .= pack("v", $index);
  958. $data .= pack("v", $rowmin);
  959. $data .= pack("v", $rowmax);
  960. $data .= pack("C", $colmin);
  961. $data .= pack("C", $colmax);
  962. $this->append($header . $data);
  963. }
  964. /**
  965. * Store the NAME record in the long format that is used for storing the repeat
  966. * rows and columns when both are specified. This shares a lot of code with
  967. * writeNameShort() but we use a separate method to keep the code clean.
  968. * Code abstraction for reuse can be carried too far, and I should know. ;-)
  969. *
  970. * @param integer $index Sheet index
  971. * @param integer $type Built-in name type
  972. * @param integer $rowmin Start row
  973. * @param integer $rowmax End row
  974. * @param integer $colmin Start colum
  975. * @param integer $colmax End column
  976. */
  977. private function writeNameLong($index, $type, $rowmin, $rowmax, $colmin, $colmax)
  978. {
  979. $record = 0x0018; // Record identifier
  980. $length = 0x003d; // Number of bytes to follow
  981. $grbit = 0x0020; // Option flags
  982. $chKey = 0x00; // Keyboard shortcut
  983. $cch = 0x01; // Length of text name
  984. $cce = 0x002e; // Length of text definition
  985. $ixals = $index + 1; // Sheet index
  986. $itab = $ixals; // Equal to ixals
  987. $cchCustMenu = 0x00; // Length of cust menu text
  988. $cchDescription = 0x00; // Length of description text
  989. $cchHelptopic = 0x00; // Length of help topic text
  990. $cchStatustext = 0x00; // Length of status bar text
  991. $rgch = $type; // Built-in name type
  992. $unknown01 = 0x29;
  993. $unknown02 = 0x002b;
  994. $unknown03 = 0x3b;
  995. $unknown04 = 0xffff-$index;
  996. $unknown05 = 0x0000;
  997. $unknown06 = 0x0000;
  998. $unknown07 = 0x1087;
  999. $unknown08 = 0x8008;
  1000. $header = pack("vv", $record, $length);
  1001. $data = pack("v", $grbit);
  1002. $data .= pack("C", $chKey);
  1003. $data .= pack("C", $cch);
  1004. $data .= pack("v", $cce);
  1005. $data .= pack("v", $ixals);
  1006. $data .= pack("v", $itab);
  1007. $data .= pack("C", $cchCustMenu);
  1008. $data .= pack("C", $cchDescription);
  1009. $data .= pack("C", $cchHelptopic);
  1010. $data .= pack("C", $cchStatustext);
  1011. $data .= pack("C", $rgch);
  1012. $data .= pack("C", $unknown01);
  1013. $data .= pack("v", $unknown02);
  1014. // Column definition
  1015. $data .= pack("C", $unknown03);
  1016. $data .= pack("v", $unknown04);
  1017. $data .= pack("v", $unknown05);
  1018. $data .= pack("v", $unknown06);
  1019. $data .= pack("v", $unknown07);
  1020. $data .= pack("v", $unknown08);
  1021. $data .= pack("v", $index);
  1022. $data .= pack("v", $index);
  1023. $data .= pack("v", 0x0000);
  1024. $data .= pack("v", 0x3fff);
  1025. $data .= pack("C", $colmin);
  1026. $data .= pack("C", $colmax);
  1027. // Row definition
  1028. $data .= pack("C", $unknown03);
  1029. $data .= pack("v", $unknown04);
  1030. $data .= pack("v", $unknown05);
  1031. $data .= pack("v", $unknown06);
  1032. $data .= pack("v", $unknown07);
  1033. $data .= pack("v", $unknown08);
  1034. $data .= pack("v", $index);
  1035. $data .= pack("v", $index);
  1036. $data .= pack("v", $rowmin);
  1037. $data .= pack("v", $rowmax);
  1038. $data .= pack("C", 0x00);
  1039. $data .= pack("C", 0xff);
  1040. // End of data
  1041. $data .= pack("C", 0x10);
  1042. $this->append($header . $data);
  1043. }
  1044. /**
  1045. * Stores the COUNTRY record for localization
  1046. *
  1047. * @return string
  1048. */
  1049. private function writeCountry()
  1050. {
  1051. $record = 0x008C; // Record identifier
  1052. $length = 4; // Number of bytes to follow
  1053. $header = pack('vv', $record, $length);
  1054. /* using the same country code always for simplicity */
  1055. $data = pack('vv', $this->countryCode, $this->countryCode);
  1056. //$this->append($header . $data);
  1057. return $this->writeData($header . $data);
  1058. }
  1059. /**
  1060. * Write the RECALCID record
  1061. *
  1062. * @return string
  1063. */
  1064. private function writeRecalcId()
  1065. {
  1066. $record = 0x01C1; // Record identifier
  1067. $length = 8; // Number of bytes to follow
  1068. $header = pack('vv', $record, $length);
  1069. // by inspection of real Excel files, MS Office Excel 2007 writes this
  1070. $data = pack('VV', 0x000001C1, 0x00001E667);
  1071. return $this->writeData($header . $data);
  1072. }
  1073. /**
  1074. * Stores the PALETTE biff record.
  1075. */
  1076. private function writePalette()
  1077. {
  1078. $aref = $this->palette;
  1079. $record = 0x0092; // Record identifier
  1080. $length = 2 + 4 * count($aref); // Number of bytes to follow
  1081. $ccv = count($aref); // Number of RGB values to follow
  1082. $data = ''; // The RGB data
  1083. // Pack the RGB data
  1084. foreach ($aref as $color) {
  1085. foreach ($color as $byte) {
  1086. $data .= pack("C", $byte);
  1087. }
  1088. }
  1089. $header = pack("vvv", $record, $length, $ccv);
  1090. $this->append($header . $data);
  1091. }
  1092. /**
  1093. * Handling of the SST continue blocks is complicated by the need to include an
  1094. * additional continuation byte depending on whether the string is split between
  1095. * blocks or whether it starts at the beginning of the block. (There are also
  1096. * additional complications that will arise later when/if Rich Strings are
  1097. * supported).
  1098. *
  1099. * The Excel documentation says that the SST record should be followed by an
  1100. * EXTSST record. The EXTSST record is a hash table that is used to optimise
  1101. * access to SST. However, despite the documentation it doesn't seem to be
  1102. * required so we will ignore it.
  1103. *
  1104. * @return string Binary data
  1105. */
  1106. private function writeSharedStringsTable()
  1107. {
  1108. // maximum size of record data (excluding record header)
  1109. $continue_limit = 8224;
  1110. // initialize array of record data blocks
  1111. $recordDatas = array();
  1112. // start SST record data block with total number of strings, total number of unique strings
  1113. $recordData = pack("VV", $this->stringTotal, $this->stringUnique);
  1114. // loop through all (unique) strings in shared strings table
  1115. foreach (array_keys($this->stringTable) as $string) {
  1116. // here $string is a BIFF8 encoded string
  1117. // length = character count
  1118. $headerinfo = unpack("vlength/Cencoding", $string);
  1119. // currently, this is always 1 = uncompressed
  1120. $encoding = $headerinfo["encoding"];
  1121. // initialize finished writing current $string
  1122. $finished = false;
  1123. while ($finished === false) {
  1124. // normally, there will be only one cycle, but if string cannot immediately be written as is
  1125. // there will be need for more than one cylcle, if string longer than one record data block, there
  1126. // may be need for even more cycles
  1127. if (strlen($recordData) + strlen($string) <= $continue_limit) {
  1128. // then we can write the string (or remainder of string) without any problems
  1129. $recordData .= $string;
  1130. if (strlen($recordData) + strlen($string) == $continue_limit) {
  1131. // we close the record data block, and initialize a new one
  1132. $recordDatas[] = $recordData;
  1133. $recordData = '';
  1134. }
  1135. // we are finished writing this string
  1136. $finished = true;
  1137. } else {
  1138. // special treatment writing the string (or remainder of the string)
  1139. // If the string is very long it may need to be written in more than one CONTINUE record.
  1140. // check how many bytes more there is room for in the current record
  1141. $space_remaining = $continue_limit - strlen($recordData);
  1142. // minimum space needed
  1143. // uncompressed: 2 byte string length length field + 1 byte option flags + 2 byte character
  1144. // compressed: 2 byte string length length field + 1 byte option flags + 1 byte character
  1145. $min_space_needed = ($encoding == 1) ? 5 : 4;
  1146. // We have two cases
  1147. // 1. space remaining is less than minimum space needed
  1148. // here we must waste the space remaining and move to next record data block
  1149. // 2. space remaining is greater than or equal to minimum space needed
  1150. // here we write as much as we can in the current block, then move to next record data block
  1151. // 1. space remaining is less than minimum space needed
  1152. if ($space_remaining < $min_space_needed) {
  1153. // we close the block, store the block data
  1154. $recordDatas[] = $recordData;
  1155. // and start new record data block where we start writing the string
  1156. $recordData = '';
  1157. // 2. space remaining is greater than or equal to minimum space needed
  1158. } else {
  1159. // initialize effective remaining space, for Unicode strings this may need to be reduced by 1, see below
  1160. $effective_space_remaining = $space_remaining;
  1161. // for uncompressed strings, sometimes effective space remaining is reduced by 1
  1162. if ($encoding == 1 && (strlen($string) - $space_remaining) % 2 == 1) {
  1163. --$effective_space_remaining;
  1164. }
  1165. // one block fininshed, store the block data
  1166. $recordData .= substr($string, 0, $effective_space_remaining);
  1167. $string = substr($string, $effective_space_remaining); // for next cycle in while loop
  1168. $recordDatas[] = $recordData;
  1169. // start new record data block with the repeated option flags
  1170. $recordData = pack('C', $encoding);
  1171. }
  1172. }
  1173. }
  1174. }
  1175. // Store the last record data block unless it is empty
  1176. // if there was no need for any continue records, this will be the for SST record data block itself
  1177. if (strlen($recordData) > 0) {
  1178. $recordDatas[] = $recordData;
  1179. }
  1180. // combine into one chunk with all the blocks SST, CONTINUE,...
  1181. $chunk = '';
  1182. foreach ($recordDatas as $i => $recordData) {
  1183. // first block should have the SST record header, remaing should have CONTINUE header
  1184. $record = ($i == 0) ? 0x00FC : 0x003C;
  1185. $header = pack("vv", $record, strlen($recordData));
  1186. $data = $header . $recordData;
  1187. $chunk .= $this->writeData($data);
  1188. }
  1189. return $chunk;
  1190. }
  1191. /**
  1192. * Writes the MSODRAWINGGROUP record if needed. Possibly split using CONTINUE records.
  1193. */
  1194. private function writeMsoDrawingGroup()
  1195. {
  1196. // write the Escher stream if necessary
  1197. if (isset($this->escher)) {
  1198. $writer = new PHPExcel_Writer_Excel5_Escher($this->escher);
  1199. $data = $writer->close();
  1200. $record = 0x00EB;
  1201. $length = strlen($data);
  1202. $header = pack("vv", $record, $length);
  1203. return $this->writeData($header . $data);
  1204. } else {
  1205. return '';
  1206. }
  1207. }
  1208. /**
  1209. * Get Escher object
  1210. *
  1211. * @return PHPExcel_Shared_Escher
  1212. */
  1213. public function getEscher()
  1214. {
  1215. return $this->escher;
  1216. }
  1217. /**
  1218. * Set Escher object
  1219. *
  1220. * @param PHPExcel_Shared_Escher $pValue
  1221. */
  1222. public function setEscher(PHPExcel_Shared_Escher $pValue = null)
  1223. {
  1224. $this->escher = $pValue;
  1225. }
  1226. }