Excel2007.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. class PHPExcel_Writer_Excel2007 extends PHPExcel_Writer_Abstract implements PHPExcel_Writer_IWriter
  8. {
  9. /**
  10. * Pre-calculate formulas
  11. * Forces PHPExcel to recalculate all formulae in a workbook when saving, so that the pre-calculated values are
  12. * immediately available to MS Excel or other office spreadsheet viewer when opening the file
  13. *
  14. * Overrides the default TRUE for this specific writer for performance reasons
  15. *
  16. * @var boolean
  17. */
  18. protected $preCalculateFormulas = false;
  19. /**
  20. * Office2003 compatibility
  21. *
  22. * @var boolean
  23. */
  24. private $office2003compatibility = false;
  25. /**
  26. * Private writer parts
  27. *
  28. * @var PHPExcel_Writer_Excel2007_WriterPart[]
  29. */
  30. private $writerParts = array();
  31. /**
  32. * Private PHPExcel
  33. *
  34. * @var PHPExcel
  35. */
  36. private $spreadSheet;
  37. /**
  38. * Private string table
  39. *
  40. * @var string[]
  41. */
  42. private $stringTable = array();
  43. /**
  44. * Private unique PHPExcel_Style_Conditional HashTable
  45. *
  46. * @var PHPExcel_HashTable
  47. */
  48. private $stylesConditionalHashTable;
  49. /**
  50. * Private unique PHPExcel_Style HashTable
  51. *
  52. * @var PHPExcel_HashTable
  53. */
  54. private $styleHashTable;
  55. /**
  56. * Private unique PHPExcel_Style_Fill HashTable
  57. *
  58. * @var PHPExcel_HashTable
  59. */
  60. private $fillHashTable;
  61. /**
  62. * Private unique PHPExcel_Style_Font HashTable
  63. *
  64. * @var PHPExcel_HashTable
  65. */
  66. private $fontHashTable;
  67. /**
  68. * Private unique PHPExcel_Style_Borders HashTable
  69. *
  70. * @var PHPExcel_HashTable
  71. */
  72. private $bordersHashTable ;
  73. /**
  74. * Private unique PHPExcel_Style_NumberFormat HashTable
  75. *
  76. * @var PHPExcel_HashTable
  77. */
  78. private $numFmtHashTable;
  79. /**
  80. * Private unique PHPExcel_Worksheet_BaseDrawing HashTable
  81. *
  82. * @var PHPExcel_HashTable
  83. */
  84. private $drawingHashTable;
  85. /**
  86. * Create a new PHPExcel_Writer_Excel2007
  87. *
  88. * @param PHPExcel $pPHPExcel
  89. */
  90. public function __construct(PHPExcel $pPHPExcel = null)
  91. {
  92. // Assign PHPExcel
  93. $this->setPHPExcel($pPHPExcel);
  94. $writerPartsArray = array( 'stringtable' => 'PHPExcel_Writer_Excel2007_StringTable',
  95. 'contenttypes' => 'PHPExcel_Writer_Excel2007_ContentTypes',
  96. 'docprops' => 'PHPExcel_Writer_Excel2007_DocProps',
  97. 'rels' => 'PHPExcel_Writer_Excel2007_Rels',
  98. 'theme' => 'PHPExcel_Writer_Excel2007_Theme',
  99. 'style' => 'PHPExcel_Writer_Excel2007_Style',
  100. 'workbook' => 'PHPExcel_Writer_Excel2007_Workbook',
  101. 'worksheet' => 'PHPExcel_Writer_Excel2007_Worksheet',
  102. 'drawing' => 'PHPExcel_Writer_Excel2007_Drawing',
  103. 'comments' => 'PHPExcel_Writer_Excel2007_Comments',
  104. 'chart' => 'PHPExcel_Writer_Excel2007_Chart',
  105. 'relsvba' => 'PHPExcel_Writer_Excel2007_RelsVBA',
  106. 'relsribbonobjects' => 'PHPExcel_Writer_Excel2007_RelsRibbon'
  107. );
  108. // Initialise writer parts
  109. // and Assign their parent IWriters
  110. foreach ($writerPartsArray as $writer => $class) {
  111. $this->writerParts[$writer] = new $class($this);
  112. }
  113. $hashTablesArray = array( 'stylesConditionalHashTable', 'fillHashTable', 'fontHashTable',
  114. 'bordersHashTable', 'numFmtHashTable', 'drawingHashTable',
  115. 'styleHashTable'
  116. );
  117. // Set HashTable variables
  118. foreach ($hashTablesArray as $tableName) {
  119. $this->$tableName = new PHPExcel_HashTable();
  120. }
  121. }
  122. /**
  123. * Get writer part
  124. *
  125. * @param string $pPartName Writer part name
  126. * @return PHPExcel_Writer_Excel2007_WriterPart
  127. */
  128. public function getWriterPart($pPartName = '')
  129. {
  130. if ($pPartName != '' && isset($this->writerParts[strtolower($pPartName)])) {
  131. return $this->writerParts[strtolower($pPartName)];
  132. } else {
  133. return null;
  134. }
  135. }
  136. /**
  137. * Save PHPExcel to file
  138. *
  139. * @param string $pFilename
  140. * @throws PHPExcel_Writer_Exception
  141. */
  142. public function save($pFilename = null)
  143. {
  144. if ($this->spreadSheet !== null) {
  145. // garbage collect
  146. $this->spreadSheet->garbageCollect();
  147. // If $pFilename is php://output or php://stdout, make it a temporary file...
  148. $originalFilename = $pFilename;
  149. if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
  150. $pFilename = @tempnam(PHPExcel_Shared_File::sys_get_temp_dir(), 'phpxltmp');
  151. if ($pFilename == '') {
  152. $pFilename = $originalFilename;
  153. }
  154. }
  155. $saveDebugLog = PHPExcel_Calculation::getInstance($this->spreadSheet)->getDebugLog()->getWriteDebugLog();
  156. PHPExcel_Calculation::getInstance($this->spreadSheet)->getDebugLog()->setWriteDebugLog(false);
  157. $saveDateReturnType = PHPExcel_Calculation_Functions::getReturnDateType();
  158. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
  159. // Create string lookup table
  160. $this->stringTable = array();
  161. for ($i = 0; $i < $this->spreadSheet->getSheetCount(); ++$i) {
  162. $this->stringTable = $this->getWriterPart('StringTable')->createStringTable($this->spreadSheet->getSheet($i), $this->stringTable);
  163. }
  164. // Create styles dictionaries
  165. $this->styleHashTable->addFromSource($this->getWriterPart('Style')->allStyles($this->spreadSheet));
  166. $this->stylesConditionalHashTable->addFromSource($this->getWriterPart('Style')->allConditionalStyles($this->spreadSheet));
  167. $this->fillHashTable->addFromSource($this->getWriterPart('Style')->allFills($this->spreadSheet));
  168. $this->fontHashTable->addFromSource($this->getWriterPart('Style')->allFonts($this->spreadSheet));
  169. $this->bordersHashTable->addFromSource($this->getWriterPart('Style')->allBorders($this->spreadSheet));
  170. $this->numFmtHashTable->addFromSource($this->getWriterPart('Style')->allNumberFormats($this->spreadSheet));
  171. // Create drawing dictionary
  172. $this->drawingHashTable->addFromSource($this->getWriterPart('Drawing')->allDrawings($this->spreadSheet));
  173. // Create new ZIP file and open it for writing
  174. $zipClass = PHPExcel_Settings::getZipClass();
  175. $objZip = new $zipClass();
  176. // Retrieve OVERWRITE and CREATE constants from the instantiated zip class
  177. // This method of accessing constant values from a dynamic class should work with all appropriate versions of PHP
  178. $ro = new ReflectionObject($objZip);
  179. $zipOverWrite = $ro->getConstant('OVERWRITE');
  180. $zipCreate = $ro->getConstant('CREATE');
  181. if (file_exists($pFilename)) {
  182. unlink($pFilename);
  183. }
  184. // Try opening the ZIP file
  185. if ($objZip->open($pFilename, $zipOverWrite) !== true) {
  186. if ($objZip->open($pFilename, $zipCreate) !== true) {
  187. throw new PHPExcel_Writer_Exception("Could not open " . $pFilename . " for writing.");
  188. }
  189. }
  190. // Add [Content_Types].xml to ZIP file
  191. $objZip->addFromString('[Content_Types].xml', $this->getWriterPart('ContentTypes')->writeContentTypes($this->spreadSheet, $this->includeCharts));
  192. //if hasMacros, add the vbaProject.bin file, Certificate file(if exists)
  193. if ($this->spreadSheet->hasMacros()) {
  194. $macrosCode=$this->spreadSheet->getMacrosCode();
  195. if (!is_null($macrosCode)) {// we have the code ?
  196. $objZip->addFromString('xl/vbaProject.bin', $macrosCode);//allways in 'xl', allways named vbaProject.bin
  197. if ($this->spreadSheet->hasMacrosCertificate()) {//signed macros ?
  198. // Yes : add the certificate file and the related rels file
  199. $objZip->addFromString('xl/vbaProjectSignature.bin', $this->spreadSheet->getMacrosCertificate());
  200. $objZip->addFromString('xl/_rels/vbaProject.bin.rels', $this->getWriterPart('RelsVBA')->writeVBARelationships($this->spreadSheet));
  201. }
  202. }
  203. }
  204. //a custom UI in this workbook ? add it ("base" xml and additional objects (pictures) and rels)
  205. if ($this->spreadSheet->hasRibbon()) {
  206. $tmpRibbonTarget=$this->spreadSheet->getRibbonXMLData('target');
  207. $objZip->addFromString($tmpRibbonTarget, $this->spreadSheet->getRibbonXMLData('data'));
  208. if ($this->spreadSheet->hasRibbonBinObjects()) {
  209. $tmpRootPath=dirname($tmpRibbonTarget).'/';
  210. $ribbonBinObjects=$this->spreadSheet->getRibbonBinObjects('data');//the files to write
  211. foreach ($ribbonBinObjects as $aPath => $aContent) {
  212. $objZip->addFromString($tmpRootPath.$aPath, $aContent);
  213. }
  214. //the rels for files
  215. $objZip->addFromString($tmpRootPath.'_rels/'.basename($tmpRibbonTarget).'.rels', $this->getWriterPart('RelsRibbonObjects')->writeRibbonRelationships($this->spreadSheet));
  216. }
  217. }
  218. // Add relationships to ZIP file
  219. $objZip->addFromString('_rels/.rels', $this->getWriterPart('Rels')->writeRelationships($this->spreadSheet));
  220. $objZip->addFromString('xl/_rels/workbook.xml.rels', $this->getWriterPart('Rels')->writeWorkbookRelationships($this->spreadSheet));
  221. // Add document properties to ZIP file
  222. $objZip->addFromString('docProps/app.xml', $this->getWriterPart('DocProps')->writeDocPropsApp($this->spreadSheet));
  223. $objZip->addFromString('docProps/core.xml', $this->getWriterPart('DocProps')->writeDocPropsCore($this->spreadSheet));
  224. $customPropertiesPart = $this->getWriterPart('DocProps')->writeDocPropsCustom($this->spreadSheet);
  225. if ($customPropertiesPart !== null) {
  226. $objZip->addFromString('docProps/custom.xml', $customPropertiesPart);
  227. }
  228. // Add theme to ZIP file
  229. $objZip->addFromString('xl/theme/theme1.xml', $this->getWriterPart('Theme')->writeTheme($this->spreadSheet));
  230. // Add string table to ZIP file
  231. $objZip->addFromString('xl/sharedStrings.xml', $this->getWriterPart('StringTable')->writeStringTable($this->stringTable));
  232. // Add styles to ZIP file
  233. $objZip->addFromString('xl/styles.xml', $this->getWriterPart('Style')->writeStyles($this->spreadSheet));
  234. // Add workbook to ZIP file
  235. $objZip->addFromString('xl/workbook.xml', $this->getWriterPart('Workbook')->writeWorkbook($this->spreadSheet, $this->preCalculateFormulas));
  236. $chartCount = 0;
  237. // Add worksheets
  238. for ($i = 0; $i < $this->spreadSheet->getSheetCount(); ++$i) {
  239. $objZip->addFromString('xl/worksheets/sheet' . ($i + 1) . '.xml', $this->getWriterPart('Worksheet')->writeWorksheet($this->spreadSheet->getSheet($i), $this->stringTable, $this->includeCharts));
  240. if ($this->includeCharts) {
  241. $charts = $this->spreadSheet->getSheet($i)->getChartCollection();
  242. if (count($charts) > 0) {
  243. foreach ($charts as $chart) {
  244. $objZip->addFromString('xl/charts/chart' . ($chartCount + 1) . '.xml', $this->getWriterPart('Chart')->writeChart($chart, $this->preCalculateFormulas));
  245. $chartCount++;
  246. }
  247. }
  248. }
  249. }
  250. $chartRef1 = $chartRef2 = 0;
  251. // Add worksheet relationships (drawings, ...)
  252. for ($i = 0; $i < $this->spreadSheet->getSheetCount(); ++$i) {
  253. // Add relationships
  254. $objZip->addFromString('xl/worksheets/_rels/sheet' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeWorksheetRelationships($this->spreadSheet->getSheet($i), ($i + 1), $this->includeCharts));
  255. $drawings = $this->spreadSheet->getSheet($i)->getDrawingCollection();
  256. $drawingCount = count($drawings);
  257. if ($this->includeCharts) {
  258. $chartCount = $this->spreadSheet->getSheet($i)->getChartCount();
  259. }
  260. // Add drawing and image relationship parts
  261. if (($drawingCount > 0) || ($chartCount > 0)) {
  262. // Drawing relationships
  263. $objZip->addFromString('xl/drawings/_rels/drawing' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeDrawingRelationships($this->spreadSheet->getSheet($i), $chartRef1, $this->includeCharts));
  264. // Drawings
  265. $objZip->addFromString('xl/drawings/drawing' . ($i + 1) . '.xml', $this->getWriterPart('Drawing')->writeDrawings($this->spreadSheet->getSheet($i), $chartRef2, $this->includeCharts));
  266. }
  267. // Add comment relationship parts
  268. if (count($this->spreadSheet->getSheet($i)->getComments()) > 0) {
  269. // VML Comments
  270. $objZip->addFromString('xl/drawings/vmlDrawing' . ($i + 1) . '.vml', $this->getWriterPart('Comments')->writeVMLComments($this->spreadSheet->getSheet($i)));
  271. // Comments
  272. $objZip->addFromString('xl/comments' . ($i + 1) . '.xml', $this->getWriterPart('Comments')->writeComments($this->spreadSheet->getSheet($i)));
  273. }
  274. // Add header/footer relationship parts
  275. if (count($this->spreadSheet->getSheet($i)->getHeaderFooter()->getImages()) > 0) {
  276. // VML Drawings
  277. $objZip->addFromString('xl/drawings/vmlDrawingHF' . ($i + 1) . '.vml', $this->getWriterPart('Drawing')->writeVMLHeaderFooterImages($this->spreadSheet->getSheet($i)));
  278. // VML Drawing relationships
  279. $objZip->addFromString('xl/drawings/_rels/vmlDrawingHF' . ($i + 1) . '.vml.rels', $this->getWriterPart('Rels')->writeHeaderFooterDrawingRelationships($this->spreadSheet->getSheet($i)));
  280. // Media
  281. foreach ($this->spreadSheet->getSheet($i)->getHeaderFooter()->getImages() as $image) {
  282. $objZip->addFromString('xl/media/' . $image->getIndexedFilename(), file_get_contents($image->getPath()));
  283. }
  284. }
  285. }
  286. // Add media
  287. for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) {
  288. if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_Drawing) {
  289. $imageContents = null;
  290. $imagePath = $this->getDrawingHashTable()->getByIndex($i)->getPath();
  291. if (strpos($imagePath, 'zip://') !== false) {
  292. $imagePath = substr($imagePath, 6);
  293. $imagePathSplitted = explode('#', $imagePath);
  294. $imageZip = new ZipArchive();
  295. $imageZip->open($imagePathSplitted[0]);
  296. $imageContents = $imageZip->getFromName($imagePathSplitted[1]);
  297. $imageZip->close();
  298. unset($imageZip);
  299. } else {
  300. $imageContents = file_get_contents($imagePath);
  301. }
  302. $objZip->addFromString('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
  303. } elseif ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_MemoryDrawing) {
  304. ob_start();
  305. call_user_func(
  306. $this->getDrawingHashTable()->getByIndex($i)->getRenderingFunction(),
  307. $this->getDrawingHashTable()->getByIndex($i)->getImageResource()
  308. );
  309. $imageContents = ob_get_contents();
  310. ob_end_clean();
  311. $objZip->addFromString('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
  312. }
  313. }
  314. PHPExcel_Calculation_Functions::setReturnDateType($saveDateReturnType);
  315. PHPExcel_Calculation::getInstance($this->spreadSheet)->getDebugLog()->setWriteDebugLog($saveDebugLog);
  316. // Close file
  317. if ($objZip->close() === false) {
  318. throw new PHPExcel_Writer_Exception("Could not close zip file $pFilename.");
  319. }
  320. // If a temporary file was used, copy it to the correct file stream
  321. if ($originalFilename != $pFilename) {
  322. if (copy($pFilename, $originalFilename) === false) {
  323. throw new PHPExcel_Writer_Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
  324. }
  325. @unlink($pFilename);
  326. }
  327. } else {
  328. throw new PHPExcel_Writer_Exception("PHPExcel object unassigned.");
  329. }
  330. }
  331. /**
  332. * Get PHPExcel object
  333. *
  334. * @return PHPExcel
  335. * @throws PHPExcel_Writer_Exception
  336. */
  337. public function getPHPExcel()
  338. {
  339. if ($this->spreadSheet !== null) {
  340. return $this->spreadSheet;
  341. } else {
  342. throw new PHPExcel_Writer_Exception("No PHPExcel object assigned.");
  343. }
  344. }
  345. /**
  346. * Set PHPExcel object
  347. *
  348. * @param PHPExcel $pPHPExcel PHPExcel object
  349. * @throws PHPExcel_Writer_Exception
  350. * @return PHPExcel_Writer_Excel2007
  351. */
  352. public function setPHPExcel(PHPExcel $pPHPExcel = null)
  353. {
  354. $this->spreadSheet = $pPHPExcel;
  355. return $this;
  356. }
  357. /**
  358. * Get string table
  359. *
  360. * @return string[]
  361. */
  362. public function getStringTable()
  363. {
  364. return $this->stringTable;
  365. }
  366. /**
  367. * Get PHPExcel_Style HashTable
  368. *
  369. * @return PHPExcel_HashTable
  370. */
  371. public function getStyleHashTable()
  372. {
  373. return $this->styleHashTable;
  374. }
  375. /**
  376. * Get PHPExcel_Style_Conditional HashTable
  377. *
  378. * @return PHPExcel_HashTable
  379. */
  380. public function getStylesConditionalHashTable()
  381. {
  382. return $this->stylesConditionalHashTable;
  383. }
  384. /**
  385. * Get PHPExcel_Style_Fill HashTable
  386. *
  387. * @return PHPExcel_HashTable
  388. */
  389. public function getFillHashTable()
  390. {
  391. return $this->fillHashTable;
  392. }
  393. /**
  394. * Get PHPExcel_Style_Font HashTable
  395. *
  396. * @return PHPExcel_HashTable
  397. */
  398. public function getFontHashTable()
  399. {
  400. return $this->fontHashTable;
  401. }
  402. /**
  403. * Get PHPExcel_Style_Borders HashTable
  404. *
  405. * @return PHPExcel_HashTable
  406. */
  407. public function getBordersHashTable()
  408. {
  409. return $this->bordersHashTable;
  410. }
  411. /**
  412. * Get PHPExcel_Style_NumberFormat HashTable
  413. *
  414. * @return PHPExcel_HashTable
  415. */
  416. public function getNumFmtHashTable()
  417. {
  418. return $this->numFmtHashTable;
  419. }
  420. /**
  421. * Get PHPExcel_Worksheet_BaseDrawing HashTable
  422. *
  423. * @return PHPExcel_HashTable
  424. */
  425. public function getDrawingHashTable()
  426. {
  427. return $this->drawingHashTable;
  428. }
  429. /**
  430. * Get Office2003 compatibility
  431. *
  432. * @return boolean
  433. */
  434. public function getOffice2003Compatibility()
  435. {
  436. return $this->office2003compatibility;
  437. }
  438. /**
  439. * Set Office2003 compatibility
  440. *
  441. * @param boolean $pValue Office2003 compatibility?
  442. * @return PHPExcel_Writer_Excel2007
  443. */
  444. public function setOffice2003Compatibility($pValue = false)
  445. {
  446. $this->office2003compatibility = $pValue;
  447. return $this;
  448. }
  449. }