7
7
use DOMNode ;
8
8
use DOMText ;
9
9
use PhpOffice \PhpSpreadsheet \Cell \Coordinate ;
10
+ use PhpOffice \PhpSpreadsheet \Cell \DataType ;
10
11
use PhpOffice \PhpSpreadsheet \Helper \Dimension as CssDimension ;
11
12
use PhpOffice \PhpSpreadsheet \Reader \Security \XmlScanner ;
12
13
use PhpOffice \PhpSpreadsheet \Spreadsheet ;
@@ -283,15 +284,35 @@ protected function releaseTableStartColumn(): string
283
284
* @param int|string $row
284
285
* @param mixed $cellContent
285
286
*/
286
- protected function flushCell (Worksheet $ sheet , $ column , $ row , &$ cellContent ): void
287
+ protected function flushCell (Worksheet $ sheet , $ column , $ row , &$ cellContent, array $ attributeArray ): void
287
288
{
288
289
if (is_string ($ cellContent )) {
289
290
// Simple String content
290
291
if (trim ($ cellContent ) > '' ) {
291
292
// Only actually write it if there's content in the string
292
293
// Write to worksheet to be done here...
293
- // ... we return the cell so we can mess about with styles more easily
294
- $ sheet ->setCellValue ($ column . $ row , $ cellContent );
294
+ // ... we return the cell, so we can mess about with styles more easily
295
+
296
+ // Set cell value explicitly if there is data-type attribute
297
+ if (isset ($ attributeArray ['data-type ' ])) {
298
+ $ datatype = $ attributeArray ['data-type ' ];
299
+ if (in_array ($ datatype , [DataType::TYPE_STRING , DataType::TYPE_STRING2 , DataType::TYPE_INLINE ])) {
300
+ //Prevent to Excel treat string with beginning equal sign or convert big numbers to scientific number
301
+ if (substr ($ cellContent , 0 , 1 ) === '= ' ) {
302
+ $ sheet ->getCell ($ column . $ row )
303
+ ->getStyle ()
304
+ ->setQuotePrefix (true );
305
+ }
306
+ }
307
+ //catching the Exception and ignoring the invalid data types
308
+ try {
309
+ $ sheet ->setCellValueExplicit ($ column . $ row , $ cellContent , $ attributeArray ['data-type ' ]);
310
+ } catch (\PhpOffice \PhpSpreadsheet \Exception $ exception ) {
311
+ $ sheet ->setCellValue ($ column . $ row , $ cellContent );
312
+ }
313
+ } else {
314
+ $ sheet ->setCellValue ($ column . $ row , $ cellContent );
315
+ }
295
316
$ this ->dataArray [$ row ][$ column ] = $ cellContent ;
296
317
}
297
318
} else {
@@ -355,7 +376,7 @@ private function processDomElementSpanEtc(Worksheet $sheet, int &$row, string &$
355
376
private function processDomElementHr (Worksheet $ sheet , int &$ row , string &$ column , string &$ cellContent , DOMElement $ child , array &$ attributeArray ): void
356
377
{
357
378
if ($ child ->nodeName === 'hr ' ) {
358
- $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent );
379
+ $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent, $ attributeArray );
359
380
++$ row ;
360
381
if (isset ($ this ->formats [$ child ->nodeName ])) {
361
382
$ sheet ->getStyle ($ column . $ row )->applyFromArray ($ this ->formats [$ child ->nodeName ]);
@@ -375,7 +396,7 @@ private function processDomElementBr(Worksheet $sheet, int &$row, string &$colum
375
396
$ sheet ->getStyle ($ column . $ row )->getAlignment ()->setWrapText (true );
376
397
} else {
377
398
// Otherwise flush our existing content and move the row cursor on
378
- $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent );
399
+ $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent, $ attributeArray );
379
400
++$ row ;
380
401
}
381
402
} else {
@@ -421,11 +442,11 @@ private function processDomElementH1Etc(Worksheet $sheet, int &$row, string &$co
421
442
$ this ->processDomElement ($ child , $ sheet , $ row , $ column , $ cellContent );
422
443
} else {
423
444
if ($ cellContent > '' ) {
424
- $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent );
445
+ $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent, $ attributeArray );
425
446
++$ row ;
426
447
}
427
448
$ this ->processDomElement ($ child , $ sheet , $ row , $ column , $ cellContent );
428
- $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent );
449
+ $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent, $ attributeArray );
429
450
430
451
if (isset ($ this ->formats [$ child ->nodeName ])) {
431
452
$ sheet ->getStyle ($ column . $ row )->applyFromArray ($ this ->formats [$ child ->nodeName ]);
@@ -448,11 +469,11 @@ private function processDomElementLi(Worksheet $sheet, int &$row, string &$colum
448
469
$ this ->processDomElement ($ child , $ sheet , $ row , $ column , $ cellContent );
449
470
} else {
450
471
if ($ cellContent > '' ) {
451
- $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent );
472
+ $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent, $ attributeArray );
452
473
}
453
474
++$ row ;
454
475
$ this ->processDomElement ($ child , $ sheet , $ row , $ column , $ cellContent );
455
- $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent );
476
+ $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent, $ attributeArray );
456
477
$ column = 'A ' ;
457
478
}
458
479
} else {
@@ -472,7 +493,7 @@ private function processDomElementImg(Worksheet $sheet, int &$row, string &$colu
472
493
private function processDomElementTable (Worksheet $ sheet , int &$ row , string &$ column , string &$ cellContent , DOMElement $ child , array &$ attributeArray ): void
473
494
{
474
495
if ($ child ->nodeName === 'table ' ) {
475
- $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent );
496
+ $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent, $ attributeArray );
476
497
$ column = $ this ->setTableStartColumn ($ column );
477
498
if ($ this ->tableLevel > 1 && $ row > 1 ) {
478
499
--$ row ;
@@ -574,7 +595,7 @@ private function processDomElementThTd(Worksheet $sheet, int &$row, string &$col
574
595
// apply inline style
575
596
$ this ->applyInlineStyle ($ sheet , $ row , $ column , $ attributeArray );
576
597
577
- $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent );
598
+ $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent, $ attributeArray );
578
599
579
600
$ this ->processDomElementBgcolor ($ sheet , $ row , $ column , $ attributeArray );
580
601
$ this ->processDomElementWidth ($ sheet , $ column , $ attributeArray );
0 commit comments