私人领地

phpexcel 字符串转码

问题状况:在导入excel的时候会出现

PHPExcel_RichText Object ( [_richTextElements:PHPExcel_RichText:private] => Array ( [0] => PHPExcel_RichText_Run Object ( [_font:PHPExcel_RichText_Run:private] => PHPExcel_Style_Font Object ( [_name:protected] => 細明體 [_size:protected] => 12 [_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] => 蜜糖皇后暖色胭脂 ) [1] => PHPExcel_RichText_Run Object ( [_font:PHPExcel_RichText_Run:private] => PHPExcel_Style_Font Object ( [_name:protected] => Calibri [_size:protected] => 12 [_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] => The Honey Queen Honeycomb Blusher ) ) )

解决办法

import("Org.Util.PHPExcel");   // 这里不能漏掉
 import("Org.Util.PHPExcel.IOFactory");
$objReader = \PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
 /******   上面的代码可以不用看,下面的才是处理的重点     ******/
 // 获取excel C2的文本
$cell = $objPHPExcel->getActiveSheet()->getCell('C2')->getValue(); 
 // 开始格式化 
 if(is_object($cell))  $cell= $cell->__toString();

 

事例参考 $file_name=“xxx.xls”

public function excel($file_name){
        //$file_name= './Upload/excel/123456.xls';
        import("Org.Util.PHPExcel");   // 这里不能漏掉
        import("Org.Util.PHPExcel.IOFactory");
        $objReader = \PHPExcel_IOFactory::createReader('Excel5');
        $objPHPExcel = $objReader->load($file_name,$encode='utf-8');
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow(); // 取得总行数
        $highestColumn = $sheet->getHighestColumn(); // 取得总列数
        
        for($i=1;$i<$highestRow+1;$i++){
            $data[$i]['a'] = $objPHPExcel->getActiveSheet()->getCell('A'.$i)->getValue();
            $data[$i]['b'] = $objPHPExcel->getActiveSheet()->getCell('B'.$i)->getValue();
            $data[$i]['c'] = $objPHPExcel->getActiveSheet()->getCell('C'.$i)->getValue();
            $data[$i]['d'] = $objPHPExcel->getActiveSheet()->getCell('D'.$i)->getValue();    
            $data[$i]['e'] = $objPHPExcel->getActiveSheet()->getCell('E'.$i)->getValue();    
            $data[$i]['f'] = $objPHPExcel->getActiveSheet()->getCell('F'.$i)->getValue();    
            $data[$i]['g'] = $objPHPExcel->getActiveSheet()->getCell('G'.$i)->getValue();
            
            //格式化数据
            if(is_object($data[$i]['a']))  $data[$i]['a']= $data[$i]['a']->__toString();
            if(is_object($data[$i]['b']))  $data[$i]['b']= $data[$i]['b']->__toString();
            if(is_object($data[$i]['c']))  $data[$i]['c']= $data[$i]['c']->__toString();
            if(is_object($data[$i]['d']))  $data[$i]['d']= $data[$i]['d']->__toString();
            if(is_object($data[$i]['e']))  $data[$i]['e']= $data[$i]['e']->__toString();
            if(is_object($data[$i]['f']))  $data[$i]['f']= $data[$i]['f']->__toString();
            if(is_object($data[$i]['g']))  $data[$i]['g']= $data[$i]['g']->__toString();    
        }

        return $data;
     
    }

 

posted @ 2016-07-09 15:02  狂奔的蜗牛Snails  阅读(2412)  评论(0编辑  收藏  举报