Search code examples
phpxlsx

xlsx doesnt not return empty cell using SimpleXLSX


sample xlsx

Solved Hi currently im using SimpleXLSX to parse the xlsx (excel file), all the empty cell values are removed while return the array values,but I need to keep empty/NULL value of any cell in return value

     $xlsx = new SimpleXLSX($uploadfile);
     $j =1;
     //list($num_cols, $num_rows) = $xlsx->dimension();  //Previous
       $items = array();
        list($cols,) = $xlsx->dimension($j);        


    print_r($xlsx->rows());
    //foreach ($xlsx->rows() as $r) { //Previous 
     foreach( $xlsx->rows($j) as $k => $r) {  //fixed
        // if ($i != 0) {
     // for( $i=0; $i < $num_cols; $i++ )//Previous 
       for( $i = 0; $i < $cols; $i++) //fixed
    if(!empty($r[$i]))
                            {
                                $v = $r[$i];
                            }else{

                                $v = '&nbsp;';
                            }
                           // echo $v;
                             $items[$i]  = $v;
                        //   

    }
           $val = implode('|', $items);
 O/P:

 Array
(
[0] => Array
    (
        [0] => Name of the Candidate
        [1] => phone number
        [2] => Postal Address 
        [3] => mobilenumber
        [4] => country
    )

[1] => Array
    (
        [0] => abc
        [2] => 123, 
        [4] => india
    )

[2] => Array
    (
        [0] => fsdfsf
        [1] => 23423423
        [3] => 3223423423
        [4] => us
    )

 )   

Solution

  • Untested code:

    <?php
    
    $xlsx = new SimpleXLSX($uploadfile);
    list($num_rows, $num_cols) = $xlsx->dimension();
    
    $values = [];
    foreach ($xlsx->rows() as $r)
    {
        $rowValue = '';
    
        for ($j = 0; $j < $num_cols; $j++)
        {
            $rowValue .= !empty($r[$j]) && $r[$j] != '' ? $r[$j] : 'null';
        }
    
        $values[] = $rowValue;
    }
    
    var_dump($values);
    
    ?>