phpexcel 字串轉碼

來源:互聯網
上載者:User

標籤:

問題狀況:在匯入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;         }

 

phpexcel 字串轉碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.