91 parent::__construct();
92 $callback = self::$constructorCallback;
93 if ($callback !== null) {
106 self::$constructorCallback = $callback;
111 return self::$constructorCallback;
116 $this->inputEncoding = $pValue;
128 $this->fallbackEncoding = $pValue;
143 rewind($this->fileHandle);
145 if (fgets($this->fileHandle, self::UTF8_BOM_LEN + 1) !== self::UTF8_BOM) {
146 rewind($this->fileHandle);
155 $line = fgets($this->fileHandle);
156 if ($line ===
false) {
160 if ((strlen(trim($line,
"\r\n")) == 5) && (stripos($line,
'sep=') === 0)) {
161 $this->delimiter = substr($line, 4, 1);
174 if ($this->delimiter !== null) {
178 $inferenceEngine =
new Delimiter($this->fileHandle, $this->escapeCharacter, $this->enclosure);
181 if ($inferenceEngine->linesCounted() === 0) {
182 $this->delimiter = $inferenceEngine->getDefaultDelimiter();
188 $this->delimiter = $inferenceEngine->infer();
191 if ($this->delimiter === null) {
192 $this->delimiter = $inferenceEngine->getDefaultDelimiter();
213 $worksheetInfo[0][
'worksheetName'] =
'Worksheet';
214 $worksheetInfo[0][
'lastColumnLetter'] =
'A';
215 $worksheetInfo[0][
'lastColumnIndex'] = 0;
216 $worksheetInfo[0][
'totalRows'] = 0;
217 $worksheetInfo[0][
'totalColumns'] = 0;
220 $rowData = fgetcsv(
$fileHandle, 0, $this->delimiter ??
'', $this->enclosure, $this->escapeCharacter);
221 while (is_array($rowData)) {
222 ++$worksheetInfo[0][
'totalRows'];
223 $worksheetInfo[0][
'lastColumnIndex'] = max($worksheetInfo[0][
'lastColumnIndex'], count($rowData) - 1);
224 $rowData = fgetcsv(
$fileHandle, 0, $this->delimiter ??
'', $this->enclosure, $this->escapeCharacter);
228 $worksheetInfo[0][
'totalColumns'] = $worksheetInfo[0][
'lastColumnIndex'] + 1;
233 return $worksheetInfo;
243 public function load($pFilename)
255 $fhandle = $this->
canRead($pFilename);
257 throw new Exception($pFilename .
' is an Invalid Spreadsheet file.');
259 if ($this->inputEncoding === self::GUESS_ENCODING) {
260 $this->inputEncoding = self::guessEncoding($pFilename, $this->fallbackEncoding);
263 if ($this->inputEncoding !==
'UTF-8') {
264 fclose($this->fileHandle);
265 $entireFile = file_get_contents($pFilename);
266 $this->fileHandle =
fopen(
'php://memory',
'r+b');
267 if ($this->fileHandle !==
false && $entireFile !==
false) {
269 fwrite($this->fileHandle,
$data);
280 $lineEnding = ini_get(
'auto_detect_line_endings') ?:
'0';
281 ini_set(
'auto_detect_line_endings',
'1');
303 $rowData = fgetcsv(
$fileHandle, 0, $this->delimiter ??
'', $this->enclosure, $this->escapeCharacter);
304 while (is_array($rowData)) {
307 foreach ($rowData as $rowDatum) {
308 if ($rowDatum !=
'' && $this->readFilter->readCell($columnLetter, $currentRow)) {
309 if ($this->contiguous) {
311 $noOutputYet =
false;
315 $outRow = $currentRow;
318 $sheet->getCell($columnLetter . $outRow)->setValue($rowDatum);
322 $rowData = fgetcsv(
$fileHandle, 0, $this->delimiter ??
'', $this->enclosure, $this->escapeCharacter);
329 ini_set(
'auto_detect_line_endings', $lineEnding);
354 if ($enclosure ==
'') {
369 $this->sheetIndex = $pValue;
376 $this->contiguous = (bool) $contiguous;
408 return is_string($extension) ? strtolower($extension) :
'';
427 fclose($this->fileHandle);
430 $extension = self::extractStringLower(pathinfo($pFilename, PATHINFO_EXTENSION));
431 if (in_array($extension, [
'csv',
'tsv'])) {
436 $type = mime_content_type($pFilename);
444 return in_array(
$type, $supportedTypes,
true);
447 private static function guessEncodingTestNoBom(
string &$encoding,
string &$contents,
string $compare,
string $setEncoding): void
449 if ($encoding ===
'') {
450 $pos = strpos($contents, $compare);
451 if ($pos !==
false && $pos % strlen($compare) === 0) {
452 $encoding = $setEncoding;
460 $contents = file_get_contents($filename);
461 self::guessEncodingTestNoBom($encoding, $contents, self::UTF32BE_LF,
'UTF-32BE');
462 self::guessEncodingTestNoBom($encoding, $contents, self::UTF32LE_LF,
'UTF-32LE');
463 self::guessEncodingTestNoBom($encoding, $contents, self::UTF16BE_LF,
'UTF-16BE');
464 self::guessEncodingTestNoBom($encoding, $contents, self::UTF16LE_LF,
'UTF-16LE');
465 if ($encoding ===
'' && preg_match(
'//u', $contents) === 1) {
472 private static function guessEncodingTestBom(
string &$encoding,
string $first4,
string $compare,
string $setEncoding): void
474 if ($encoding ===
'') {
475 if ($compare === substr($first4, 0, strlen($compare))) {
476 $encoding = $setEncoding;
484 $first4 = file_get_contents($filename,
false, null, 0, 4);
485 if ($first4 !==
false) {
486 self::guessEncodingTestBom($encoding, $first4, self::UTF8_BOM,
'UTF-8');
487 self::guessEncodingTestBom($encoding, $first4, self::UTF16BE_BOM,
'UTF-16BE');
488 self::guessEncodingTestBom($encoding, $first4, self::UTF32BE_BOM,
'UTF-32BE');
489 self::guessEncodingTestBom($encoding, $first4, self::UTF32LE_BOM,
'UTF-32LE');
490 self::guessEncodingTestBom($encoding, $first4, self::UTF16LE_BOM,
'UTF-16LE');
498 $encoding = self::guessEncodingBom($filename);
499 if ($encoding ===
'') {
500 $encoding = self::guessEncodingNoBom($filename);
503 return ($encoding ===
'') ? $dflt : $encoding;
static extractStringLower($extension)
Scrutinizer believes, incorrectly, that the specific pathinfo call in canRead can return something ot...
setSheetIndex(int $pValue)
static guessEncodingBom(string $filename)
__construct()
Create a new CSV Reader instance.
inferSeparator()
Infer the separator if it isn't explicitly set in the file or specified by the user.
setDelimiter(string $delimiter)
const DEFAULT_FALLBACK_ENCODING
static setConstructorCallback(?callable $callback)
Set a callback to change the defaults.
static getConstructorCallback()
createSheet($sheetIndex=null)
Create sheet and add it to this workbook.
checkSeparator()
Identify any separator that is explicitly set in the file.
setContiguous(bool $contiguous)
setActiveSheetIndex($pIndex)
Set active sheet index.
load($pFilename)
Loads Spreadsheet from file.
setEscapeCharacter(string $escapeCharacter)
static guessEncoding(string $filename, string $dflt=self::DEFAULT_FALLBACK_ENCODING)
static guessEncodingTestBom(string &$encoding, string $first4, string $compare, string $setEncoding)
static convertEncoding($value, $to, $from)
Convert string from one encoding to another.
static $constructorCallback
setInputEncoding(string $pValue)
static guessEncodingNoBom(string $filename)
canRead($pFilename)
Can the current IReader read the file?
setEnclosure(string $enclosure)
loadIntoExisting(string $pFilename, Spreadsheet $spreadsheet)
Loads PhpSpreadsheet from file into PhpSpreadsheet instance.
listWorksheetInfo(string $pFilename)
Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)...
static guessEncodingTestNoBom(string &$encoding, string &$contents, string $compare, string $setEncoding)
static stringFromColumnIndex($columnIndex)
String from column index.
openFileOrMemory(string $pFilename)
openFile($pFilename)
Open file for reading.
setFallbackEncoding(string $pValue)
skipBOM()
Move filepointer past any BOM marker.
getSheetCount()
Get sheet count.