Excel2007.php 16 KB

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