zoukankan      html  css  js  c++  java
  • 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;
         
        }
  • 相关阅读:
    P1144 最短路计数
    P2966 [USACO09DEC]牛收费路径Cow Toll Paths
    P2419 [USACO08JAN]牛大赛Cow Contest
    P1462 通往奥格瑞玛的道路
    P1346 电车
    P1339 [USACO09OCT]热浪Heat Wave
    P1418 选点问题
    P1330 封锁阳光大学
    P1182 数列分段Section II
    P2661 信息传递
  • 原文地址:https://www.cnblogs.com/wesky/p/5655904.html
Copyright © 2011-2022 走看看