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,34 @@ 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, $ 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 ($ datatype == DataType::TYPE_STRING || $ datatype == DataType::TYPE_STRING2 ) {
300
+ if (substr ($ cellContent , 0 , 1 ) === '= ' || is_numeric ($ cellContent )) {
301
+ $ sheet ->getCell ($ column . $ row )
302
+ ->getStyle ()
303
+ ->setQuotePrefix (true );
304
+ }
305
+ }
306
+ //catching the Exception and ignoring the invalid data types
307
+ try {
308
+ $ sheet ->setCellValueExplicit ($ column . $ row , $ cellContent , $ attributeArray ['data-type ' ]);
309
+ } catch (\PhpOffice \PhpSpreadsheet \Exception $ exception ) {
310
+ $ sheet ->setCellValue ($ column . $ row , $ cellContent );
311
+ }
312
+ } else {
313
+ $ sheet ->setCellValue ($ column . $ row , $ cellContent );
314
+ }
295
315
$ this ->dataArray [$ row ][$ column ] = $ cellContent ;
296
316
}
297
317
} else {
@@ -355,7 +375,7 @@ private function processDomElementSpanEtc(Worksheet $sheet, int &$row, string &$
355
375
private function processDomElementHr (Worksheet $ sheet , int &$ row , string &$ column , string &$ cellContent , DOMElement $ child , array &$ attributeArray ): void
356
376
{
357
377
if ($ child ->nodeName === 'hr ' ) {
358
- $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent );
378
+ $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent, $ attributeArray );
359
379
++$ row ;
360
380
if (isset ($ this ->formats [$ child ->nodeName ])) {
361
381
$ sheet ->getStyle ($ column . $ row )->applyFromArray ($ this ->formats [$ child ->nodeName ]);
@@ -375,7 +395,7 @@ private function processDomElementBr(Worksheet $sheet, int &$row, string &$colum
375
395
$ sheet ->getStyle ($ column . $ row )->getAlignment ()->setWrapText (true );
376
396
} else {
377
397
// Otherwise flush our existing content and move the row cursor on
378
- $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent );
398
+ $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent, $ attributeArray );
379
399
++$ row ;
380
400
}
381
401
} else {
@@ -421,11 +441,11 @@ private function processDomElementH1Etc(Worksheet $sheet, int &$row, string &$co
421
441
$ this ->processDomElement ($ child , $ sheet , $ row , $ column , $ cellContent );
422
442
} else {
423
443
if ($ cellContent > '' ) {
424
- $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent );
444
+ $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent, $ attributeArray );
425
445
++$ row ;
426
446
}
427
447
$ this ->processDomElement ($ child , $ sheet , $ row , $ column , $ cellContent );
428
- $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent );
448
+ $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent, $ attributeArray );
429
449
430
450
if (isset ($ this ->formats [$ child ->nodeName ])) {
431
451
$ sheet ->getStyle ($ column . $ row )->applyFromArray ($ this ->formats [$ child ->nodeName ]);
@@ -448,11 +468,11 @@ private function processDomElementLi(Worksheet $sheet, int &$row, string &$colum
448
468
$ this ->processDomElement ($ child , $ sheet , $ row , $ column , $ cellContent );
449
469
} else {
450
470
if ($ cellContent > '' ) {
451
- $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent );
471
+ $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent, $ attributeArray );
452
472
}
453
473
++$ row ;
454
474
$ this ->processDomElement ($ child , $ sheet , $ row , $ column , $ cellContent );
455
- $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent );
475
+ $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent, $ attributeArray );
456
476
$ column = 'A ' ;
457
477
}
458
478
} else {
@@ -472,7 +492,7 @@ private function processDomElementImg(Worksheet $sheet, int &$row, string &$colu
472
492
private function processDomElementTable (Worksheet $ sheet , int &$ row , string &$ column , string &$ cellContent , DOMElement $ child , array &$ attributeArray ): void
473
493
{
474
494
if ($ child ->nodeName === 'table ' ) {
475
- $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent );
495
+ $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent, $ attributeArray );
476
496
$ column = $ this ->setTableStartColumn ($ column );
477
497
if ($ this ->tableLevel > 1 && $ row > 1 ) {
478
498
--$ row ;
@@ -564,18 +584,6 @@ private function processDomElementDataFormat(Worksheet $sheet, int $row, string
564
584
}
565
585
}
566
586
567
- private function processDomElementDataType (Worksheet $ sheet , int $ row , string $ column , array $ attributeArray ): void
568
- {
569
- if (isset ($ attributeArray ['data-type ' ])) {
570
- $ cell = $ sheet ->getCell ($ column . $ row );
571
- $ cellValue = $ cell ->getValue ();
572
- //cast value to the datatype
573
- $ sheet ->setCellValueExplicit ($ column . $ row , $ cellValue , $ attributeArray ['data-type ' ]);
574
- //set datatype
575
- $ cell ->setDataType ($ attributeArray ['data-type ' ]);
576
- }
577
- }
578
-
579
587
private function processDomElementThTd (Worksheet $ sheet , int &$ row , string &$ column , string &$ cellContent , DOMElement $ child , array &$ attributeArray ): void
580
588
{
581
589
while (isset ($ this ->rowspan [$ column . $ row ])) {
@@ -586,15 +594,14 @@ private function processDomElementThTd(Worksheet $sheet, int &$row, string &$col
586
594
// apply inline style
587
595
$ this ->applyInlineStyle ($ sheet , $ row , $ column , $ attributeArray );
588
596
589
- $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent );
597
+ $ this ->flushCell ($ sheet , $ column , $ row , $ cellContent, $ attributeArray );
590
598
591
599
$ this ->processDomElementBgcolor ($ sheet , $ row , $ column , $ attributeArray );
592
600
$ this ->processDomElementWidth ($ sheet , $ column , $ attributeArray );
593
601
$ this ->processDomElementHeight ($ sheet , $ row , $ attributeArray );
594
602
$ this ->processDomElementAlign ($ sheet , $ row , $ column , $ attributeArray );
595
603
$ this ->processDomElementVAlign ($ sheet , $ row , $ column , $ attributeArray );
596
604
$ this ->processDomElementDataFormat ($ sheet , $ row , $ column , $ attributeArray );
597
- $ this ->processDomElementDataType ($ sheet , $ row , $ column , $ attributeArray );
598
605
599
606
if (isset ($ attributeArray ['rowspan ' ], $ attributeArray ['colspan ' ])) {
600
607
//create merging rowspan and colspan
0 commit comments