PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

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