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;
     
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM