9
9
use PhpOffice \PhpSpreadsheet \Reader \Exception as ReaderException ;
10
10
use PhpOffice \PhpSpreadsheet \Shared \StringHelper ;
11
11
use PhpOffice \PhpSpreadsheet \Spreadsheet ;
12
- use PhpOffice \PhpSpreadsheet \Style \NumberFormat ;
13
12
use PhpOffice \PhpSpreadsheet \Worksheet \Worksheet ;
14
13
15
14
class Csv extends BaseReader
@@ -102,6 +101,14 @@ class Csv extends BaseReader
102
101
/** @var bool */
103
102
private $ sheetNameIsFileName = false ;
104
103
104
+ private string $ getTrue = 'true ' ;
105
+
106
+ private string $ getFalse = 'false ' ;
107
+
108
+ private string $ thousandsSeparator = ', ' ;
109
+
110
+ private string $ decimalSeparator = '. ' ;
111
+
105
112
/**
106
113
* Create a new CSV Reader instance.
107
114
*/
@@ -234,13 +241,14 @@ public function listWorksheetInfo(string $filename): array
234
241
$ worksheetInfo [0 ]['lastColumnIndex ' ] = 0 ;
235
242
$ worksheetInfo [0 ]['totalRows ' ] = 0 ;
236
243
$ worksheetInfo [0 ]['totalColumns ' ] = 0 ;
244
+ $ delimiter = $ this ->delimiter ?? '' ;
237
245
238
246
// Loop through each line of the file in turn
239
- $ rowData = fgetcsv ($ fileHandle , 0 , $ this -> delimiter ?? '' , $ this ->enclosure , $ this ->escapeCharacter );
247
+ $ rowData = fgetcsv ($ fileHandle , 0 , $ delimiter , $ this ->enclosure , $ this ->escapeCharacter );
240
248
while (is_array ($ rowData )) {
241
249
++$ worksheetInfo [0 ]['totalRows ' ];
242
250
$ worksheetInfo [0 ]['lastColumnIndex ' ] = max ($ worksheetInfo [0 ]['lastColumnIndex ' ], count ($ rowData ) - 1 );
243
- $ rowData = fgetcsv ($ fileHandle , 0 , $ this -> delimiter ?? '' , $ this ->enclosure , $ this ->escapeCharacter );
251
+ $ rowData = fgetcsv ($ fileHandle , 0 , $ delimiter , $ this ->enclosure , $ this ->escapeCharacter );
244
252
}
245
253
246
254
$ worksheetInfo [0 ]['lastColumnLetter ' ] = Coordinate::stringFromColumnIndex ($ worksheetInfo [0 ]['lastColumnIndex ' ] + 1 );
@@ -386,15 +394,24 @@ private function loadStringOrFile(string $filename, Spreadsheet $spreadsheet, bo
386
394
$ outRow = 0 ;
387
395
388
396
// Loop through each line of the file in turn
389
- $ rowData = fgetcsv ($ fileHandle , 0 , $ this ->delimiter ?? '' , $ this ->enclosure , $ this ->escapeCharacter );
397
+ $ delimiter = $ this ->delimiter ?? '' ;
398
+ $ rowData = fgetcsv ($ fileHandle , 0 , $ delimiter , $ this ->enclosure , $ this ->escapeCharacter );
390
399
$ valueBinder = Cell::getValueBinder ();
391
400
$ preserveBooleanString = method_exists ($ valueBinder , 'getBooleanConversion ' ) && $ valueBinder ->getBooleanConversion ();
401
+ $ this ->getTrue = Calculation::getTRUE ();
402
+ $ this ->getFalse = Calculation::getFALSE ();
403
+ $ this ->thousandsSeparator = StringHelper::getThousandsSeparator ();
404
+ $ this ->decimalSeparator = StringHelper::getDecimalSeparator ();
392
405
while (is_array ($ rowData )) {
393
406
$ noOutputYet = true ;
394
407
$ columnLetter = 'A ' ;
395
408
foreach ($ rowData as $ rowDatum ) {
396
- $ this ->convertBoolean ($ rowDatum , $ preserveBooleanString );
397
- $ numberFormatMask = $ this ->convertFormattedNumber ($ rowDatum );
409
+ if ($ preserveBooleanString ) {
410
+ $ rowDatum = $ rowDatum ?? '' ;
411
+ } else {
412
+ $ this ->convertBoolean ($ rowDatum );
413
+ }
414
+ $ numberFormatMask = $ this ->castFormattedNumberToNumeric ? $ this ->convertFormattedNumber ($ rowDatum ) : '' ;
398
415
if (($ rowDatum !== '' || $ this ->preserveNullString ) && $ this ->readFilter ->readCell ($ columnLetter , $ currentRow )) {
399
416
if ($ this ->contiguous ) {
400
417
if ($ noOutputYet ) {
@@ -405,15 +422,17 @@ private function loadStringOrFile(string $filename, Spreadsheet $spreadsheet, bo
405
422
$ outRow = $ currentRow ;
406
423
}
407
424
// Set basic styling for the value (Note that this could be overloaded by styling in a value binder)
408
- $ sheet ->getCell ($ columnLetter . $ outRow )->getStyle ()
409
- ->getNumberFormat ()
410
- ->setFormatCode ($ numberFormatMask );
425
+ if ($ numberFormatMask !== '' ) {
426
+ $ sheet ->getStyle ($ columnLetter . $ outRow )
427
+ ->getNumberFormat ()
428
+ ->setFormatCode ($ numberFormatMask );
429
+ }
411
430
// Set cell value
412
431
$ sheet ->getCell ($ columnLetter . $ outRow )->setValue ($ rowDatum );
413
432
}
414
433
++$ columnLetter ;
415
434
}
416
- $ rowData = fgetcsv ($ fileHandle , 0 , $ this -> delimiter ?? '' , $ this ->enclosure , $ this ->escapeCharacter );
435
+ $ rowData = fgetcsv ($ fileHandle , 0 , $ delimiter , $ this ->enclosure , $ this ->escapeCharacter );
417
436
++$ currentRow ;
418
437
}
419
438
@@ -429,12 +448,12 @@ private function loadStringOrFile(string $filename, Spreadsheet $spreadsheet, bo
429
448
/**
430
449
* Convert string true/false to boolean, and null to null-string.
431
450
*/
432
- private function convertBoolean (mixed &$ rowDatum, bool $ preserveBooleanString ): void
451
+ private function convertBoolean (mixed &$ rowDatum ): void
433
452
{
434
- if (is_string ($ rowDatum ) && ! $ preserveBooleanString ) {
435
- if (strcasecmp (Calculation:: getTRUE () , $ rowDatum ) === 0 || strcasecmp ('true ' , $ rowDatum ) === 0 ) {
453
+ if (is_string ($ rowDatum )) {
454
+ if (strcasecmp ($ this -> getTrue , $ rowDatum ) === 0 || strcasecmp ('true ' , $ rowDatum ) === 0 ) {
436
455
$ rowDatum = true ;
437
- } elseif (strcasecmp (Calculation:: getFALSE () , $ rowDatum ) === 0 || strcasecmp ('false ' , $ rowDatum ) === 0 ) {
456
+ } elseif (strcasecmp ($ this -> getFalse , $ rowDatum ) === 0 || strcasecmp ('false ' , $ rowDatum ) === 0 ) {
438
457
$ rowDatum = false ;
439
458
}
440
459
} else {
@@ -447,18 +466,18 @@ private function convertBoolean(mixed &$rowDatum, bool $preserveBooleanString):
447
466
*/
448
467
private function convertFormattedNumber (mixed &$ rowDatum ): string
449
468
{
450
- $ numberFormatMask = NumberFormat:: FORMAT_GENERAL ;
469
+ $ numberFormatMask = '' ;
451
470
if ($ this ->castFormattedNumberToNumeric === true && is_string ($ rowDatum )) {
452
471
$ numeric = str_replace (
453
- [StringHelper:: getThousandsSeparator (), StringHelper:: getDecimalSeparator () ],
472
+ [$ this -> thousandsSeparator , $ this -> decimalSeparator ],
454
473
['' , '. ' ],
455
474
$ rowDatum
456
475
);
457
476
458
477
if (is_numeric ($ numeric )) {
459
- $ decimalPos = strpos ($ rowDatum , StringHelper:: getDecimalSeparator () );
478
+ $ decimalPos = strpos ($ rowDatum , $ this -> decimalSeparator );
460
479
if ($ this ->preserveNumericFormatting === true ) {
461
- $ numberFormatMask = (str_contains ($ rowDatum , StringHelper:: getThousandsSeparator () ))
480
+ $ numberFormatMask = (str_contains ($ rowDatum , $ this -> thousandsSeparator ))
462
481
? '#,##0 ' : '0 ' ;
463
482
if ($ decimalPos !== false ) {
464
483
$ decimals = strlen ($ rowDatum ) - $ decimalPos - 1 ;
0 commit comments