123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- <?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 $includeSeparatorLine = 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) {
- $this->setUseBOM(true);
- $this->setIncludeSeparatorLine(true);
- $this->setEnclosure('"');
- $this->setDelimiter(";");
- $this->setLineEnding("\r\n");
- }
- if ($this->useBOM) {
-
- fwrite($fileHandle, "\xEF\xBB\xBF");
- }
- if ($this->includeSeparatorLine) {
-
- fwrite($fileHandle, 'sep=' . $this->getDelimiter() . $this->lineEnding);
- }
-
- $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 getIncludeSeparatorLine()
- {
- return $this->includeSeparatorLine;
- }
-
- public function setIncludeSeparatorLine($pValue = false)
- {
- $this->includeSeparatorLine = $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.");
- }
- }
- }
|