5use InvalidArgumentException;
91 parent::__construct();
93 if ($callback !==
null) {
106 self::$constructorCallback = $callback;
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) {
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);
369 $this->sheetIndex = $pValue;
408 return is_string($extension) ? strtolower($extension) :
'';
423 }
catch (InvalidArgumentException $e) {
427 fclose($this->fileHandle);
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);
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) {
499 if ($encoding ===
'') {
503 return ($encoding ===
'') ? $dflt : $encoding;
An exception for terminatinating execution or to throw for unit testing.
Helper class to manipulate cell coordinates.
static stringFromColumnIndex($columnIndex)
String from column index.
openFile($pFilename)
Open file for reading.
load($pFilename)
Loads Spreadsheet from file.
setEscapeCharacter(string $escapeCharacter)
checkSeparator()
Identify any separator that is explicitly set in the file.
setInputEncoding(string $pValue)
listWorksheetInfo(string $pFilename)
Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
static extractStringLower($extension)
Scrutinizer believes, incorrectly, that the specific pathinfo call in canRead can return something ot...
static guessEncoding(string $filename, string $dflt=self::DEFAULT_FALLBACK_ENCODING)
loadIntoExisting(string $pFilename, Spreadsheet $spreadsheet)
Loads PhpSpreadsheet from file into PhpSpreadsheet instance.
setFallbackEncoding(string $pValue)
static guessEncodingTestBom(string &$encoding, string $first4, string $compare, string $setEncoding)
static $constructorCallback
inferSeparator()
Infer the separator if it isn't explicitly set in the file or specified by the user.
openFileOrMemory(string $pFilename)
setDelimiter(string $delimiter)
static getConstructorCallback()
static guessEncodingBom(string $filename)
static guessEncodingNoBom(string $filename)
skipBOM()
Move filepointer past any BOM marker.
setEnclosure(string $enclosure)
const DEFAULT_FALLBACK_ENCODING
static setConstructorCallback(?callable $callback)
Set a callback to change the defaults.
__construct()
Create a new CSV Reader instance.
static guessEncodingTestNoBom(string &$encoding, string &$contents, string $compare, string $setEncoding)
setSheetIndex(int $pValue)
setContiguous(bool $contiguous)
canRead($pFilename)
Can the current IReader read the file?
static convertEncoding($value, $to, $from)
Convert string from one encoding to another.
createSheet($sheetIndex=null)
Create sheet and add it to this workbook.
setActiveSheetIndex($pIndex)
Set active sheet index.
getSheetCount()
Get sheet count.