PageRenderTime 29ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/v1.6.0/Classes/PHPExcel/Writer/Excel5.php

#
PHP | 388 lines | 215 code | 50 blank | 123 comment | 29 complexity | fd249e7603195c4e5cfd6692d0ffa288 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-3.0, LGPL-3.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2008 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
  23. * @copyright Copyright (c) 2006 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/lgpl.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. /** PHPExcel_IWriter */
  28. require_once 'PHPExcel/Writer/IWriter.php';
  29. /** PHPExcel_Cell */
  30. require_once 'PHPExcel/Cell.php';
  31. /** PHPExcel_Writer_Excel5_Writer */
  32. require_once 'PHPExcel/Writer/Excel5/Writer.php';
  33. /** PHPExcel_RichText */
  34. require_once 'PHPExcel/RichText.php';
  35. /**
  36. * PHPExcel_Writer_Excel5
  37. *
  38. * @category PHPExcel
  39. * @package PHPExcel_Writer
  40. * @copyright Copyright (c) 2006 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  41. */
  42. class PHPExcel_Writer_Excel5 implements PHPExcel_Writer_IWriter {
  43. /**
  44. * PHPExcel object
  45. *
  46. * @var PHPExcel
  47. */
  48. private $_phpExcel;
  49. /**
  50. * Temporary storage directory
  51. *
  52. * @var string
  53. */
  54. private $_tempDir = '';
  55. /**
  56. * Color cache
  57. */
  58. private $_colors = array();
  59. /**
  60. * Create a new PHPExcel_Writer_Excel5
  61. *
  62. * @param PHPExcel $phpExcel PHPExcel object
  63. */
  64. public function __construct(PHPExcel $phpExcel) {
  65. $this->_phpExcel = $phpExcel;
  66. $this->_tempDir = '';
  67. $this->_colors = array();
  68. }
  69. /**
  70. * Save PHPExcel to file
  71. *
  72. * @param string $pFileName
  73. * @throws Exception
  74. */
  75. public function save($pFilename = null) {
  76. $this->_colors = array();
  77. $phpExcel = $this->_phpExcel;
  78. $workbook = new PHPExcel_Writer_Excel5_Writer($pFilename);
  79. $workbook->setVersion(8);
  80. // Set temp dir
  81. if ($this->_tempDir != '') {
  82. $workbook->setTempDir($this->_tempDir);
  83. }
  84. // Create empty style
  85. $emptyStyle = new PHPExcel_Style();
  86. // Add empty sheets
  87. foreach ($phpExcel->getSheetNames() as $sheetIndex => $sheetName) {
  88. $phpSheet = $phpExcel->getSheet($sheetIndex);
  89. $worksheet = $workbook->addWorksheet($sheetName);
  90. }
  91. $allWorksheets = $workbook->worksheets();
  92. // Add full sheet data
  93. foreach ($phpExcel->getSheetNames() as $sheetIndex => $sheetName) {
  94. $phpSheet = $phpExcel->getSheet($sheetIndex);
  95. $worksheet = $allWorksheets[$sheetIndex];
  96. $worksheet->setInputEncoding("UTF-8");
  97. $aStyles = $phpSheet->getStyles();
  98. $freeze = $phpSheet->getFreezePane();
  99. if ($freeze) {
  100. list($column, $row) = PHPExcel_Cell::coordinateFromString($freeze);
  101. $worksheet->freezePanes(array($row - 1, PHPExcel_Cell::columnIndexFromString($column) - 1));
  102. }
  103. //if ($sheetIndex == $phpExcel->getActiveSheetIndex()) {
  104. // $worksheet->select();
  105. //}
  106. if ($phpSheet->getProtection()->getSheet()) {
  107. $worksheet->protect($phpSheet->getProtection()->getPassword(), true);
  108. }
  109. if (!$phpSheet->getShowGridlines()) {
  110. $worksheet->hideGridLines();
  111. }
  112. $formats = array();
  113. foreach ($phpSheet->getCellCollection() as $cell) {
  114. $row = $cell->getRow() - 1;
  115. $column = PHPExcel_Cell::columnIndexFromString($cell->getColumn()) - 1;
  116. // Don't break Excel!
  117. if ($row + 1 >= 65569) {
  118. break;
  119. }
  120. $style = $emptyStyle;
  121. if (isset($aStyles[$cell->getCoordinate()])) {
  122. $style = $aStyles[$cell->getCoordinate()];
  123. }
  124. $styleHash = $style->getHashCode();
  125. if (!isset($formats[$styleHash])) {
  126. $formats[$styleHash] = $workbook->addFormat(array(
  127. 'HAlign' => $style->getAlignment()->getHorizontal(),
  128. 'VAlign' => $this->_mapVAlign($style->getAlignment()->getVertical()),
  129. 'TextRotation' => $style->getAlignment()->getTextRotation(),
  130. 'Bold' => $style->getFont()->getBold(),
  131. 'FontFamily' => $style->getFont()->getName(),
  132. 'Color' => $this->_addColor($workbook, $style->getFont()->getColor()->getRGB()),
  133. 'Underline' => $this->_mapUnderline($style->getFont()->getUnderline()),
  134. 'Size' => $style->getFont()->getSize(),
  135. //~ 'Script' => $style->getSuperscript(),
  136. 'NumFormat' => iconv("UTF-8", "Windows-1252", $style->getNumberFormat()->getFormatCode()),
  137. 'Bottom' => $this->_mapBorderStyle($style->getBorders()->getBottom()->getBorderStyle()),
  138. 'Top' => $this->_mapBorderStyle($style->getBorders()->getTop()->getBorderStyle()),
  139. 'Left' => $this->_mapBorderStyle($style->getBorders()->getLeft()->getBorderStyle()),
  140. 'Right' => $this->_mapBorderStyle($style->getBorders()->getRight()->getBorderStyle()),
  141. 'BottomColor' => $this->_addColor($workbook, $style->getBorders()->getBottom()->getColor()->getRGB()),
  142. 'TopColor' => $this->_addColor($workbook, $style->getBorders()->getTop()->getColor()->getRGB()),
  143. 'RightColor' => $this->_addColor($workbook, $style->getBorders()->getRight()->getColor()->getRGB()),
  144. 'LeftColor' => $this->_addColor($workbook, $style->getBorders()->getLeft()->getColor()->getRGB()),
  145. 'FgColor' => $this->_addColor($workbook, $style->getFill()->getStartColor()->getRGB()),
  146. 'BgColor' => $this->_addColor($workbook, $style->getFill()->getEndColor()->getRGB()),
  147. 'Pattern' => $this->_mapFillType($style->getFill()->getFillType()),
  148. ));
  149. if ($style->getAlignment()->getWrapText()) {
  150. $formats[$styleHash]->setTextWrap();
  151. }
  152. if ($style->getFont()->getItalic()) {
  153. $formats[$styleHash]->setItalic();
  154. }
  155. if ($style->getFont()->getStriketrough()) {
  156. $formats[$styleHash]->setStrikeOut();
  157. }
  158. }
  159. // Write cell value
  160. if ($cell->getValue() instanceof PHPExcel_RichText) {
  161. $worksheet->write($row, $column, $cell->getValue()->getPlainText(), $formats[$styleHash]);
  162. } else {
  163. // Hyperlink?
  164. if ($cell->hasHyperlink()) {
  165. $worksheet->writeUrl($row, $column, str_replace('sheet://', 'internal:', $cell->getHyperlink()->getUrl()), $cell->getValue(), $formats[$styleHash]);
  166. } else {
  167. $worksheet->write($row, $column, $cell->getValue(), $formats[$styleHash]);
  168. }
  169. }
  170. }
  171. $phpSheet->calculateColumnWidths();
  172. foreach ($phpSheet->getColumnDimensions() as $columnDimension) {
  173. $column = PHPExcel_Cell::columnIndexFromString($columnDimension->getColumnIndex()) - 1;
  174. $worksheet->setColumn( $column, $column, $columnDimension->getWidth(), null, ($columnDimension->getVisible() ? '0' : '1') );
  175. }
  176. foreach ($phpSheet->getRowDimensions() as $rowDimension) {
  177. $worksheet->setRow( $rowDimension->getRowIndex() - 1, $rowDimension->getRowHeight(), null, ($rowDimension->getVisible() ? '0' : '1') );
  178. }
  179. foreach ($phpSheet->getMergeCells() as $cells) {
  180. list($first, $last) = PHPExcel_Cell::splitRange($cells);
  181. list($firstColumn, $firstRow) = PHPExcel_Cell::coordinateFromString($first);
  182. list($lastColumn, $lastRow) = PHPExcel_Cell::coordinateFromString($last);
  183. $worksheet->mergeCells($firstRow - 1, PHPExcel_Cell::columnIndexFromString($firstColumn) - 1, $lastRow - 1, PHPExcel_Cell::columnIndexFromString($lastColumn) - 1);
  184. }
  185. foreach ($phpSheet->getDrawingCollection() as $drawing) {
  186. if ($drawing instanceof PHPExcel_Worksheet_BaseDrawing) {
  187. $filename = $drawing->getPath();
  188. $imagesize = getimagesize($filename);
  189. switch ($imagesize[2]) {
  190. case 1: $image = imagecreatefromgif($filename); break;
  191. case 2: $image = imagecreatefromjpeg($filename); break;
  192. case 3: $image = imagecreatefrompng($filename); break;
  193. default: continue 2;
  194. }
  195. list($column, $row) = PHPExcel_Cell::coordinateFromString($drawing->getCoordinates());
  196. $worksheet->insertBitmap($row - 1, PHPExcel_Cell::columnIndexFromString($column) - 1, $image, $drawing->getOffsetX(), $drawing->getOffsetY(), $drawing->getWidth() / $imagesize[0], $drawing->getHeight() / $imagesize[1]);
  197. }
  198. }
  199. // page setup
  200. if ($phpSheet->getPageSetup()->getOrientation() == PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE) {
  201. $worksheet->setLandscape();
  202. }
  203. $worksheet->setPaper($phpSheet->getPageSetup()->getPaperSize());
  204. $worksheet->setHeader($phpSheet->getHeaderFooter()->getOddHeader(), $phpSheet->getPageMargins()->getHeader());
  205. $worksheet->setFooter($phpSheet->getHeaderFooter()->getOddFooter(), $phpSheet->getPageMargins()->getFooter());
  206. $worksheet->setMarginLeft($phpSheet->getPageMargins()->getLeft());
  207. $worksheet->setMarginRight($phpSheet->getPageMargins()->getRight());
  208. $worksheet->setMarginTop($phpSheet->getPageMargins()->getTop());
  209. $worksheet->setMarginBottom($phpSheet->getPageMargins()->getBottom());
  210. // -------------------------------------------------------------------
  211. // Commented due to bug:
  212. // http://pear.php.net/bugs/bug.php?id=2146
  213. // -------------------------------------------------------------------
  214. // if ($phpSheet->getPageSetup()->isPrintAreaSet()) {
  215. // // Print area
  216. // $printArea = PHPExcel_Cell::splitRange($phpSheet->getPageSetup()->getPrintArea());
  217. // $printArea[0] = PHPExcel_Cell::coordinateFromString($printArea[0]);
  218. // $printArea[1] = PHPExcel_Cell::coordinateFromString($printArea[1]);
  219. //
  220. // $worksheet->printArea(
  221. // $printArea[0][1],
  222. // PHPExcel_Cell::columnIndexFromString($printArea[0][0]) - 1,
  223. // $printArea[1][1],
  224. // PHPExcel_Cell::columnIndexFromString($printArea[1][0]) - 1
  225. // );
  226. // }
  227. }
  228. $workbook->close();
  229. }
  230. /**
  231. * Add color
  232. */
  233. private function _addColor($workbook, $rgb) {
  234. if (!isset($this->_colors[$rgb])) {
  235. $workbook->setCustomColor(8 + count($this->_colors), hexdec(substr($rgb, 0, 2)), hexdec(substr($rgb, 2, 2)), hexdec(substr($rgb, 4)));
  236. $this->_colors[$rgb] = 8 + count($this->_colors);
  237. }
  238. return $this->_colors[$rgb];
  239. }
  240. /**
  241. * Map border style
  242. */
  243. private function _mapBorderStyle($borderStyle) {
  244. switch ($borderStyle) {
  245. case PHPExcel_Style_Border::BORDER_NONE: return 0;
  246. case PHPExcel_Style_Border::BORDER_THICK: return 2;
  247. default: return 1; // map others to thin
  248. }
  249. }
  250. /**
  251. * Map underline
  252. */
  253. private function _mapUnderline($underline) {
  254. switch ($underline) {
  255. case PHPExcel_Style_Font::UNDERLINE_NONE:
  256. return 0;
  257. case PHPExcel_Style_Font::UNDERLINE_DOUBLE:
  258. case PHPExcel_Style_Font::UNDERLINE_DOUBLEACCOUNTING:
  259. return 2;
  260. default:
  261. return 1; // map others to single
  262. }
  263. }
  264. /**
  265. * Map fill type
  266. */
  267. private function _mapFillType($fillType) {
  268. switch ($fillType) { // just a guess
  269. case PHPExcel_Style_Fill::FILL_NONE: return 0;
  270. case PHPExcel_Style_Fill::FILL_SOLID: return 1;
  271. case PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR: return 2;
  272. case PHPExcel_Style_Fill::FILL_GRADIENT_PATH: return 3;
  273. case PHPExcel_Style_Fill::FILL_PATTERN_DARKDOWN: return 4;
  274. case PHPExcel_Style_Fill::FILL_PATTERN_DARKGRAY: return 5;
  275. case PHPExcel_Style_Fill::FILL_PATTERN_DARKGRID: return 6;
  276. case PHPExcel_Style_Fill::FILL_PATTERN_DARKHORIZONTAL: return 7;
  277. case PHPExcel_Style_Fill::FILL_PATTERN_DARKTRELLIS: return 8;
  278. case PHPExcel_Style_Fill::FILL_PATTERN_DARKUP: return 9;
  279. case PHPExcel_Style_Fill::FILL_PATTERN_DARKVERTICAL: return 10;
  280. case PHPExcel_Style_Fill::FILL_PATTERN_GRAY0625: return 11;
  281. case PHPExcel_Style_Fill::FILL_PATTERN_GRAY125: return 12;
  282. case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTDOWN: return 13;
  283. case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRAY: return 14;
  284. case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRID: return 15;
  285. case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTHORIZONTAL: return 16;
  286. case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTTRELLIS: return 17;
  287. case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTUP: return 18;
  288. case PHPExcel_Style_Fill::FILL_PATTERN_LIGHTVERTICAL: return 19;
  289. case PHPExcel_Style_Fill::FILL_PATTERN_MEDIUMGRAY: return 20;
  290. }
  291. return 0;
  292. }
  293. /**
  294. * Map VAlign
  295. */
  296. private function _mapVAlign($vAlign) {
  297. return ($vAlign == 'center' || $vAlign == 'justify' ? 'v' : '') . $vAlign;
  298. }
  299. /**
  300. * Get an array of all styles
  301. *
  302. * @param PHPExcel $pPHPExcel
  303. * @return PHPExcel_Style[] All styles in PHPExcel
  304. * @throws Exception
  305. */
  306. private function _allStyles(PHPExcel $pPHPExcel = null)
  307. {
  308. // Get an array of all styles
  309. $aStyles = array();
  310. for ($i = 0; $i < $pPHPExcel->getSheetCount(); $i++) {
  311. foreach ($pPHPExcel->getSheet($i)->getStyles() as $style) {
  312. $aStyles[] = $style;
  313. }
  314. }
  315. return $aStyles;
  316. }
  317. /**
  318. * Get temporary storage directory
  319. *
  320. * @return string
  321. */
  322. public function getTempDir() {
  323. return $this->_tempDir;
  324. }
  325. /**
  326. * Set temporary storage directory
  327. *
  328. * @param string $pValue Temporary storage directory
  329. * @throws Exception Exception when directory does not exist
  330. */
  331. public function setTempDir($pValue = '') {
  332. if (is_dir($pValue)) {
  333. $this->_tempDir = $pValue;
  334. } else {
  335. throw new Exception("Directory does not exist: $pValue");
  336. }
  337. }
  338. }