PHPEXCEL读出数据是PHPExcel_RichText类型

今天在做导入EXCEL数据时,而且单元格里的数据类型改成文本类型后,在PHPEXCEL读出来的是PHPExcel_RichText类型的,这类型使getValue()是不管用了,因为这时候getValue()返回的PHPExcel_RichText(下面是PHPExcel_RichText数据保存格式)是一个Object类型了,所以在插入数据的时候就为空。

  1.  PHPExcel_RichText Object 
  2.     [_richTextElements:PHPExcel_RichText:private] => Array 
  3.         ( 
  4.             [0] => PHPExcel_RichText_TextElement Object 
  5.                 ( 
  6.                     [_text:PHPExcel_RichText_TextElement:private] => l 
  7.                 ) 
  8.  
  9.             [1] => PHPExcel_RichText_Run Object 
  10.                 ( 
  11.                     [_font:PHPExcel_RichText_Run:private] => PHPExcel_Style_Font Object 
  12.                         ( 
  13.                             [_name:protected] => Calibri 
  14.                             [_size:protected] => 11 
  15.                             [_bold:protected] =>  
  16.                             [_italic:protected] =>  
  17.                             [_superScript:protected] =>  
  18.                             [_subScript:protected] =>  
  19.                             [_underline:protected] => none 
  20.                             [_strikethrough:protected] =>  
  21.                             [_color:protected] => PHPExcel_Style_Color Object 
  22.                                 ( 
  23.                                     [_argb:protected] => FF000000 
  24.                                     [_parentPropertyName:protected] =>  
  25.                                     [_isSupervisor:protected] =>  
  26.                                     [_parent:protected] =>  
  27.                                 ) 
  28.  
  29.                             [_isSupervisor:protected] =>  
  30.                             [_parent:protected] =>  
  31.                             [colorIndex] => 8 
  32.                         ) 
  33.  
  34.                     [_text:PHPExcel_RichText_TextElement:private] => isimin 
  35.                 ) 
  36.  
  37.         ) 
  38.  


解决方法如下:
 

    1. $cell = $currentSheet->getCell($addr)->getValue();//获取对应值  
    2.   if($cell instanceof PHPExcel_RichText){ 
    3.         //富文本转换字符串 这里非常的重要
    4.           $cell = $cell->__toString();  
    5. }
    6.    
posted @ 2019-03-12 14:40  HigherLee  阅读(911)  评论(0编辑  收藏  举报