123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- <?php
- class PHPExcel_Writer_CSV extends PHPExcel_Writer_Abstract implements PHPExcel_Writer_IWriter {
-
- private $_phpExcel;
-
- private $_delimiter = ',';
-
- private $_enclosure = '"';
-
- private $_lineEnding = PHP_EOL;
-
- private $_sheetIndex = 0;
-
- private $_useBOM = false;
-
- private $_excelCompatibility = false;
-
- public function __construct(PHPExcel $phpExcel) {
- $this->_phpExcel = $phpExcel;
- }
-
- public function save($pFilename = null) {
-
- $sheet = $this->_phpExcel->getSheet($this->_sheetIndex);
- $saveDebugLog = PHPExcel_Calculation::getInstance($this->_phpExcel)->getDebugLog()->getWriteDebugLog();
- PHPExcel_Calculation::getInstance($this->_phpExcel)->getDebugLog()->setWriteDebugLog(FALSE);
- $saveArrayReturnType = PHPExcel_Calculation::getArrayReturnType();
- PHPExcel_Calculation::setArrayReturnType(PHPExcel_Calculation::RETURN_ARRAY_AS_VALUE);
-
- $fileHandle = fopen($pFilename, 'wb+');
- if ($fileHandle === false) {
- throw new PHPExcel_Writer_Exception("Could not open file $pFilename for writing.");
- }
- if ($this->_excelCompatibility) {
- fwrite($fileHandle, "\xEF\xBB\xBF");
- $this->setEnclosure('"');
- $this->setDelimiter(";");
- $this->setLineEnding("\r\n");
- fwrite($fileHandle, 'sep=' . $this->getDelimiter() . $this->_lineEnding);
- } elseif ($this->_useBOM) {
-
- fwrite($fileHandle, "\xEF\xBB\xBF");
- }
-
- $maxCol = $sheet->getHighestDataColumn();
- $maxRow = $sheet->getHighestDataRow();
-
- for($row = 1; $row <= $maxRow; ++$row) {
-
- $cellsArray = $sheet->rangeToArray('A'.$row.':'.$maxCol.$row,'', $this->_preCalculateFormulas);
-
- $this->_writeLine($fileHandle, $cellsArray[0]);
- }
-
- fclose($fileHandle);
- PHPExcel_Calculation::setArrayReturnType($saveArrayReturnType);
- PHPExcel_Calculation::getInstance($this->_phpExcel)->getDebugLog()->setWriteDebugLog($saveDebugLog);
- }
-
- public function getDelimiter() {
- return $this->_delimiter;
- }
-
- public function setDelimiter($pValue = ',') {
- $this->_delimiter = $pValue;
- return $this;
- }
-
- public function getEnclosure() {
- return $this->_enclosure;
- }
-
- public function setEnclosure($pValue = '"') {
- if ($pValue == '') {
- $pValue = null;
- }
- $this->_enclosure = $pValue;
- return $this;
- }
-
- public function getLineEnding() {
- return $this->_lineEnding;
- }
-
- public function setLineEnding($pValue = PHP_EOL) {
- $this->_lineEnding = $pValue;
- return $this;
- }
-
- public function getUseBOM() {
- return $this->_useBOM;
- }
-
- public function setUseBOM($pValue = false) {
- $this->_useBOM = $pValue;
- return $this;
- }
-
- public function getExcelCompatibility() {
- return $this->_excelCompatibility;
- }
-
- public function setExcelCompatibility($pValue = false) {
- $this->_excelCompatibility = $pValue;
- return $this;
- }
-
- public function getSheetIndex() {
- return $this->_sheetIndex;
- }
-
- public function setSheetIndex($pValue = 0) {
- $this->_sheetIndex = $pValue;
- return $this;
- }
-
- private function _writeLine($pFileHandle = null, $pValues = null) {
- if (is_array($pValues)) {
-
- $writeDelimiter = false;
-
- $line = '';
- foreach ($pValues as $element) {
-
- $element = str_replace($this->_enclosure, $this->_enclosure . $this->_enclosure, $element);
-
- if ($writeDelimiter) {
- $line .= $this->_delimiter;
- } else {
- $writeDelimiter = true;
- }
-
- $line .= $this->_enclosure . $element . $this->_enclosure;
- }
-
- $line .= $this->_lineEnding;
-
- fwrite($pFileHandle, $line);
- } else {
- throw new PHPExcel_Writer_Exception("Invalid data row passed to CSV writer.");
- }
- }
- }
|