/classes/worksheet.php

https://github.com/bobzhai/kohana-phpexcel · PHP · 315 lines · 146 code · 10 blank · 159 comment · 11 complexity · fe2ec080429cf7dbdbe6d8a8abac411c MD5 · raw file

  1. <?php defined('SYSPATH') or die('No direct access allowed.');
  2. /**
  3. * PHP Excel library. Helper class to make spreadsheet creation easier.
  4. *
  5. * @package Spreadsheet
  6. * @author Korney Czukowski
  7. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  8. */
  9. class Worksheet
  10. {
  11. /**
  12. * Flag whether to size column widths automatically
  13. *
  14. * @var boolean
  15. */
  16. protected $auto_size = FALSE;
  17. /**
  18. * Column names, may be associative. Order of this array defines columns order of this worksheet.
  19. * The following examples correspond with the examples for `$data` property.
  20. *
  21. * Example 1:
  22. *
  23. * $columns = array('First Name', 'Last Name', 'Phone');
  24. *
  25. * Example 2:
  26. *
  27. * $columns = array(
  28. * 'first_name' => 'First Name',
  29. * 'last_name' => 'Last Name',
  30. * 'phone' => 'Phone',
  31. * );
  32. *
  33. * Example 3:
  34. *
  35. * $columns = array(
  36. * 'first_name' => 'First Name',
  37. * 'last_name' => 'Last Name',
  38. * 'phone' => 'Phone',
  39. * 'full_name' => 'Full name',
  40. * );
  41. *
  42. * @var array
  43. */
  44. protected $columns = array();
  45. /**
  46. * This array keeps data rows.
  47. *
  48. * Example 1:
  49. *
  50. * $data = array(
  51. * array('Martin', 'Hoover', '207-422-9702'), // Note: these data are completely made up
  52. * array('Anna', 'Lantz', '208-704-9524'),
  53. * );
  54. *
  55. * Example 2:
  56. *
  57. * $data = array(
  58. * array(
  59. * 'first_name' => 'Martin',
  60. * 'last_name' => 'Hoover',
  61. * 'phone' => '207-422-9702',
  62. * ),
  63. * array(
  64. * 'phone' => '208-704-9524',
  65. * 'last_name' => 'Lantz',
  66. * 'first_name' => 'Anna',
  67. * ),
  68. * );
  69. *
  70. * Example 3:
  71. *
  72. * class Person {
  73. * public function full_name() {
  74. * return $this->first_name.' '.$this->last_name;
  75. * }
  76. * }
  77. * $data = array(
  78. * Person {
  79. * $first_name => 'Martin',
  80. * $last_name => 'Hoover',
  81. * $phone => '207-422-9702',
  82. * },
  83. * Person {
  84. * $first_name => 'Anna',
  85. * $last_name => 'Lantz',
  86. * $phone => '208-704-9524',
  87. * },
  88. * );
  89. *
  90. * Note: the data in these examples are all completely made up and generated by fakenamegenerator.com
  91. *
  92. * @var array
  93. */
  94. protected $data = array();
  95. /**
  96. * Flag whether to include column names as the 1st row of the worksheet
  97. *
  98. * @var boolean
  99. */
  100. protected $include_names = FALSE;
  101. /**
  102. * Column formats. It needs to be in a similar manner like `$columns` property and the values may be either
  103. * literal or `PHPExcel_Style_NumberFormat` constants. Default value is `PHPExcel_Style_NumberFormat::FORMAT_GENERAL`.
  104. *
  105. * @var array
  106. */
  107. protected $formats = array();
  108. /**
  109. * Default worksheet title. This property is only used by class constructor
  110. *
  111. * @var string
  112. */
  113. protected $title;
  114. /**
  115. * Column data types. It needs to be in a similar manner like `$columns` property and the values may be either
  116. * literal or `PHPExcel_Cell_DataType` constants. Default value is `PHPExcel_Cell_DataType::TYPE_STRING`.
  117. *
  118. * @var array
  119. */
  120. protected $types = array();
  121. /**
  122. * PHPExcel Worksheet instance
  123. *
  124. * @var PHPExcel_Worksheet
  125. */
  126. protected $_worksheet;
  127. /**
  128. * Class constructor
  129. * @param PHPExcel_Worksheet $worksheet
  130. */
  131. public function __construct(PHPExcel $spreadsheet, PHPExcel_Worksheet $worksheet = NULL)
  132. {
  133. if ($worksheet === NULL)
  134. {
  135. $this->_worksheet = new PHPExcel_Worksheet($spreadsheet);
  136. }
  137. else
  138. {
  139. $this->_worksheet = $worksheet;
  140. }
  141. // Add worksheet to a spreadsheet
  142. $spreadsheet->addSheet($this->_worksheet);
  143. // Set worksheet title
  144. if ($this->title !== NULL)
  145. {
  146. $this->title($this->title);
  147. }
  148. }
  149. /**
  150. * Columns getter/setter
  151. */
  152. public function columns($key = NULL, $value = NULL)
  153. {
  154. return $this->_get_set('columns', $key, $value);
  155. }
  156. /**
  157. * Data getter/setter
  158. */
  159. public function data($key = NULL, $value = NULL)
  160. {
  161. return $this->_get_set('data', $key, $value);
  162. }
  163. /**
  164. * Column formats getter/setter
  165. */
  166. public function formats($key = NULL, $value = NULL)
  167. {
  168. return $this->_get_set('formats', $key, $value);
  169. }
  170. /**
  171. * Inserts data into worksheet and returns it
  172. *
  173. * @return PHPExcel_Worksheet
  174. */
  175. public function render()
  176. {
  177. // Set worksheet header
  178. if ($this->include_names)
  179. {
  180. $this->_set_row(1, $this->columns, TRUE);
  181. $offset = 2;
  182. }
  183. else
  184. {
  185. $offset = 1;
  186. }
  187. // Set data
  188. $rows = 0;
  189. foreach ($this->data as $row => $data)
  190. {
  191. $this->_set_row($row + $offset, $data);
  192. $rows++;
  193. }
  194. // Set column styles and width
  195. $column = 0;
  196. foreach ($this->columns as $key => $name)
  197. {
  198. $column_dim = PHPExcel_Cell::stringFromColumnIndex($column);
  199. $format = Arr::get($this->formats, $key);
  200. if ($format !== NULL)
  201. {
  202. $this->_worksheet->getStyle($column_dim.$offset.':'.$column_dim.($offset + $rows))
  203. ->getNumberFormat()
  204. ->setFormatCode($format);
  205. }
  206. if ($this->auto_size === TRUE)
  207. {
  208. $this->_worksheet
  209. ->getColumnDimension($column_dim)
  210. ->setAutoSize(TRUE);
  211. }
  212. $column++;
  213. }
  214. return $this->_worksheet;
  215. }
  216. /**
  217. * Worksheet title getter/setter
  218. */
  219. public function title($title = NULL)
  220. {
  221. if ($title === NULL)
  222. {
  223. return $this->_worksheet->getTitle();
  224. }
  225. else
  226. {
  227. $this->_worksheet->setTitle($title);
  228. return $this;
  229. }
  230. }
  231. /**
  232. * Common getter/setter method
  233. *
  234. * @param string $property
  235. * @param mixed $key
  236. * @param mixed $value
  237. * @return mixed
  238. */
  239. private function _get_set($property, $key = NULL, $value = NULL)
  240. {
  241. if ($key === NULL)
  242. {
  243. return $this->{$property};
  244. }
  245. elseif (is_array($key) OR $key instanceof Iterator)
  246. {
  247. $this->{$property} = $key;
  248. }
  249. else
  250. {
  251. $this->{$property}[$key] = $value;
  252. }
  253. return $this;
  254. }
  255. /**
  256. * Sets cells of a single row
  257. *
  258. * @param int $row
  259. * @param mixed $cell_values
  260. * @param boolean $header
  261. * @return Worksheet
  262. */
  263. private function _set_row($row, &$data, $header = FALSE)
  264. {
  265. $column = 0;
  266. $format = NULL;
  267. $type = PHPExcel_Cell_DataType::TYPE_STRING;
  268. foreach ($this->columns as $key => $name)
  269. {
  270. $value = NULL;
  271. if (is_array($data))
  272. {
  273. $value = $data[$key];
  274. }
  275. elseif (is_object($data))
  276. {
  277. if (method_exists($data, $key))
  278. {
  279. $value = $data->$key();
  280. }
  281. elseif (isset($data->$key))
  282. {
  283. $value = $data->$key;
  284. }
  285. }
  286. // Determine cell type and format
  287. if ($header === FALSE)
  288. {
  289. $type = Arr::get($this->types, $key);
  290. }
  291. // Set cell value
  292. $coordinates = PHPExcel_Cell::stringFromColumnIndex($column).$row;
  293. if ($type !== NULL)
  294. {
  295. $this->_worksheet->setCellValueExplicit($coordinates, $value, $type);
  296. }
  297. else
  298. {
  299. $this->_worksheet->setCellValue($coordinates, $value);
  300. }
  301. $column++;
  302. }
  303. return $this;
  304. }
  305. }