php做EXCEL数据导出导入开发的一些小问题

前两天刚刚做开发CRM系统项目,在做要做EXCEL导出导入功能,因为以前做.NET开发用的是NPOI,但可是没找到PHP版本的,所以就网搜找了个国外的开源PHPEXCEL ,

一开始只是做了简单的导入导出,还没有出现做什么问题,一切顺利(因为那是EXCEL的单元格都没有设置什么数据类型的情况下),

在做导入读取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
                )

        )

)

用var_dump()输出一看蒙了,不过也没关系,既然是PHPExcel_RichText类型的数据,经过查文档,发现该对象有getPlainText()方法获取单元格的值,这回笑了^_^

在这里要做个判断

if(getValue() instanceof \PHPExcel_RichText) 

{

  //处理PHPExcel_RichText读取

}else{

  //直接读取getValue()

}

oK,注意有些网上用new PHPExcel_RichText($cell) 转换的,会出现 Call to undefined method PHPExcel_CachedObjectStorage_Memory::getStyle()这个错误的

这个是PHPExcel的一个BUG或是遗漏吧,这错误解决方案是:

在CacheBase.php中的PHPExcel_CachedObjectStorage_Memory加一个getStyle()方法,

public function getStyle($pCellCooldinate='A1')
{
  return $this->_parent->getStyle($pCellCooldinate) ;
}

posted @ 2015-11-11 13:29  草木本心  阅读(5285)  评论(1编辑  收藏  举报