从 SpreadsheetExcelReader 获取最终输出数组中包含的空白列值

发布于 2024-12-27 14:48:58 字数 2812 浏览 5 评论 0原文

我正在使用以下插件来读取 microsoft excel 文件(.xls 扩展名)

http://php-spreadsheetreader.googlecode.com/svn-history/r26/Excel/OLERead.php

根据当前插件实现,它将读取的 excel 作为 大批。

下面的代码读取 Excel 并将其作为数组返回。

        include_once('spreadsheet_excel_reader.php');
        $data = new Spreadsheet_Excel_Reader_New();
        $data->setOutputEncoding('CP1251');
        $data->read('full path of file');
        echo '<pre>';print_r($data->sheets[0]['cells']);echo '</pre>';exit;

数组单元格包含的值数量等于 Excel 工作表中的行数。

每个值本身都是一个数组,其中键的数量等于工作表中的列数。

数组单元格如下所示 -

    [cells] => Array( 
                              [1] => Array
                                    (
                                        [1] => a
                                        [2] => b
                                        [3] => c
                                        [4] => d
                                        [5] => e
                                        [6] => f
                                        [7] => g
                                    )

                                [2] => Array
                                    (
                                        [1] => fddfg
                                        [2] => dfgd
                                        [5] => ghjgh 
                                        [6] => dgdf
                                        [7] => uyijkgh
                                    )

                                [3] => Array
                                    (
                                        [1] => fghfg
                                        [2] => gvsfdgdf
                                        [4] => fdg4t4  
                                        [5] => gfdg
                                        [6] => dfgd
                                        [7] => ghfghf
                                    )

                                [4] => Array
                                    (
                                        [1] => fgh
                                        [2] => sfsdf
                                        [5] => fghfgh
                                        [6] => fsdf
                                        [7] => dfgdfg
                                    ) 
)

从上面的数组中可以看出,键 1 包含工作表标题数组。 因此,Excel 工作表中总共有 7 列。 目前,如果任何单元格在工作表中不包含任何值,那么它会跳过数组中的这些值 但我的要求是,如果单元格不包含任何值,在这种情况下,数组应该包含空白值。

在插件中,整个数组创建过程发生在下面的函数中

函数_parsesheet($spos)

我尝试添加一个新案例来处理空条目,但没有成功。

对上述问题的任何帮助都会很棒。

I am using the below plugin for reading micrsoft excel file (.xls extension)

http://php-spreadsheetreader.googlecode.com/svn-history/r26/Excel/OLERead.php

As per the current plugin implementation it returns the read excel as an array.

The below code reads the excels and returns it as an array

        include_once('spreadsheet_excel_reader.php');
        $data = new Spreadsheet_Excel_Reader_New();
        $data->setOutputEncoding('CP1251');
        $data->read('full path of file');
        echo '<pre>';print_r($data->sheets[0]['cells']);echo '</pre>';exit;

The array cells contains the number of values equal to number of rows in an excel sheet.

Each of the value itself is an array consisting of number of keys equal to number of columns in the sheet.

The array cells looks as below -

    [cells] => Array( 
                              [1] => Array
                                    (
                                        [1] => a
                                        [2] => b
                                        [3] => c
                                        [4] => d
                                        [5] => e
                                        [6] => f
                                        [7] => g
                                    )

                                [2] => Array
                                    (
                                        [1] => fddfg
                                        [2] => dfgd
                                        [5] => ghjgh 
                                        [6] => dgdf
                                        [7] => uyijkgh
                                    )

                                [3] => Array
                                    (
                                        [1] => fghfg
                                        [2] => gvsfdgdf
                                        [4] => fdg4t4  
                                        [5] => gfdg
                                        [6] => dfgd
                                        [7] => ghfghf
                                    )

                                [4] => Array
                                    (
                                        [1] => fgh
                                        [2] => sfsdf
                                        [5] => fghfgh
                                        [6] => fsdf
                                        [7] => dfgdfg
                                    ) 
)

As seen from the above array the key 1 contains array of headings of the sheet.
So in all there are 7 columns in an excel sheet.
Currently if any of the cell contains no value in sheet then it skips those values in an array
but my requirement is that if a cell contains no values in that case the array should be containing blank values.

In plugin this whole array creation process takes place in below function

function _parsesheet($spos)

I tries adding a new case for handling null entries but with no success.

Any help on the above issue would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

李不 2025-01-03 14:48:58
$max = 0;
array_walk($data->sheets[0]['cells'],function($row) use (&$max) { $max = max(max(array_keys($row)),$max); });
array_walk($data->sheets[0]['cells'],function(&$row) use ($max) { $row = $row + array_fill_keys(range(1,$max),NULL); ksort($row); });

var_dump($data->sheets[0]['cells']);

用 NULL 单元格填充每个缺失的列
对于丢失的行没有做任何事情。
PHP >= 5.3.0

$max = 0;
array_walk($data->sheets[0]['cells'],function($row) use (&$max) { $max = max(max(array_keys($row)),$max); });
array_walk($data->sheets[0]['cells'],function(&$row) use ($max) { $row = $row + array_fill_keys(range(1,$max),NULL); ksort($row); });

var_dump($data->sheets[0]['cells']);

Pads every missing column with a NULL cell
Doesn't do anything about missing rows.
PHP >= 5.3.0

关于从前 2025-01-03 14:48:58

它应该如何处理内容后面的所有空值?
您可以迭代数组并填充所有缺失的行。

How should it handle all the null values after your content?
You could iterate through the array and pad all the missing rows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文