xlsx不会使用SimpleXLSX返回空单元格

时间:2012-07-06 12:30:59

标签: php xlsx

sample xlsx

解决 嗨,目前我正在使用SimpleXLSX来解析xlsx(excel文件),在返回数组值时删除所有空单元格值,但我需要保留任何单元格的空/ NULL值返回值

     $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
    )

 )   

1 个答案:

答案 0 :(得分:-1)

未经测试的代码:

<?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);

?>