Unknown Error when PHPExcel Read a Cell with chinese string

Topics: Developer Forum, User Forum
Jan 8, 2009 at 3:58 AM
this is my code:
<?php
error_reporting(E_ALL);

/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . '../class');

/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';

echo date('H:i:s') . " Load from Excel file<br/>";
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load("2009Q1.xlsx");

//$objReader = PHPExcel_IOFactory::createReader('Excel5');
//$objPHPExcel = $objReader->load("2009Q1.xls");
$sheet = $objPHPExcel->getSheet(0);
// Echo memory peak usage
echo date('H:i:s') . " Peak memory usage: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MB<br/>";
//
$HighestRow = $objPHPExcel->getActiveSheet()->getHighestRow();
$HighestCol = $objPHPExcel->getActiveSheet()->getHighestColumn();

//print_r($objPHPExcel);
for($i=1;$i<=$HighestRow;$i++){
    for($j='A';$j<=$HighestCol;$j++){
        $val = $sheet->getCell($j.$i)->getValue();
        echo $j.$i."[".$charset."]=".$val."<br>";
    }
}
?>

Webpage will print this file content like this:

11:54:29 Load from Excel file
11:54:30 Peak memory usage: 9.25 MB
A1[]=项目
B1[]=模块
C1[]=功能
D1[]=备注
E1[]=工作量
F1[]=截止时间
G1[]=优先级
A2[]=UNIQLO
B2[]=电话处理模块
C2[]=IB电话内容监控
Unknown Error Type: 4096 - D:\xampp\htdocs\harmony\t.php @ L28

Cell C3's Content was:  
IB内容统计/各项内容比率



Jan 8, 2009 at 4:02 AM
if i change my code as this:
        echo $j.$i."[".$charset."]=".$val."<br>";   --->    echo $j.$i."[".$charset."]=".urldecode(urlencode($val))."<br>";

It will output a blank line and without any error message.
Developer
Jan 8, 2009 at 4:03 AM
Can you send me the file? (erik at phpexcel dot net)

Jan 8, 2009 at 4:21 AM
I sent a mail to you just now.
Developer
Jan 8, 2009 at 4:54 AM
Edited Jan 8, 2009 at 4:54 AM
The problem is that some of your cell values contain Rich-Text. If you try to echo a Rich-Text value you get error.

I found out like this:

for($i=1;$i<=$HighestRow;$i++){
    for($j='A'; $j<=$HighestCol; $j++){
        $val = $sheet->getCell($j . $i)->getValue();

        //echo $j.$i."[]=".$val."<br>";
        var_dump(get_class($sheet->getCell($j . $i)->getValue()));

        }
}

If you are only interested in the plain text, use this:

$objReader->setReadDataOnly(true);

Related discussion:
http://www.codeplex.com/PHPExcel/Thread/View.aspx?ThreadId=34513

Developer
Jan 8, 2009 at 5:00 AM
@maarten: Is it possible to implement magical __toString() method for PHPExcel_RichText ?

Jan 8, 2009 at 5:28 AM
you are right.

if I add $objReader->setReadDataOnly(true); it's works.

thank you very much.