CSV.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 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_CSV
  23. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.8.0, 2014-03-02
  26. */
  27. /**
  28. * PHPExcel_Writer_CSV
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Writer_CSV
  32. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Writer_CSV extends PHPExcel_Writer_Abstract implements PHPExcel_Writer_IWriter {
  35. /**
  36. * PHPExcel object
  37. *
  38. * @var PHPExcel
  39. */
  40. private $_phpExcel;
  41. /**
  42. * Delimiter
  43. *
  44. * @var string
  45. */
  46. private $_delimiter = ',';
  47. /**
  48. * Enclosure
  49. *
  50. * @var string
  51. */
  52. private $_enclosure = '"';
  53. /**
  54. * Line ending
  55. *
  56. * @var string
  57. */
  58. private $_lineEnding = PHP_EOL;
  59. /**
  60. * Sheet index to write
  61. *
  62. * @var int
  63. */
  64. private $_sheetIndex = 0;
  65. /**
  66. * Whether to write a BOM (for UTF8).
  67. *
  68. * @var boolean
  69. */
  70. private $_useBOM = false;
  71. /**
  72. * Whether to write a fully Excel compatible CSV file.
  73. *
  74. * @var boolean
  75. */
  76. private $_excelCompatibility = false;
  77. /**
  78. * Create a new PHPExcel_Writer_CSV
  79. *
  80. * @param PHPExcel $phpExcel PHPExcel object
  81. */
  82. public function __construct(PHPExcel $phpExcel) {
  83. $this->_phpExcel = $phpExcel;
  84. }
  85. /**
  86. * Save PHPExcel to file
  87. *
  88. * @param string $pFilename
  89. * @throws PHPExcel_Writer_Exception
  90. */
  91. public function save($pFilename = null) {
  92. // Fetch sheet
  93. $sheet = $this->_phpExcel->getSheet($this->_sheetIndex);
  94. $saveDebugLog = PHPExcel_Calculation::getInstance($this->_phpExcel)->getDebugLog()->getWriteDebugLog();
  95. PHPExcel_Calculation::getInstance($this->_phpExcel)->getDebugLog()->setWriteDebugLog(FALSE);
  96. $saveArrayReturnType = PHPExcel_Calculation::getArrayReturnType();
  97. PHPExcel_Calculation::setArrayReturnType(PHPExcel_Calculation::RETURN_ARRAY_AS_VALUE);
  98. // Open file
  99. $fileHandle = fopen($pFilename, 'wb+');
  100. if ($fileHandle === false) {
  101. throw new PHPExcel_Writer_Exception("Could not open file $pFilename for writing.");
  102. }
  103. if ($this->_excelCompatibility) {
  104. fwrite($fileHandle, "\xEF\xBB\xBF"); // Enforce UTF-8 BOM Header
  105. $this->setEnclosure('"'); // Set enclosure to "
  106. $this->setDelimiter(";"); // Set delimiter to a semi-colon
  107. $this->setLineEnding("\r\n");
  108. fwrite($fileHandle, 'sep=' . $this->getDelimiter() . $this->_lineEnding);
  109. } elseif ($this->_useBOM) {
  110. // Write the UTF-8 BOM code if required
  111. fwrite($fileHandle, "\xEF\xBB\xBF");
  112. }
  113. // Identify the range that we need to extract from the worksheet
  114. $maxCol = $sheet->getHighestDataColumn();
  115. $maxRow = $sheet->getHighestDataRow();
  116. // Write rows to file
  117. for($row = 1; $row <= $maxRow; ++$row) {
  118. // Convert the row to an array...
  119. $cellsArray = $sheet->rangeToArray('A'.$row.':'.$maxCol.$row,'', $this->_preCalculateFormulas);
  120. // ... and write to the file
  121. $this->_writeLine($fileHandle, $cellsArray[0]);
  122. }
  123. // Close file
  124. fclose($fileHandle);
  125. PHPExcel_Calculation::setArrayReturnType($saveArrayReturnType);
  126. PHPExcel_Calculation::getInstance($this->_phpExcel)->getDebugLog()->setWriteDebugLog($saveDebugLog);
  127. }
  128. /**
  129. * Get delimiter
  130. *
  131. * @return string
  132. */
  133. public function getDelimiter() {
  134. return $this->_delimiter;
  135. }
  136. /**
  137. * Set delimiter
  138. *
  139. * @param string $pValue Delimiter, defaults to ,
  140. * @return PHPExcel_Writer_CSV
  141. */
  142. public function setDelimiter($pValue = ',') {
  143. $this->_delimiter = $pValue;
  144. return $this;
  145. }
  146. /**
  147. * Get enclosure
  148. *
  149. * @return string
  150. */
  151. public function getEnclosure() {
  152. return $this->_enclosure;
  153. }
  154. /**
  155. * Set enclosure
  156. *
  157. * @param string $pValue Enclosure, defaults to "
  158. * @return PHPExcel_Writer_CSV
  159. */
  160. public function setEnclosure($pValue = '"') {
  161. if ($pValue == '') {
  162. $pValue = null;
  163. }
  164. $this->_enclosure = $pValue;
  165. return $this;
  166. }
  167. /**
  168. * Get line ending
  169. *
  170. * @return string
  171. */
  172. public function getLineEnding() {
  173. return $this->_lineEnding;
  174. }
  175. /**
  176. * Set line ending
  177. *
  178. * @param string $pValue Line ending, defaults to OS line ending (PHP_EOL)
  179. * @return PHPExcel_Writer_CSV
  180. */
  181. public function setLineEnding($pValue = PHP_EOL) {
  182. $this->_lineEnding = $pValue;
  183. return $this;
  184. }
  185. /**
  186. * Get whether BOM should be used
  187. *
  188. * @return boolean
  189. */
  190. public function getUseBOM() {
  191. return $this->_useBOM;
  192. }
  193. /**
  194. * Set whether BOM should be used
  195. *
  196. * @param boolean $pValue Use UTF-8 byte-order mark? Defaults to false
  197. * @return PHPExcel_Writer_CSV
  198. */
  199. public function setUseBOM($pValue = false) {
  200. $this->_useBOM = $pValue;
  201. return $this;
  202. }
  203. /**
  204. * Get whether the file should be saved with full Excel Compatibility
  205. *
  206. * @return boolean
  207. */
  208. public function getExcelCompatibility() {
  209. return $this->_excelCompatibility;
  210. }
  211. /**
  212. * Set whether the file should be saved with full Excel Compatibility
  213. *
  214. * @param boolean $pValue Set the file to be written as a fully Excel compatible csv file
  215. * Note that this overrides other settings such as useBOM, enclosure and delimiter
  216. * @return PHPExcel_Writer_CSV
  217. */
  218. public function setExcelCompatibility($pValue = false) {
  219. $this->_excelCompatibility = $pValue;
  220. return $this;
  221. }
  222. /**
  223. * Get sheet index
  224. *
  225. * @return int
  226. */
  227. public function getSheetIndex() {
  228. return $this->_sheetIndex;
  229. }
  230. /**
  231. * Set sheet index
  232. *
  233. * @param int $pValue Sheet index
  234. * @return PHPExcel_Writer_CSV
  235. */
  236. public function setSheetIndex($pValue = 0) {
  237. $this->_sheetIndex = $pValue;
  238. return $this;
  239. }
  240. /**
  241. * Write line to CSV file
  242. *
  243. * @param mixed $pFileHandle PHP filehandle
  244. * @param array $pValues Array containing values in a row
  245. * @throws PHPExcel_Writer_Exception
  246. */
  247. private function _writeLine($pFileHandle = null, $pValues = null) {
  248. if (is_array($pValues)) {
  249. // No leading delimiter
  250. $writeDelimiter = false;
  251. // Build the line
  252. $line = '';
  253. foreach ($pValues as $element) {
  254. // Escape enclosures
  255. $element = str_replace($this->_enclosure, $this->_enclosure . $this->_enclosure, $element);
  256. // Add delimiter
  257. if ($writeDelimiter) {
  258. $line .= $this->_delimiter;
  259. } else {
  260. $writeDelimiter = true;
  261. }
  262. // Add enclosed string
  263. $line .= $this->_enclosure . $element . $this->_enclosure;
  264. }
  265. // Add line ending
  266. $line .= $this->_lineEnding;
  267. // Write to file
  268. fwrite($pFileHandle, $line);
  269. } else {
  270. throw new PHPExcel_Writer_Exception("Invalid data row passed to CSV writer.");
  271. }
  272. }
  273. }