-1

样品 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
    )

 )   
4

1 回答 1

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

?>
于 2012-07-06T15:06:20.637 回答