Question

I am reading an excel spreadsheet with PHP COM utility, everything is working fine except there are some cells in Excel file having different language data. When I read this data through PHP Com it displays like ???????

$ExlApp = new COM ( "Excel.Application" );
$workbook = $ExlApp->Workbooks->Open ( 'f:\dev\htdocs\excel\testfile.xlsx' );
$worksheet = $workbook->worksheets ( 1 );

$done = false;
$row_index = 1;
while ( $done == false ) {

    $english = $worksheet->cells ( $row_index, 1 )->value;
    $dari = $worksheet->cells ( $row_index, 2 )->value;

    if ($english != '') {
        $row_index ++;
        echo "<div style='float:left;width:420px'>".$english."</div><div>".$dari."</div>";
    } else {
        $done = true;
    }
}

$workbook->close ();

I have checked page encoding and its set to UTF-8. When I open original excel file it shows correct text but when I read it from PHP COM the encoding is lost. Does anyone have solution to this problem.

EDIT

How I can ensure that the value given by excel $worksheet->cells ( $row_index,2)->value is in correct encoding OR is there any property in Excel which I can set through PHP COM so it return data in UTF-8?

I have checked the encoding of value returned by Excel cell through mb_detect_encoding function in PHP and it gives ASCII where as it must give UTF-16 or UTF-8. It appears that excel does not give value in correct encoding.

Here is the Excel file I am reading with this script: http://asimishaq.com/myfiles/testfile.xlsx

Note that the solution is required using PHP COM-INTEROP only.

Was it helpful?

Solution

As pointed out by @rc we need to specify codepage property in COM constructor to obtain data in correct encoding.

$ExlApp = new COM ( "Excel.Application", NULL, CP_UTF8 );

By changing the above line in script the data is displayed correctly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top