CSV.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2013 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 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.9, 2013-06-02
  26. */
  27. /**
  28. * PHPExcel_Writer_CSV
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Writer_CSV
  32. * @copyright Copyright (c) 2006 - 2013 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. // Write the UTF-16LE BOM code
  105. fwrite($fileHandle, "\xFF\xFE"); // Excel uses UTF-16LE encoding
  106. $this->setEnclosure(); // Default enclosure is "
  107. $this->setDelimiter("\t"); // Excel delimiter is a TAB
  108. } elseif ($this->_useBOM) {
  109. // Write the UTF-8 BOM code
  110. fwrite($fileHandle, "\xEF\xBB\xBF");
  111. }
  112. // Identify the range that we need to extract from the worksheet
  113. $maxCol = $sheet->getHighestColumn();
  114. $maxRow = $sheet->getHighestRow();
  115. // Write rows to file
  116. for($row = 1; $row <= $maxRow; ++$row) {
  117. // Convert the row to an array...
  118. $cellsArray = $sheet->rangeToArray('A'.$row.':'.$maxCol.$row,'', $this->_preCalculateFormulas);
  119. // ... and write to the file
  120. $this->_writeLine($fileHandle, $cellsArray[0]);
  121. }
  122. // Close file
  123. fclose($fileHandle);
  124. PHPExcel_Calculation::setArrayReturnType($saveArrayReturnType);
  125. PHPExcel_Calculation::getInstance($this->_phpExcel)->getDebugLog()->setWriteDebugLog($saveDebugLog);
  126. }
  127. /**
  128. * Get delimiter
  129. *
  130. * @return string
  131. */
  132. public function getDelimiter() {
  133. return $this->_delimiter;
  134. }
  135. /**
  136. * Set delimiter
  137. *
  138. * @param string $pValue Delimiter, defaults to ,
  139. * @return PHPExcel_Writer_CSV
  140. */
  141. public function setDelimiter($pValue = ',') {
  142. $this->_delimiter = $pValue;
  143. return $this;
  144. }
  145. /**
  146. * Get enclosure
  147. *
  148. * @return string
  149. */
  150. public function getEnclosure() {
  151. return $this->_enclosure;
  152. }
  153. /**
  154. * Set enclosure
  155. *
  156. * @param string $pValue Enclosure, defaults to "
  157. * @return PHPExcel_Writer_CSV
  158. */
  159. public function setEnclosure($pValue = '"') {
  160. if ($pValue == '') {
  161. $pValue = null;
  162. }
  163. $this->_enclosure = $pValue;
  164. return $this;
  165. }
  166. /**
  167. * Get line ending
  168. *
  169. * @return string
  170. */
  171. public function getLineEnding() {
  172. return $this->_lineEnding;
  173. }
  174. /**
  175. * Set line ending
  176. *
  177. * @param string $pValue Line ending, defaults to OS line ending (PHP_EOL)
  178. * @return PHPExcel_Writer_CSV
  179. */
  180. public function setLineEnding($pValue = PHP_EOL) {
  181. $this->_lineEnding = $pValue;
  182. return $this;
  183. }
  184. /**
  185. * Get whether BOM should be used
  186. *
  187. * @return boolean
  188. */
  189. public function getUseBOM() {
  190. return $this->_useBOM;
  191. }
  192. /**
  193. * Set whether BOM should be used
  194. *
  195. * @param boolean $pValue Use UTF-8 byte-order mark? Defaults to false
  196. * @return PHPExcel_Writer_CSV
  197. */
  198. public function setUseBOM($pValue = false) {
  199. $this->_useBOM = $pValue;
  200. return $this;
  201. }
  202. /**
  203. * Get whether the file should be saved with full Excel Compatibility
  204. *
  205. * @return boolean
  206. */
  207. public function getExcelCompatibility() {
  208. return $this->_excelCompatibility;
  209. }
  210. /**
  211. * Set whether the file should be saved with full Excel Compatibility
  212. *
  213. * @param boolean $pValue Set the file to be written as a fully Excel compatible csv file
  214. * Note that this overrides other settings such as useBOM, enclosure and delimiter
  215. * @return PHPExcel_Writer_CSV
  216. */
  217. public function setExcelCompatibility($pValue = false) {
  218. $this->_excelCompatibility = $pValue;
  219. return $this;
  220. }
  221. /**
  222. * Get sheet index
  223. *
  224. * @return int
  225. */
  226. public function getSheetIndex() {
  227. return $this->_sheetIndex;
  228. }
  229. /**
  230. * Set sheet index
  231. *
  232. * @param int $pValue Sheet index
  233. * @return PHPExcel_Writer_CSV
  234. */
  235. public function setSheetIndex($pValue = 0) {
  236. $this->_sheetIndex = $pValue;
  237. return $this;
  238. }
  239. /**
  240. * Write line to CSV file
  241. *
  242. * @param mixed $pFileHandle PHP filehandle
  243. * @param array $pValues Array containing values in a row
  244. * @throws PHPExcel_Writer_Exception
  245. */
  246. private function _writeLine($pFileHandle = null, $pValues = null) {
  247. if (is_array($pValues)) {
  248. // No leading delimiter
  249. $writeDelimiter = false;
  250. // Build the line
  251. $line = '';
  252. foreach ($pValues as $element) {
  253. // Escape enclosures
  254. $element = str_replace($this->_enclosure, $this->_enclosure . $this->_enclosure, $element);
  255. // Add delimiter
  256. if ($writeDelimiter) {
  257. $line .= $this->_delimiter;
  258. } else {
  259. $writeDelimiter = true;
  260. }
  261. // Add enclosed string
  262. $line .= $this->_enclosure . $element . $this->_enclosure;
  263. }
  264. // Add line ending
  265. $line .= $this->_lineEnding;
  266. // Write to file
  267. if ($this->_excelCompatibility) {
  268. fwrite($pFileHandle, mb_convert_encoding($line,"UTF-16LE","UTF-8"));
  269. } else {
  270. fwrite($pFileHandle, $line);
  271. }
  272. } else {
  273. throw new PHPExcel_Writer_Exception("Invalid data row passed to CSV writer.");
  274. }
  275. }
  276. }