Excel5.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2013 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 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.9, 2013-06-02
  26. */
  27. /**
  28. * PHPExcel_Writer_Excel5
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Writer_Excel5
  32. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Writer_Excel5 extends PHPExcel_Writer_Abstract implements PHPExcel_Writer_IWriter
  35. {
  36. /**
  37. * PHPExcel object
  38. *
  39. * @var PHPExcel
  40. */
  41. private $_phpExcel;
  42. /**
  43. * Total number of shared strings in workbook
  44. *
  45. * @var int
  46. */
  47. private $_str_total = 0;
  48. /**
  49. * Number of unique shared strings in workbook
  50. *
  51. * @var int
  52. */
  53. private $_str_unique = 0;
  54. /**
  55. * Array of unique shared strings in workbook
  56. *
  57. * @var array
  58. */
  59. private $_str_table = array();
  60. /**
  61. * Color cache. Mapping between RGB value and color index.
  62. *
  63. * @var array
  64. */
  65. private $_colors;
  66. /**
  67. * Formula parser
  68. *
  69. * @var PHPExcel_Writer_Excel5_Parser
  70. */
  71. private $_parser;
  72. /**
  73. * Identifier clusters for drawings. Used in MSODRAWINGGROUP record.
  74. *
  75. * @var array
  76. */
  77. private $_IDCLs;
  78. /**
  79. * Basic OLE object summary information
  80. *
  81. * @var array
  82. */
  83. private $_summaryInformation;
  84. /**
  85. * Extended OLE object document summary information
  86. *
  87. * @var array
  88. */
  89. private $_documentSummaryInformation;
  90. /**
  91. * Create a new PHPExcel_Writer_Excel5
  92. *
  93. * @param PHPExcel $phpExcel PHPExcel object
  94. */
  95. public function __construct(PHPExcel $phpExcel) {
  96. $this->_phpExcel = $phpExcel;
  97. $this->_parser = new PHPExcel_Writer_Excel5_Parser();
  98. }
  99. /**
  100. * Save PHPExcel to file
  101. *
  102. * @param string $pFilename
  103. * @throws PHPExcel_Writer_Exception
  104. */
  105. public function save($pFilename = null) {
  106. // garbage collect
  107. $this->_phpExcel->garbageCollect();
  108. $saveDebugLog = PHPExcel_Calculation::getInstance($this->_phpExcel)->getDebugLog()->getWriteDebugLog();
  109. PHPExcel_Calculation::getInstance($this->_phpExcel)->getDebugLog()->setWriteDebugLog(FALSE);
  110. $saveDateReturnType = PHPExcel_Calculation_Functions::getReturnDateType();
  111. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
  112. // initialize colors array
  113. $this->_colors = array();
  114. // Initialise workbook writer
  115. $this->_writerWorkbook = new PHPExcel_Writer_Excel5_Workbook($this->_phpExcel,
  116. $this->_str_total, $this->_str_unique, $this->_str_table,
  117. $this->_colors, $this->_parser);
  118. // Initialise worksheet writers
  119. $countSheets = $this->_phpExcel->getSheetCount();
  120. for ($i = 0; $i < $countSheets; ++$i) {
  121. $this->_writerWorksheets[$i] = new PHPExcel_Writer_Excel5_Worksheet($this->_str_total, $this->_str_unique,
  122. $this->_str_table, $this->_colors,
  123. $this->_parser,
  124. $this->_preCalculateFormulas,
  125. $this->_phpExcel->getSheet($i));
  126. }
  127. // build Escher objects. Escher objects for workbooks needs to be build before Escher object for workbook.
  128. $this->_buildWorksheetEschers();
  129. $this->_buildWorkbookEscher();
  130. // add 15 identical cell style Xfs
  131. // for now, we use the first cellXf instead of cellStyleXf
  132. $cellXfCollection = $this->_phpExcel->getCellXfCollection();
  133. for ($i = 0; $i < 15; ++$i) {
  134. $this->_writerWorkbook->addXfWriter($cellXfCollection[0], true);
  135. }
  136. // add all the cell Xfs
  137. foreach ($this->_phpExcel->getCellXfCollection() as $style) {
  138. $this->_writerWorkbook->addXfWriter($style, false);
  139. }
  140. // add font from rich text eleemnts
  141. for ($i = 0; $i < $countSheets; ++$i) {
  142. foreach ($this->_writerWorksheets[$i]->_phpSheet->getCellCollection() as $cellID) {
  143. $cell = $this->_writerWorksheets[$i]->_phpSheet->getCell($cellID);
  144. $cVal = $cell->getValue();
  145. if ($cVal instanceof PHPExcel_RichText) {
  146. $elements = $cVal->getRichTextElements();
  147. foreach ($elements as $element) {
  148. if ($element instanceof PHPExcel_RichText_Run) {
  149. $font = $element->getFont();
  150. $this->_writerWorksheets[$i]->_fntHashIndex[$font->getHashCode()] = $this->_writerWorkbook->_addFont($font);
  151. }
  152. }
  153. }
  154. }
  155. }
  156. // initialize OLE file
  157. $workbookStreamName = 'Workbook';
  158. $OLE = new PHPExcel_Shared_OLE_PPS_File(PHPExcel_Shared_OLE::Asc2Ucs($workbookStreamName));
  159. // Write the worksheet streams before the global workbook stream,
  160. // because the byte sizes of these are needed in the global workbook stream
  161. $worksheetSizes = array();
  162. for ($i = 0; $i < $countSheets; ++$i) {
  163. $this->_writerWorksheets[$i]->close();
  164. $worksheetSizes[] = $this->_writerWorksheets[$i]->_datasize;
  165. }
  166. // add binary data for global workbook stream
  167. $OLE->append($this->_writerWorkbook->writeWorkbook($worksheetSizes));
  168. // add binary data for sheet streams
  169. for ($i = 0; $i < $countSheets; ++$i) {
  170. $OLE->append($this->_writerWorksheets[$i]->getData());
  171. }
  172. $this->_documentSummaryInformation = $this->_writeDocumentSummaryInformation();
  173. // initialize OLE Document Summary Information
  174. if(isset($this->_documentSummaryInformation) && !empty($this->_documentSummaryInformation)){
  175. $OLE_DocumentSummaryInformation = new PHPExcel_Shared_OLE_PPS_File(PHPExcel_Shared_OLE::Asc2Ucs(chr(5) . 'DocumentSummaryInformation'));
  176. $OLE_DocumentSummaryInformation->append($this->_documentSummaryInformation);
  177. }
  178. $this->_summaryInformation = $this->_writeSummaryInformation();
  179. // initialize OLE Summary Information
  180. if(isset($this->_summaryInformation) && !empty($this->_summaryInformation)){
  181. $OLE_SummaryInformation = new PHPExcel_Shared_OLE_PPS_File(PHPExcel_Shared_OLE::Asc2Ucs(chr(5) . 'SummaryInformation'));
  182. $OLE_SummaryInformation->append($this->_summaryInformation);
  183. }
  184. // define OLE Parts
  185. $arrRootData = array($OLE);
  186. // initialize OLE Properties file
  187. if(isset($OLE_SummaryInformation)){
  188. $arrRootData[] = $OLE_SummaryInformation;
  189. }
  190. // initialize OLE Extended Properties file
  191. if(isset($OLE_DocumentSummaryInformation)){
  192. $arrRootData[] = $OLE_DocumentSummaryInformation;
  193. }
  194. $root = new PHPExcel_Shared_OLE_PPS_Root(time(), time(), $arrRootData);
  195. // save the OLE file
  196. $res = $root->save($pFilename);
  197. PHPExcel_Calculation_Functions::setReturnDateType($saveDateReturnType);
  198. PHPExcel_Calculation::getInstance($this->_phpExcel)->getDebugLog()->setWriteDebugLog($saveDebugLog);
  199. }
  200. /**
  201. * Set temporary storage directory
  202. *
  203. * @deprecated
  204. * @param string $pValue Temporary storage directory
  205. * @throws PHPExcel_Writer_Exception when directory does not exist
  206. * @return PHPExcel_Writer_Excel5
  207. */
  208. public function setTempDir($pValue = '') {
  209. return $this;
  210. }
  211. /**
  212. * Build the Worksheet Escher objects
  213. *
  214. */
  215. private function _buildWorksheetEschers()
  216. {
  217. // 1-based index to BstoreContainer
  218. $blipIndex = 0;
  219. $lastReducedSpId = 0;
  220. $lastSpId = 0;
  221. foreach ($this->_phpExcel->getAllsheets() as $sheet) {
  222. // sheet index
  223. $sheetIndex = $sheet->getParent()->getIndex($sheet);
  224. $escher = null;
  225. // check if there are any shapes for this sheet
  226. $filterRange = $sheet->getAutoFilter()->getRange();
  227. if (count($sheet->getDrawingCollection()) == 0 && empty($filterRange)) {
  228. continue;
  229. }
  230. // create intermediate Escher object
  231. $escher = new PHPExcel_Shared_Escher();
  232. // dgContainer
  233. $dgContainer = new PHPExcel_Shared_Escher_DgContainer();
  234. // set the drawing index (we use sheet index + 1)
  235. $dgId = $sheet->getParent()->getIndex($sheet) + 1;
  236. $dgContainer->setDgId($dgId);
  237. $escher->setDgContainer($dgContainer);
  238. // spgrContainer
  239. $spgrContainer = new PHPExcel_Shared_Escher_DgContainer_SpgrContainer();
  240. $dgContainer->setSpgrContainer($spgrContainer);
  241. // add one shape which is the group shape
  242. $spContainer = new PHPExcel_Shared_Escher_DgContainer_SpgrContainer_SpContainer();
  243. $spContainer->setSpgr(true);
  244. $spContainer->setSpType(0);
  245. $spContainer->setSpId(($sheet->getParent()->getIndex($sheet) + 1) << 10);
  246. $spgrContainer->addChild($spContainer);
  247. // add the shapes
  248. $countShapes[$sheetIndex] = 0; // count number of shapes (minus group shape), in sheet
  249. foreach ($sheet->getDrawingCollection() as $drawing) {
  250. ++$blipIndex;
  251. ++$countShapes[$sheetIndex];
  252. // add the shape
  253. $spContainer = new PHPExcel_Shared_Escher_DgContainer_SpgrContainer_SpContainer();
  254. // set the shape type
  255. $spContainer->setSpType(0x004B);
  256. // set the shape flag
  257. $spContainer->setSpFlag(0x02);
  258. // set the shape index (we combine 1-based sheet index and $countShapes to create unique shape index)
  259. $reducedSpId = $countShapes[$sheetIndex];
  260. $spId = $reducedSpId
  261. | ($sheet->getParent()->getIndex($sheet) + 1) << 10;
  262. $spContainer->setSpId($spId);
  263. // keep track of last reducedSpId
  264. $lastReducedSpId = $reducedSpId;
  265. // keep track of last spId
  266. $lastSpId = $spId;
  267. // set the BLIP index
  268. $spContainer->setOPT(0x4104, $blipIndex);
  269. // set coordinates and offsets, client anchor
  270. $coordinates = $drawing->getCoordinates();
  271. $offsetX = $drawing->getOffsetX();
  272. $offsetY = $drawing->getOffsetY();
  273. $width = $drawing->getWidth();
  274. $height = $drawing->getHeight();
  275. $twoAnchor = PHPExcel_Shared_Excel5::oneAnchor2twoAnchor($sheet, $coordinates, $offsetX, $offsetY, $width, $height);
  276. $spContainer->setStartCoordinates($twoAnchor['startCoordinates']);
  277. $spContainer->setStartOffsetX($twoAnchor['startOffsetX']);
  278. $spContainer->setStartOffsetY($twoAnchor['startOffsetY']);
  279. $spContainer->setEndCoordinates($twoAnchor['endCoordinates']);
  280. $spContainer->setEndOffsetX($twoAnchor['endOffsetX']);
  281. $spContainer->setEndOffsetY($twoAnchor['endOffsetY']);
  282. $spgrContainer->addChild($spContainer);
  283. }
  284. // AutoFilters
  285. if(!empty($filterRange)){
  286. $rangeBounds = PHPExcel_Cell::rangeBoundaries($filterRange);
  287. $iNumColStart = $rangeBounds[0][0];
  288. $iNumColEnd = $rangeBounds[1][0];
  289. $iInc = $iNumColStart;
  290. while($iInc <= $iNumColEnd){
  291. ++$countShapes[$sheetIndex];
  292. // create an Drawing Object for the dropdown
  293. $oDrawing = new PHPExcel_Worksheet_BaseDrawing();
  294. // get the coordinates of drawing
  295. $cDrawing = PHPExcel_Cell::stringFromColumnIndex($iInc - 1) . $rangeBounds[0][1];
  296. $oDrawing->setCoordinates($cDrawing);
  297. $oDrawing->setWorksheet($sheet);
  298. // add the shape
  299. $spContainer = new PHPExcel_Shared_Escher_DgContainer_SpgrContainer_SpContainer();
  300. // set the shape type
  301. $spContainer->setSpType(0x00C9);
  302. // set the shape flag
  303. $spContainer->setSpFlag(0x01);
  304. // set the shape index (we combine 1-based sheet index and $countShapes to create unique shape index)
  305. $reducedSpId = $countShapes[$sheetIndex];
  306. $spId = $reducedSpId
  307. | ($sheet->getParent()->getIndex($sheet) + 1) << 10;
  308. $spContainer->setSpId($spId);
  309. // keep track of last reducedSpId
  310. $lastReducedSpId = $reducedSpId;
  311. // keep track of last spId
  312. $lastSpId = $spId;
  313. $spContainer->setOPT(0x007F, 0x01040104); // Protection -> fLockAgainstGrouping
  314. $spContainer->setOPT(0x00BF, 0x00080008); // Text -> fFitTextToShape
  315. $spContainer->setOPT(0x01BF, 0x00010000); // Fill Style -> fNoFillHitTest
  316. $spContainer->setOPT(0x01FF, 0x00080000); // Line Style -> fNoLineDrawDash
  317. $spContainer->setOPT(0x03BF, 0x000A0000); // Group Shape -> fPrint
  318. // set coordinates and offsets, client anchor
  319. $endCoordinates = PHPExcel_Cell::stringFromColumnIndex(PHPExcel_Cell::stringFromColumnIndex($iInc - 1));
  320. $endCoordinates .= $rangeBounds[0][1] + 1;
  321. $spContainer->setStartCoordinates($cDrawing);
  322. $spContainer->setStartOffsetX(0);
  323. $spContainer->setStartOffsetY(0);
  324. $spContainer->setEndCoordinates($endCoordinates);
  325. $spContainer->setEndOffsetX(0);
  326. $spContainer->setEndOffsetY(0);
  327. $spgrContainer->addChild($spContainer);
  328. $iInc++;
  329. }
  330. }
  331. // identifier clusters, used for workbook Escher object
  332. $this->_IDCLs[$dgId] = $lastReducedSpId;
  333. // set last shape index
  334. $dgContainer->setLastSpId($lastSpId);
  335. // set the Escher object
  336. $this->_writerWorksheets[$sheetIndex]->setEscher($escher);
  337. }
  338. }
  339. /**
  340. * Build the Escher object corresponding to the MSODRAWINGGROUP record
  341. */
  342. private function _buildWorkbookEscher()
  343. {
  344. $escher = null;
  345. // any drawings in this workbook?
  346. $found = false;
  347. foreach ($this->_phpExcel->getAllSheets() as $sheet) {
  348. if (count($sheet->getDrawingCollection()) > 0) {
  349. $found = true;
  350. break;
  351. }
  352. }
  353. // nothing to do if there are no drawings
  354. if (!$found) {
  355. return;
  356. }
  357. // if we reach here, then there are drawings in the workbook
  358. $escher = new PHPExcel_Shared_Escher();
  359. // dggContainer
  360. $dggContainer = new PHPExcel_Shared_Escher_DggContainer();
  361. $escher->setDggContainer($dggContainer);
  362. // set IDCLs (identifier clusters)
  363. $dggContainer->setIDCLs($this->_IDCLs);
  364. // this loop is for determining maximum shape identifier of all drawing
  365. $spIdMax = 0;
  366. $totalCountShapes = 0;
  367. $countDrawings = 0;
  368. foreach ($this->_phpExcel->getAllsheets() as $sheet) {
  369. $sheetCountShapes = 0; // count number of shapes (minus group shape), in sheet
  370. if (count($sheet->getDrawingCollection()) > 0) {
  371. ++$countDrawings;
  372. foreach ($sheet->getDrawingCollection() as $drawing) {
  373. ++$sheetCountShapes;
  374. ++$totalCountShapes;
  375. $spId = $sheetCountShapes
  376. | ($this->_phpExcel->getIndex($sheet) + 1) << 10;
  377. $spIdMax = max($spId, $spIdMax);
  378. }
  379. }
  380. }
  381. $dggContainer->setSpIdMax($spIdMax + 1);
  382. $dggContainer->setCDgSaved($countDrawings);
  383. $dggContainer->setCSpSaved($totalCountShapes + $countDrawings); // total number of shapes incl. one group shapes per drawing
  384. // bstoreContainer
  385. $bstoreContainer = new PHPExcel_Shared_Escher_DggContainer_BstoreContainer();
  386. $dggContainer->setBstoreContainer($bstoreContainer);
  387. // the BSE's (all the images)
  388. foreach ($this->_phpExcel->getAllsheets() as $sheet) {
  389. foreach ($sheet->getDrawingCollection() as $drawing) {
  390. if ($drawing instanceof PHPExcel_Worksheet_Drawing) {
  391. $filename = $drawing->getPath();
  392. list($imagesx, $imagesy, $imageFormat) = getimagesize($filename);
  393. switch ($imageFormat) {
  394. case 1: // GIF, not supported by BIFF8, we convert to PNG
  395. $blipType = PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG;
  396. ob_start();
  397. imagepng(imagecreatefromgif($filename));
  398. $blipData = ob_get_contents();
  399. ob_end_clean();
  400. break;
  401. case 2: // JPEG
  402. $blipType = PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_JPEG;
  403. $blipData = file_get_contents($filename);
  404. break;
  405. case 3: // PNG
  406. $blipType = PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG;
  407. $blipData = file_get_contents($filename);
  408. break;
  409. case 6: // Windows DIB (BMP), we convert to PNG
  410. $blipType = PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG;
  411. ob_start();
  412. imagepng(PHPExcel_Shared_Drawing::imagecreatefrombmp($filename));
  413. $blipData = ob_get_contents();
  414. ob_end_clean();
  415. break;
  416. default: continue 2;
  417. }
  418. $blip = new PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE_Blip();
  419. $blip->setData($blipData);
  420. $BSE = new PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE();
  421. $BSE->setBlipType($blipType);
  422. $BSE->setBlip($blip);
  423. $bstoreContainer->addBSE($BSE);
  424. } else if ($drawing instanceof PHPExcel_Worksheet_MemoryDrawing) {
  425. switch ($drawing->getRenderingFunction()) {
  426. case PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG:
  427. $blipType = PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_JPEG;
  428. $renderingFunction = 'imagejpeg';
  429. break;
  430. case PHPExcel_Worksheet_MemoryDrawing::RENDERING_GIF:
  431. case PHPExcel_Worksheet_MemoryDrawing::RENDERING_PNG:
  432. case PHPExcel_Worksheet_MemoryDrawing::RENDERING_DEFAULT:
  433. $blipType = PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG;
  434. $renderingFunction = 'imagepng';
  435. break;
  436. }
  437. ob_start();
  438. call_user_func($renderingFunction, $drawing->getImageResource());
  439. $blipData = ob_get_contents();
  440. ob_end_clean();
  441. $blip = new PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE_Blip();
  442. $blip->setData($blipData);
  443. $BSE = new PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE();
  444. $BSE->setBlipType($blipType);
  445. $BSE->setBlip($blip);
  446. $bstoreContainer->addBSE($BSE);
  447. }
  448. }
  449. }
  450. // Set the Escher object
  451. $this->_writerWorkbook->setEscher($escher);
  452. }
  453. /**
  454. * Build the OLE Part for DocumentSummary Information
  455. * @return string
  456. */
  457. private function _writeDocumentSummaryInformation(){
  458. // offset: 0; size: 2; must be 0xFE 0xFF (UTF-16 LE byte order mark)
  459. $data = pack('v', 0xFFFE);
  460. // offset: 2; size: 2;
  461. $data .= pack('v', 0x0000);
  462. // offset: 4; size: 2; OS version
  463. $data .= pack('v', 0x0106);
  464. // offset: 6; size: 2; OS indicator
  465. $data .= pack('v', 0x0002);
  466. // offset: 8; size: 16
  467. $data .= pack('VVVV', 0x00, 0x00, 0x00, 0x00);
  468. // offset: 24; size: 4; section count
  469. $data .= pack('V', 0x0001);
  470. // offset: 28; size: 16; first section's class id: 02 d5 cd d5 9c 2e 1b 10 93 97 08 00 2b 2c f9 ae
  471. $data .= pack('vvvvvvvv', 0xD502, 0xD5CD, 0x2E9C, 0x101B, 0x9793, 0x0008, 0x2C2B, 0xAEF9);
  472. // offset: 44; size: 4; offset of the start
  473. $data .= pack('V', 0x30);
  474. // SECTION
  475. $dataSection = array();
  476. $dataSection_NumProps = 0;
  477. $dataSection_Summary = '';
  478. $dataSection_Content = '';
  479. // GKPIDDSI_CODEPAGE: CodePage
  480. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x01),
  481. 'offset' => array('pack' => 'V'),
  482. 'type' => array('pack' => 'V', 'data' => 0x02), // 2 byte signed integer
  483. 'data' => array('data' => 1252));
  484. $dataSection_NumProps++;
  485. // GKPIDDSI_CATEGORY : Category
  486. if($this->_phpExcel->getProperties()->getCategory()){
  487. $dataProp = $this->_phpExcel->getProperties()->getCategory();
  488. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x02),
  489. 'offset' => array('pack' => 'V'),
  490. 'type' => array('pack' => 'V', 'data' => 0x1E),
  491. 'data' => array('data' => $dataProp, 'length' => strlen($dataProp)));
  492. $dataSection_NumProps++;
  493. }
  494. // GKPIDDSI_VERSION :Version of the application that wrote the property storage
  495. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x17),
  496. 'offset' => array('pack' => 'V'),
  497. 'type' => array('pack' => 'V', 'data' => 0x03),
  498. 'data' => array('pack' => 'V', 'data' => 0x000C0000));
  499. $dataSection_NumProps++;
  500. // GKPIDDSI_SCALE : FALSE
  501. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x0B),
  502. 'offset' => array('pack' => 'V'),
  503. 'type' => array('pack' => 'V', 'data' => 0x0B),
  504. 'data' => array('data' => false));
  505. $dataSection_NumProps++;
  506. // GKPIDDSI_LINKSDIRTY : True if any of the values for the linked properties have changed outside of the application
  507. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x10),
  508. 'offset' => array('pack' => 'V'),
  509. 'type' => array('pack' => 'V', 'data' => 0x0B),
  510. 'data' => array('data' => false));
  511. $dataSection_NumProps++;
  512. // GKPIDDSI_SHAREDOC : FALSE
  513. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x13),
  514. 'offset' => array('pack' => 'V'),
  515. 'type' => array('pack' => 'V', 'data' => 0x0B),
  516. 'data' => array('data' => false));
  517. $dataSection_NumProps++;
  518. // GKPIDDSI_HYPERLINKSCHANGED : True if any of the values for the _PID_LINKS (hyperlink text) have changed outside of the application
  519. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x16),
  520. 'offset' => array('pack' => 'V'),
  521. 'type' => array('pack' => 'V', 'data' => 0x0B),
  522. 'data' => array('data' => false));
  523. $dataSection_NumProps++;
  524. // GKPIDDSI_DOCSPARTS
  525. // MS-OSHARED p75 (2.3.3.2.2.1)
  526. // Structure is VtVecUnalignedLpstrValue (2.3.3.1.9)
  527. // cElements
  528. $dataProp = pack('v', 0x0001);
  529. $dataProp .= pack('v', 0x0000);
  530. // array of UnalignedLpstr
  531. // cch
  532. $dataProp .= pack('v', 0x000A);
  533. $dataProp .= pack('v', 0x0000);
  534. // value
  535. $dataProp .= 'Worksheet'.chr(0);
  536. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x0D),
  537. 'offset' => array('pack' => 'V'),
  538. 'type' => array('pack' => 'V', 'data' => 0x101E),
  539. 'data' => array('data' => $dataProp, 'length' => strlen($dataProp)));
  540. $dataSection_NumProps++;
  541. // GKPIDDSI_HEADINGPAIR
  542. // VtVecHeadingPairValue
  543. // cElements
  544. $dataProp = pack('v', 0x0002);
  545. $dataProp .= pack('v', 0x0000);
  546. // Array of vtHeadingPair
  547. // vtUnalignedString - headingString
  548. // stringType
  549. $dataProp .= pack('v', 0x001E);
  550. // padding
  551. $dataProp .= pack('v', 0x0000);
  552. // UnalignedLpstr
  553. // cch
  554. $dataProp .= pack('v', 0x0013);
  555. $dataProp .= pack('v', 0x0000);
  556. // value
  557. $dataProp .= 'Feuilles de calcul';
  558. // vtUnalignedString - headingParts
  559. // wType : 0x0003 = 32 bit signed integer
  560. $dataProp .= pack('v', 0x0300);
  561. // padding
  562. $dataProp .= pack('v', 0x0000);
  563. // value
  564. $dataProp .= pack('v', 0x0100);
  565. $dataProp .= pack('v', 0x0000);
  566. $dataProp .= pack('v', 0x0000);
  567. $dataProp .= pack('v', 0x0000);
  568. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x0C),
  569. 'offset' => array('pack' => 'V'),
  570. 'type' => array('pack' => 'V', 'data' => 0x100C),
  571. 'data' => array('data' => $dataProp, 'length' => strlen($dataProp)));
  572. $dataSection_NumProps++;
  573. // 4 Section Length
  574. // 4 Property count
  575. // 8 * $dataSection_NumProps (8 = ID (4) + OffSet(4))
  576. $dataSection_Content_Offset = 8 + $dataSection_NumProps * 8;
  577. foreach ($dataSection as $dataProp){
  578. // Summary
  579. $dataSection_Summary .= pack($dataProp['summary']['pack'], $dataProp['summary']['data']);
  580. // Offset
  581. $dataSection_Summary .= pack($dataProp['offset']['pack'], $dataSection_Content_Offset);
  582. // DataType
  583. $dataSection_Content .= pack($dataProp['type']['pack'], $dataProp['type']['data']);
  584. // Data
  585. if($dataProp['type']['data'] == 0x02){ // 2 byte signed integer
  586. $dataSection_Content .= pack('V', $dataProp['data']['data']);
  587. $dataSection_Content_Offset += 4 + 4;
  588. }
  589. elseif($dataProp['type']['data'] == 0x03){ // 4 byte signed integer
  590. $dataSection_Content .= pack('V', $dataProp['data']['data']);
  591. $dataSection_Content_Offset += 4 + 4;
  592. }
  593. elseif($dataProp['type']['data'] == 0x0B){ // Boolean
  594. if($dataProp['data']['data'] == false){
  595. $dataSection_Content .= pack('V', 0x0000);
  596. } else {
  597. $dataSection_Content .= pack('V', 0x0001);
  598. }
  599. $dataSection_Content_Offset += 4 + 4;
  600. }
  601. elseif($dataProp['type']['data'] == 0x1E){ // null-terminated string prepended by dword string length
  602. // Null-terminated string
  603. $dataProp['data']['data'] .= chr(0);
  604. $dataProp['data']['length'] += 1;
  605. // Complete the string with null string for being a %4
  606. $dataProp['data']['length'] = $dataProp['data']['length'] + ((4 - $dataProp['data']['length'] % 4)==4 ? 0 : (4 - $dataProp['data']['length'] % 4));
  607. $dataProp['data']['data'] = str_pad($dataProp['data']['data'], $dataProp['data']['length'], chr(0), STR_PAD_RIGHT);
  608. $dataSection_Content .= pack('V', $dataProp['data']['length']);
  609. $dataSection_Content .= $dataProp['data']['data'];
  610. $dataSection_Content_Offset += 4 + 4 + strlen($dataProp['data']['data']);
  611. }
  612. elseif($dataProp['type']['data'] == 0x40){ // Filetime (64-bit value representing the number of 100-nanosecond intervals since January 1, 1601)
  613. $dataSection_Content .= $dataProp['data']['data'];
  614. $dataSection_Content_Offset += 4 + 8;
  615. }
  616. else {
  617. // Data Type Not Used at the moment
  618. $dataSection_Content .= $dataProp['data']['data'];
  619. $dataSection_Content_Offset += 4 + $dataProp['data']['length'];
  620. }
  621. }
  622. // Now $dataSection_Content_Offset contains the size of the content
  623. // section header
  624. // offset: $secOffset; size: 4; section length
  625. // + x Size of the content (summary + content)
  626. $data .= pack('V', $dataSection_Content_Offset);
  627. // offset: $secOffset+4; size: 4; property count
  628. $data .= pack('V', $dataSection_NumProps);
  629. // Section Summary
  630. $data .= $dataSection_Summary;
  631. // Section Content
  632. $data .= $dataSection_Content;
  633. return $data;
  634. }
  635. /**
  636. * Build the OLE Part for Summary Information
  637. * @return string
  638. */
  639. private function _writeSummaryInformation(){
  640. // offset: 0; size: 2; must be 0xFE 0xFF (UTF-16 LE byte order mark)
  641. $data = pack('v', 0xFFFE);
  642. // offset: 2; size: 2;
  643. $data .= pack('v', 0x0000);
  644. // offset: 4; size: 2; OS version
  645. $data .= pack('v', 0x0106);
  646. // offset: 6; size: 2; OS indicator
  647. $data .= pack('v', 0x0002);
  648. // offset: 8; size: 16
  649. $data .= pack('VVVV', 0x00, 0x00, 0x00, 0x00);
  650. // offset: 24; size: 4; section count
  651. $data .= pack('V', 0x0001);
  652. // offset: 28; size: 16; first section's class id: e0 85 9f f2 f9 4f 68 10 ab 91 08 00 2b 27 b3 d9
  653. $data .= pack('vvvvvvvv', 0x85E0, 0xF29F, 0x4FF9, 0x1068, 0x91AB, 0x0008, 0x272B, 0xD9B3);
  654. // offset: 44; size: 4; offset of the start
  655. $data .= pack('V', 0x30);
  656. // SECTION
  657. $dataSection = array();
  658. $dataSection_NumProps = 0;
  659. $dataSection_Summary = '';
  660. $dataSection_Content = '';
  661. // CodePage : CP-1252
  662. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x01),
  663. 'offset' => array('pack' => 'V'),
  664. 'type' => array('pack' => 'V', 'data' => 0x02), // 2 byte signed integer
  665. 'data' => array('data' => 1252));
  666. $dataSection_NumProps++;
  667. // Title
  668. if($this->_phpExcel->getProperties()->getTitle()){
  669. $dataProp = $this->_phpExcel->getProperties()->getTitle();
  670. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x02),
  671. 'offset' => array('pack' => 'V'),
  672. 'type' => array('pack' => 'V', 'data' => 0x1E), // null-terminated string prepended by dword string length
  673. 'data' => array('data' => $dataProp, 'length' => strlen($dataProp)));
  674. $dataSection_NumProps++;
  675. }
  676. // Subject
  677. if($this->_phpExcel->getProperties()->getSubject()){
  678. $dataProp = $this->_phpExcel->getProperties()->getSubject();
  679. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x03),
  680. 'offset' => array('pack' => 'V'),
  681. 'type' => array('pack' => 'V', 'data' => 0x1E), // null-terminated string prepended by dword string length
  682. 'data' => array('data' => $dataProp, 'length' => strlen($dataProp)));
  683. $dataSection_NumProps++;
  684. }
  685. // Author (Creator)
  686. if($this->_phpExcel->getProperties()->getCreator()){
  687. $dataProp = $this->_phpExcel->getProperties()->getCreator();
  688. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x04),
  689. 'offset' => array('pack' => 'V'),
  690. 'type' => array('pack' => 'V', 'data' => 0x1E), // null-terminated string prepended by dword string length
  691. 'data' => array('data' => $dataProp, 'length' => strlen($dataProp)));
  692. $dataSection_NumProps++;
  693. }
  694. // Keywords
  695. if($this->_phpExcel->getProperties()->getKeywords()){
  696. $dataProp = $this->_phpExcel->getProperties()->getKeywords();
  697. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x05),
  698. 'offset' => array('pack' => 'V'),
  699. 'type' => array('pack' => 'V', 'data' => 0x1E), // null-terminated string prepended by dword string length
  700. 'data' => array('data' => $dataProp, 'length' => strlen($dataProp)));
  701. $dataSection_NumProps++;
  702. }
  703. // Comments (Description)
  704. if($this->_phpExcel->getProperties()->getDescription()){
  705. $dataProp = $this->_phpExcel->getProperties()->getDescription();
  706. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x06),
  707. 'offset' => array('pack' => 'V'),
  708. 'type' => array('pack' => 'V', 'data' => 0x1E), // null-terminated string prepended by dword string length
  709. 'data' => array('data' => $dataProp, 'length' => strlen($dataProp)));
  710. $dataSection_NumProps++;
  711. }
  712. // Last Saved By (LastModifiedBy)
  713. if($this->_phpExcel->getProperties()->getLastModifiedBy()){
  714. $dataProp = $this->_phpExcel->getProperties()->getLastModifiedBy();
  715. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x08),
  716. 'offset' => array('pack' => 'V'),
  717. 'type' => array('pack' => 'V', 'data' => 0x1E), // null-terminated string prepended by dword string length
  718. 'data' => array('data' => $dataProp, 'length' => strlen($dataProp)));
  719. $dataSection_NumProps++;
  720. }
  721. // Created Date/Time
  722. if($this->_phpExcel->getProperties()->getCreated()){
  723. $dataProp = $this->_phpExcel->getProperties()->getCreated();
  724. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x0C),
  725. 'offset' => array('pack' => 'V'),
  726. 'type' => array('pack' => 'V', 'data' => 0x40), // Filetime (64-bit value representing the number of 100-nanosecond intervals since January 1, 1601)
  727. 'data' => array('data' => PHPExcel_Shared_OLE::LocalDate2OLE($dataProp)));
  728. $dataSection_NumProps++;
  729. }
  730. // Modified Date/Time
  731. if($this->_phpExcel->getProperties()->getModified()){
  732. $dataProp = $this->_phpExcel->getProperties()->getModified();
  733. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x0D),
  734. 'offset' => array('pack' => 'V'),
  735. 'type' => array('pack' => 'V', 'data' => 0x40), // Filetime (64-bit value representing the number of 100-nanosecond intervals since January 1, 1601)
  736. 'data' => array('data' => PHPExcel_Shared_OLE::LocalDate2OLE($dataProp)));
  737. $dataSection_NumProps++;
  738. }
  739. // Security
  740. $dataSection[] = array('summary'=> array('pack' => 'V', 'data' => 0x13),
  741. 'offset' => array('pack' => 'V'),
  742. 'type' => array('pack' => 'V', 'data' => 0x03), // 4 byte signed integer
  743. 'data' => array('data' => 0x00));
  744. $dataSection_NumProps++;
  745. // 4 Section Length
  746. // 4 Property count
  747. // 8 * $dataSection_NumProps (8 = ID (4) + OffSet(4))
  748. $dataSection_Content_Offset = 8 + $dataSection_NumProps * 8;
  749. foreach ($dataSection as $dataProp){
  750. // Summary
  751. $dataSection_Summary .= pack($dataProp['summary']['pack'], $dataProp['summary']['data']);
  752. // Offset
  753. $dataSection_Summary .= pack($dataProp['offset']['pack'], $dataSection_Content_Offset);
  754. // DataType
  755. $dataSection_Content .= pack($dataProp['type']['pack'], $dataProp['type']['data']);
  756. // Data
  757. if($dataProp['type']['data'] == 0x02){ // 2 byte signed integer
  758. $dataSection_Content .= pack('V', $dataProp['data']['data']);
  759. $dataSection_Content_Offset += 4 + 4;
  760. }
  761. elseif($dataProp['type']['data'] == 0x03){ // 4 byte signed integer
  762. $dataSection_Content .= pack('V', $dataProp['data']['data']);
  763. $dataSection_Content_Offset += 4 + 4;
  764. }
  765. elseif($dataProp['type']['data'] == 0x1E){ // null-terminated string prepended by dword string length
  766. // Null-terminated string
  767. $dataProp['data']['data'] .= chr(0);
  768. $dataProp['data']['length'] += 1;
  769. // Complete the string with null string for being a %4
  770. $dataProp['data']['length'] = $dataProp['data']['length'] + ((4 - $dataProp['data']['length'] % 4)==4 ? 0 : (4 - $dataProp['data']['length'] % 4));
  771. $dataProp['data']['data'] = str_pad($dataProp['data']['data'], $dataProp['data']['length'], chr(0), STR_PAD_RIGHT);
  772. $dataSection_Content .= pack('V', $dataProp['data']['length']);
  773. $dataSection_Content .= $dataProp['data']['data'];
  774. $dataSection_Content_Offset += 4 + 4 + strlen($dataProp['data']['data']);
  775. }
  776. elseif($dataProp['type']['data'] == 0x40){ // Filetime (64-bit value representing the number of 100-nanosecond intervals since January 1, 1601)
  777. $dataSection_Content .= $dataProp['data']['data'];
  778. $dataSection_Content_Offset += 4 + 8;
  779. }
  780. else {
  781. // Data Type Not Used at the moment
  782. }
  783. }
  784. // Now $dataSection_Content_Offset contains the size of the content
  785. // section header
  786. // offset: $secOffset; size: 4; section length
  787. // + x Size of the content (summary + content)
  788. $data .= pack('V', $dataSection_Content_Offset);
  789. // offset: $secOffset+4; size: 4; property count
  790. $data .= pack('V', $dataSection_NumProps);
  791. // Section Summary
  792. $data .= $dataSection_Summary;
  793. // Section Content
  794. $data .= $dataSection_Content;
  795. return $data;
  796. }
  797. }