ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
CSV.php
Go to the documentation of this file.
1<?php
42 private $_phpExcel;
43
49 private $_delimiter = ',';
50
56 private $_enclosure = '"';
57
64
70 private $_sheetIndex = 0;
71
77 private $_useBOM = false;
78
84 private $_excelCompatibility = false;
85
91 public function __construct(PHPExcel $phpExcel) {
92 $this->_phpExcel = $phpExcel;
93 }
94
101 public function save($pFilename = null) {
102 // Fetch sheet
103 $sheet = $this->_phpExcel->getSheet($this->_sheetIndex);
104
105 $saveDebugLog = PHPExcel_Calculation::getInstance($this->_phpExcel)->getDebugLog()->getWriteDebugLog();
106 PHPExcel_Calculation::getInstance($this->_phpExcel)->getDebugLog()->setWriteDebugLog(FALSE);
107 $saveArrayReturnType = PHPExcel_Calculation::getArrayReturnType();
109
110 // Open file
111 $fileHandle = fopen($pFilename, 'wb+');
112 if ($fileHandle === false) {
113 throw new PHPExcel_Writer_Exception("Could not open file $pFilename for writing.");
114 }
115
116 if ($this->_excelCompatibility) {
117 fwrite($fileHandle, "\xEF\xBB\xBF"); // Enforce UTF-8 BOM Header
118 $this->setEnclosure('"'); // Set enclosure to "
119 $this->setDelimiter(";"); // Set delimiter to a semi-colon
120 $this->setLineEnding("\r\n");
121 fwrite($fileHandle, 'sep=' . $this->getDelimiter() . $this->_lineEnding);
122 } elseif ($this->_useBOM) {
123 // Write the UTF-8 BOM code if required
124 fwrite($fileHandle, "\xEF\xBB\xBF");
125 }
126
127 // Identify the range that we need to extract from the worksheet
128 $maxCol = $sheet->getHighestDataColumn();
129 $maxRow = $sheet->getHighestDataRow();
130
131 // Write rows to file
132 for($row = 1; $row <= $maxRow; ++$row) {
133 // Convert the row to an array...
134 $cellsArray = $sheet->rangeToArray('A'.$row.':'.$maxCol.$row,'', $this->_preCalculateFormulas);
135 // ... and write to the file
136 $this->_writeLine($fileHandle, $cellsArray[0]);
137 }
138
139 // Close file
140 fclose($fileHandle);
141
142 PHPExcel_Calculation::setArrayReturnType($saveArrayReturnType);
143 PHPExcel_Calculation::getInstance($this->_phpExcel)->getDebugLog()->setWriteDebugLog($saveDebugLog);
144 }
145
151 public function getDelimiter() {
152 return $this->_delimiter;
153 }
154
161 public function setDelimiter($pValue = ',') {
162 $this->_delimiter = $pValue;
163 return $this;
164 }
165
171 public function getEnclosure() {
172 return $this->_enclosure;
173 }
174
181 public function setEnclosure($pValue = '"') {
182 if ($pValue == '') {
183 $pValue = null;
184 }
185 $this->_enclosure = $pValue;
186 return $this;
187 }
188
194 public function getLineEnding() {
195 return $this->_lineEnding;
196 }
197
204 public function setLineEnding($pValue = PHP_EOL) {
205 $this->_lineEnding = $pValue;
206 return $this;
207 }
208
214 public function getUseBOM() {
215 return $this->_useBOM;
216 }
217
224 public function setUseBOM($pValue = false) {
225 $this->_useBOM = $pValue;
226 return $this;
227 }
228
234 public function getExcelCompatibility() {
236 }
237
245 public function setExcelCompatibility($pValue = false) {
246 $this->_excelCompatibility = $pValue;
247 return $this;
248 }
249
255 public function getSheetIndex() {
256 return $this->_sheetIndex;
257 }
258
265 public function setSheetIndex($pValue = 0) {
266 $this->_sheetIndex = $pValue;
267 return $this;
268 }
269
277 private function _writeLine($pFileHandle = null, $pValues = null) {
278 if (is_array($pValues)) {
279 // No leading delimiter
280 $writeDelimiter = false;
281
282 // Build the line
283 $line = '';
284
285 foreach ($pValues as $element) {
286 // Escape enclosures
287 $element = str_replace($this->_enclosure, $this->_enclosure . $this->_enclosure, $element);
288
289 // Add delimiter
290 if ($writeDelimiter) {
291 $line .= $this->_delimiter;
292 } else {
293 $writeDelimiter = true;
294 }
295
296 // Add enclosed string
297 $line .= $this->_enclosure . $element . $this->_enclosure;
298 }
299
300 // Add line ending
301 $line .= $this->_lineEnding;
302
303 // Write to file
304 fwrite($pFileHandle, $line);
305 } else {
306 throw new PHPExcel_Writer_Exception("Invalid data row passed to CSV writer.");
307 }
308 }
309
310}
An exception for terminatinating execution or to throw for unit testing.
static getInstance(PHPExcel $workbook=NULL)
Get an instance of this class.
static getArrayReturnType()
Return the Array Return Type (Array or Value of first element in the array)
static setArrayReturnType($returnType)
Set the Array Return Type (Array or Value of first element in the array)
setSheetIndex($pValue=0)
Set sheet index.
Definition: CSV.php:265
setUseBOM($pValue=false)
Set whether BOM should be used.
Definition: CSV.php:224
save($pFilename=null)
Save PHPExcel to file.
Definition: CSV.php:101
getSheetIndex()
Get sheet index.
Definition: CSV.php:255
setExcelCompatibility($pValue=false)
Set whether the file should be saved with full Excel Compatibility.
Definition: CSV.php:245
_writeLine($pFileHandle=null, $pValues=null)
Write line to CSV file.
Definition: CSV.php:277
setDelimiter($pValue=',')
Set delimiter.
Definition: CSV.php:161
getEnclosure()
Get enclosure.
Definition: CSV.php:171
setLineEnding($pValue=PHP_EOL)
Set line ending.
Definition: CSV.php:204
__construct(PHPExcel $phpExcel)
Create a new PHPExcel_Writer_CSV.
Definition: CSV.php:91
getUseBOM()
Get whether BOM should be used.
Definition: CSV.php:214
getExcelCompatibility()
Get whether the file should be saved with full Excel Compatibility.
Definition: CSV.php:234
setEnclosure($pValue='"')
Set enclosure.
Definition: CSV.php:181
getLineEnding()
Get line ending.
Definition: CSV.php:194
getDelimiter()
Get delimiter.
Definition: CSV.php:151