Excel2007.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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_Excel2007
  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_Excel2007
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Writer_2007
  32. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Writer_Excel2007 extends PHPExcel_Writer_Abstract implements PHPExcel_Writer_IWriter
  35. {
  36. /**
  37. * Office2003 compatibility
  38. *
  39. * @var boolean
  40. */
  41. private $_office2003compatibility = false;
  42. /**
  43. * Private writer parts
  44. *
  45. * @var PHPExcel_Writer_Excel2007_WriterPart[]
  46. */
  47. private $_writerParts = array();
  48. /**
  49. * Private PHPExcel
  50. *
  51. * @var PHPExcel
  52. */
  53. private $_spreadSheet;
  54. /**
  55. * Private string table
  56. *
  57. * @var string[]
  58. */
  59. private $_stringTable = array();
  60. /**
  61. * Private unique PHPExcel_Style_Conditional HashTable
  62. *
  63. * @var PHPExcel_HashTable
  64. */
  65. private $_stylesConditionalHashTable;
  66. /**
  67. * Private unique PHPExcel_Style_Fill HashTable
  68. *
  69. * @var PHPExcel_HashTable
  70. */
  71. private $_fillHashTable;
  72. /**
  73. * Private unique PHPExcel_Style_Font HashTable
  74. *
  75. * @var PHPExcel_HashTable
  76. */
  77. private $_fontHashTable;
  78. /**
  79. * Private unique PHPExcel_Style_Borders HashTable
  80. *
  81. * @var PHPExcel_HashTable
  82. */
  83. private $_bordersHashTable ;
  84. /**
  85. * Private unique PHPExcel_Style_NumberFormat HashTable
  86. *
  87. * @var PHPExcel_HashTable
  88. */
  89. private $_numFmtHashTable;
  90. /**
  91. * Private unique PHPExcel_Worksheet_BaseDrawing HashTable
  92. *
  93. * @var PHPExcel_HashTable
  94. */
  95. private $_drawingHashTable;
  96. /**
  97. * Create a new PHPExcel_Writer_Excel2007
  98. *
  99. * @param PHPExcel $pPHPExcel
  100. */
  101. public function __construct(PHPExcel $pPHPExcel = null)
  102. {
  103. // Assign PHPExcel
  104. $this->setPHPExcel($pPHPExcel);
  105. $writerPartsArray = array( 'stringtable' => 'PHPExcel_Writer_Excel2007_StringTable',
  106. 'contenttypes' => 'PHPExcel_Writer_Excel2007_ContentTypes',
  107. 'docprops' => 'PHPExcel_Writer_Excel2007_DocProps',
  108. 'rels' => 'PHPExcel_Writer_Excel2007_Rels',
  109. 'theme' => 'PHPExcel_Writer_Excel2007_Theme',
  110. 'style' => 'PHPExcel_Writer_Excel2007_Style',
  111. 'workbook' => 'PHPExcel_Writer_Excel2007_Workbook',
  112. 'worksheet' => 'PHPExcel_Writer_Excel2007_Worksheet',
  113. 'drawing' => 'PHPExcel_Writer_Excel2007_Drawing',
  114. 'comments' => 'PHPExcel_Writer_Excel2007_Comments',
  115. 'chart' => 'PHPExcel_Writer_Excel2007_Chart',
  116. );
  117. // Initialise writer parts
  118. // and Assign their parent IWriters
  119. foreach ($writerPartsArray as $writer => $class) {
  120. $this->_writerParts[$writer] = new $class($this);
  121. }
  122. $hashTablesArray = array( '_stylesConditionalHashTable', '_fillHashTable', '_fontHashTable',
  123. '_bordersHashTable', '_numFmtHashTable', '_drawingHashTable'
  124. );
  125. // Set HashTable variables
  126. foreach ($hashTablesArray as $tableName) {
  127. $this->$tableName = new PHPExcel_HashTable();
  128. }
  129. }
  130. /**
  131. * Get writer part
  132. *
  133. * @param string $pPartName Writer part name
  134. * @return PHPExcel_Writer_Excel2007_WriterPart
  135. */
  136. public function getWriterPart($pPartName = '') {
  137. if ($pPartName != '' && isset($this->_writerParts[strtolower($pPartName)])) {
  138. return $this->_writerParts[strtolower($pPartName)];
  139. } else {
  140. return null;
  141. }
  142. }
  143. /**
  144. * Save PHPExcel to file
  145. *
  146. * @param string $pFilename
  147. * @throws PHPExcel_Writer_Exception
  148. */
  149. public function save($pFilename = null)
  150. {
  151. if ($this->_spreadSheet !== NULL) {
  152. // garbage collect
  153. $this->_spreadSheet->garbageCollect();
  154. // If $pFilename is php://output or php://stdout, make it a temporary file...
  155. $originalFilename = $pFilename;
  156. if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
  157. $pFilename = @tempnam(PHPExcel_Shared_File::sys_get_temp_dir(), 'phpxltmp');
  158. if ($pFilename == '') {
  159. $pFilename = $originalFilename;
  160. }
  161. }
  162. $saveDebugLog = PHPExcel_Calculation::getInstance($this->_spreadSheet)->getDebugLog()->getWriteDebugLog();
  163. PHPExcel_Calculation::getInstance($this->_spreadSheet)->getDebugLog()->setWriteDebugLog(FALSE);
  164. $saveDateReturnType = PHPExcel_Calculation_Functions::getReturnDateType();
  165. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
  166. // Create string lookup table
  167. $this->_stringTable = array();
  168. for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
  169. $this->_stringTable = $this->getWriterPart('StringTable')->createStringTable($this->_spreadSheet->getSheet($i), $this->_stringTable);
  170. }
  171. // Create styles dictionaries
  172. $this->_stylesConditionalHashTable->addFromSource( $this->getWriterPart('Style')->allConditionalStyles($this->_spreadSheet) );
  173. $this->_fillHashTable->addFromSource( $this->getWriterPart('Style')->allFills($this->_spreadSheet) );
  174. $this->_fontHashTable->addFromSource( $this->getWriterPart('Style')->allFonts($this->_spreadSheet) );
  175. $this->_bordersHashTable->addFromSource( $this->getWriterPart('Style')->allBorders($this->_spreadSheet) );
  176. $this->_numFmtHashTable->addFromSource( $this->getWriterPart('Style')->allNumberFormats($this->_spreadSheet) );
  177. // Create drawing dictionary
  178. $this->_drawingHashTable->addFromSource( $this->getWriterPart('Drawing')->allDrawings($this->_spreadSheet) );
  179. // Create new ZIP file and open it for writing
  180. $zipClass = PHPExcel_Settings::getZipClass();
  181. $objZip = new $zipClass();
  182. // Retrieve OVERWRITE and CREATE constants from the instantiated zip class
  183. // This method of accessing constant values from a dynamic class should work with all appropriate versions of PHP
  184. $ro = new ReflectionObject($objZip);
  185. $zipOverWrite = $ro->getConstant('OVERWRITE');
  186. $zipCreate = $ro->getConstant('CREATE');
  187. if (file_exists($pFilename)) {
  188. unlink($pFilename);
  189. }
  190. // Try opening the ZIP file
  191. if ($objZip->open($pFilename, $zipOverWrite) !== true) {
  192. if ($objZip->open($pFilename, $zipCreate) !== true) {
  193. throw new PHPExcel_Writer_Exception("Could not open " . $pFilename . " for writing.");
  194. }
  195. }
  196. // Add [Content_Types].xml to ZIP file
  197. $objZip->addFromString('[Content_Types].xml', $this->getWriterPart('ContentTypes')->writeContentTypes($this->_spreadSheet, $this->_includeCharts));
  198. // Add relationships to ZIP file
  199. $objZip->addFromString('_rels/.rels', $this->getWriterPart('Rels')->writeRelationships($this->_spreadSheet));
  200. $objZip->addFromString('xl/_rels/workbook.xml.rels', $this->getWriterPart('Rels')->writeWorkbookRelationships($this->_spreadSheet));
  201. // Add document properties to ZIP file
  202. $objZip->addFromString('docProps/app.xml', $this->getWriterPart('DocProps')->writeDocPropsApp($this->_spreadSheet));
  203. $objZip->addFromString('docProps/core.xml', $this->getWriterPart('DocProps')->writeDocPropsCore($this->_spreadSheet));
  204. $customPropertiesPart = $this->getWriterPart('DocProps')->writeDocPropsCustom($this->_spreadSheet);
  205. if ($customPropertiesPart !== NULL) {
  206. $objZip->addFromString('docProps/custom.xml', $customPropertiesPart);
  207. }
  208. // Add theme to ZIP file
  209. $objZip->addFromString('xl/theme/theme1.xml', $this->getWriterPart('Theme')->writeTheme($this->_spreadSheet));
  210. // Add string table to ZIP file
  211. $objZip->addFromString('xl/sharedStrings.xml', $this->getWriterPart('StringTable')->writeStringTable($this->_stringTable));
  212. // Add styles to ZIP file
  213. $objZip->addFromString('xl/styles.xml', $this->getWriterPart('Style')->writeStyles($this->_spreadSheet));
  214. // Add workbook to ZIP file
  215. $objZip->addFromString('xl/workbook.xml', $this->getWriterPart('Workbook')->writeWorkbook($this->_spreadSheet, $this->_preCalculateFormulas));
  216. $chartCount = 0;
  217. // Add worksheets
  218. for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
  219. $objZip->addFromString('xl/worksheets/sheet' . ($i + 1) . '.xml', $this->getWriterPart('Worksheet')->writeWorksheet($this->_spreadSheet->getSheet($i), $this->_stringTable, $this->_includeCharts));
  220. if ($this->_includeCharts) {
  221. $charts = $this->_spreadSheet->getSheet($i)->getChartCollection();
  222. if (count($charts) > 0) {
  223. foreach($charts as $chart) {
  224. $objZip->addFromString('xl/charts/chart' . ($chartCount + 1) . '.xml', $this->getWriterPart('Chart')->writeChart($chart));
  225. $chartCount++;
  226. }
  227. }
  228. }
  229. }
  230. $chartRef1 = $chartRef2 = 0;
  231. // Add worksheet relationships (drawings, ...)
  232. for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
  233. // Add relationships
  234. $objZip->addFromString('xl/worksheets/_rels/sheet' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeWorksheetRelationships($this->_spreadSheet->getSheet($i), ($i + 1), $this->_includeCharts));
  235. $drawings = $this->_spreadSheet->getSheet($i)->getDrawingCollection();
  236. $drawingCount = count($drawings);
  237. if ($this->_includeCharts) {
  238. $chartCount = $this->_spreadSheet->getSheet($i)->getChartCount();
  239. }
  240. // Add drawing and image relationship parts
  241. if (($drawingCount > 0) || ($chartCount > 0)) {
  242. // Drawing relationships
  243. $objZip->addFromString('xl/drawings/_rels/drawing' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeDrawingRelationships($this->_spreadSheet->getSheet($i),$chartRef1, $this->_includeCharts));
  244. // Drawings
  245. $objZip->addFromString('xl/drawings/drawing' . ($i + 1) . '.xml', $this->getWriterPart('Drawing')->writeDrawings($this->_spreadSheet->getSheet($i),$chartRef2,$this->_includeCharts));
  246. }
  247. // Add comment relationship parts
  248. if (count($this->_spreadSheet->getSheet($i)->getComments()) > 0) {
  249. // VML Comments
  250. $objZip->addFromString('xl/drawings/vmlDrawing' . ($i + 1) . '.vml', $this->getWriterPart('Comments')->writeVMLComments($this->_spreadSheet->getSheet($i)));
  251. // Comments
  252. $objZip->addFromString('xl/comments' . ($i + 1) . '.xml', $this->getWriterPart('Comments')->writeComments($this->_spreadSheet->getSheet($i)));
  253. }
  254. // Add header/footer relationship parts
  255. if (count($this->_spreadSheet->getSheet($i)->getHeaderFooter()->getImages()) > 0) {
  256. // VML Drawings
  257. $objZip->addFromString('xl/drawings/vmlDrawingHF' . ($i + 1) . '.vml', $this->getWriterPart('Drawing')->writeVMLHeaderFooterImages($this->_spreadSheet->getSheet($i)));
  258. // VML Drawing relationships
  259. $objZip->addFromString('xl/drawings/_rels/vmlDrawingHF' . ($i + 1) . '.vml.rels', $this->getWriterPart('Rels')->writeHeaderFooterDrawingRelationships($this->_spreadSheet->getSheet($i)));
  260. // Media
  261. foreach ($this->_spreadSheet->getSheet($i)->getHeaderFooter()->getImages() as $image) {
  262. $objZip->addFromString('xl/media/' . $image->getIndexedFilename(), file_get_contents($image->getPath()));
  263. }
  264. }
  265. }
  266. // Add media
  267. for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) {
  268. if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_Drawing) {
  269. $imageContents = null;
  270. $imagePath = $this->getDrawingHashTable()->getByIndex($i)->getPath();
  271. if (strpos($imagePath, 'zip://') !== false) {
  272. $imagePath = substr($imagePath, 6);
  273. $imagePathSplitted = explode('#', $imagePath);
  274. $imageZip = new ZipArchive();
  275. $imageZip->open($imagePathSplitted[0]);
  276. $imageContents = $imageZip->getFromName($imagePathSplitted[1]);
  277. $imageZip->close();
  278. unset($imageZip);
  279. } else {
  280. $imageContents = file_get_contents($imagePath);
  281. }
  282. $objZip->addFromString('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
  283. } else if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_MemoryDrawing) {
  284. ob_start();
  285. call_user_func(
  286. $this->getDrawingHashTable()->getByIndex($i)->getRenderingFunction(),
  287. $this->getDrawingHashTable()->getByIndex($i)->getImageResource()
  288. );
  289. $imageContents = ob_get_contents();
  290. ob_end_clean();
  291. $objZip->addFromString('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
  292. }
  293. }
  294. PHPExcel_Calculation_Functions::setReturnDateType($saveDateReturnType);
  295. PHPExcel_Calculation::getInstance($this->_spreadSheet)->getDebugLog()->setWriteDebugLog($saveDebugLog);
  296. // Close file
  297. if ($objZip->close() === false) {
  298. throw new PHPExcel_Writer_Exception("Could not close zip file $pFilename.");
  299. }
  300. // If a temporary file was used, copy it to the correct file stream
  301. if ($originalFilename != $pFilename) {
  302. if (copy($pFilename, $originalFilename) === false) {
  303. throw new PHPExcel_Writer_Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
  304. }
  305. @unlink($pFilename);
  306. }
  307. } else {
  308. throw new PHPExcel_Writer_Exception("PHPExcel object unassigned.");
  309. }
  310. }
  311. /**
  312. * Get PHPExcel object
  313. *
  314. * @return PHPExcel
  315. * @throws PHPExcel_Writer_Exception
  316. */
  317. public function getPHPExcel() {
  318. if ($this->_spreadSheet !== null) {
  319. return $this->_spreadSheet;
  320. } else {
  321. throw new PHPExcel_Writer_Exception("No PHPExcel assigned.");
  322. }
  323. }
  324. /**
  325. * Set PHPExcel object
  326. *
  327. * @param PHPExcel $pPHPExcel PHPExcel object
  328. * @throws PHPExcel_Writer_Exception
  329. * @return PHPExcel_Writer_Excel2007
  330. */
  331. public function setPHPExcel(PHPExcel $pPHPExcel = null) {
  332. $this->_spreadSheet = $pPHPExcel;
  333. return $this;
  334. }
  335. /**
  336. * Get string table
  337. *
  338. * @return string[]
  339. */
  340. public function getStringTable() {
  341. return $this->_stringTable;
  342. }
  343. /**
  344. * Get PHPExcel_Style_Conditional HashTable
  345. *
  346. * @return PHPExcel_HashTable
  347. */
  348. public function getStylesConditionalHashTable() {
  349. return $this->_stylesConditionalHashTable;
  350. }
  351. /**
  352. * Get PHPExcel_Style_Fill HashTable
  353. *
  354. * @return PHPExcel_HashTable
  355. */
  356. public function getFillHashTable() {
  357. return $this->_fillHashTable;
  358. }
  359. /**
  360. * Get PHPExcel_Style_Font HashTable
  361. *
  362. * @return PHPExcel_HashTable
  363. */
  364. public function getFontHashTable() {
  365. return $this->_fontHashTable;
  366. }
  367. /**
  368. * Get PHPExcel_Style_Borders HashTable
  369. *
  370. * @return PHPExcel_HashTable
  371. */
  372. public function getBordersHashTable() {
  373. return $this->_bordersHashTable;
  374. }
  375. /**
  376. * Get PHPExcel_Style_NumberFormat HashTable
  377. *
  378. * @return PHPExcel_HashTable
  379. */
  380. public function getNumFmtHashTable() {
  381. return $this->_numFmtHashTable;
  382. }
  383. /**
  384. * Get PHPExcel_Worksheet_BaseDrawing HashTable
  385. *
  386. * @return PHPExcel_HashTable
  387. */
  388. public function getDrawingHashTable() {
  389. return $this->_drawingHashTable;
  390. }
  391. /**
  392. * Get Office2003 compatibility
  393. *
  394. * @return boolean
  395. */
  396. public function getOffice2003Compatibility() {
  397. return $this->_office2003compatibility;
  398. }
  399. /**
  400. * Set Office2003 compatibility
  401. *
  402. * @param boolean $pValue Office2003 compatibility?
  403. * @return PHPExcel_Writer_Excel2007
  404. */
  405. public function setOffice2003Compatibility($pValue = false) {
  406. $this->_office2003compatibility = $pValue;
  407. return $this;
  408. }
  409. }