今天在做导入EXCEL数据时,而且单元格里的数据类型改成文本类型后,在PHPEXCEL读出来的是PHPExcel_RichText类型的,这类型使getValue()是不管用了,因为这时候getValue()返回的PHPExcel_RichText(下面是PHPExcel_RichText数据保存格式)是一个Object类型了,所以在插入数据的时候就为空。
- PHPExcel_RichText Object
- (
- [_richTextElements:PHPExcel_RichText:private] => Array
- (
- [0] => PHPExcel_RichText_TextElement Object
- (
- [_text:PHPExcel_RichText_TextElement:private] => l
- )
- [1] => PHPExcel_RichText_Run Object
- (
- [_font:PHPExcel_RichText_Run:private] => PHPExcel_Style_Font Object
- (
- [_name:protected] => Calibri
- [_size:protected] => 11
- [_bold:protected] =>
- [_italic:protected] =>
- [_superScript:protected] =>
- [_subScript:protected] =>
- [_underline:protected] => none
- [_strikethrough:protected] =>
- [_color:protected] => PHPExcel_Style_Color Object
- (
- [_argb:protected] => FF000000
- [_parentPropertyName:protected] =>
- [_isSupervisor:protected] =>
- [_parent:protected] =>
- )
- [_isSupervisor:protected] =>
- [_parent:protected] =>
- [colorIndex] => 8
- )
- [_text:PHPExcel_RichText_TextElement:private] => isimin
- )
- )
- )
解决方法如下:
- $cell = $currentSheet->getCell($addr)->getValue();//获取对应值
- if($cell instanceof PHPExcel_RichText){
- //富文本转换字符串 这里非常的重要
- $cell = $cell->__toString();
- }