php excel reader - ignore cells with special symbols
Asked Answered
S

1

6

I use the parser for converting xls to csv http://code.google.com/p/php-excel-reader/

<?php    
set_time_limit(300);
require_once 'excel_reader2.php';    
$data = new Spreadsheet_Excel_Reader("file.xls", false, 'UTF-8');    

$f = fopen('file.csv', 'w');    
for($row = 1; $row <= $data->rowcount(); $row++)    
{    
    $out = '';    
    for($col = 1; $col <= $data->colcount(); $col++)    
    {    
        $val = $data->val($row,$col);

        // escape " and \ characters inside the cell    
        $escaped = preg_replace(array('#”#u', '#\\\\#u', '#[”"]#u'), array('"', '\\\\\\\\', '\"'), $val);    
        if(empty($val))    
            $out .= ',';    
        else    
            $out .= '"' . $escaped . '",';    
    }
    // remove last comma (,)    
    fwrite($f, substr($out, 0, -1));    
    fwrite($f, "\n");
}
fclose($f);

?>

From some strange reason it skip cells with specials symbols - like ° or ®. How it can be fixed?

Sinistral answered 24/2, 2015 at 10:4 Comment(2)
try using php.net/manual/fr/function.html-entity-decode.phpVibrio
you got some advance with your problem?Blatt
B
3

utf8_decode and html_entity_decode works for me:

<?php    
set_time_limit(300);
require_once 'excel_reader2.php';    
$data = new Spreadsheet_Excel_Reader("file.xls", false, 'UTF-8');    

$f = fopen('file.csv', 'w');    
for($row = 1; $row <= $data->rowcount(); $row++)    
{    
    $out = '';    
    for($col = 1; $col <= $data->colcount(); $col++)    
    {    
        $val = $data->val($row,$col);

        // escape " and \ characters inside the cell    
        $escaped = preg_replace(array('#”#u', '#\\\\#u', '#[”"]#u'), array('"', '\\\\\\\\', '\"'), $val);    
        $escaped = utf8_decode($escaped);
        //$escaped = html_entity_decode($escaped);
        if(empty($val))    
            $out .= ',';    
        else    
            $out .= '"' . $escaped . '",';    
    }
    // remove last comma (,)    
    fwrite($f, substr($out, 0, -1));    
    fwrite($f, "\n");
}
fclose($f);

?>

Output:

"1","2","3","4","5"
"a","b","c","d","e"
"6","7","°","9","10"
"q","w","e","r","t"
"®","12","13","14","15"
"z","x","c","v","b"
Blatt answered 2/5, 2015 at 6:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.