php 忽略空单元格 PHPExcel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12285511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ignore empty cells PHPExcel
提问by user765368
I'm using the library PHPExcelto read data in an Excel file. The problem I'm having, is that when I use something like:
我正在使用PHPExcel库来读取 Excel 文件中的数据。我遇到的问题是,当我使用以下内容时:
$obj = PHPExcel_IOFactory::load($file);
$data = $obj->getActiveSheet()->toArray(null,true,true,true);
To load my file and convert its content into an array, I get all the columns and rows of my Excel file in my array even those without any data in them. Is there a method or something in the library PHPExcel to tell it to ignore cells in my Excel sheet that do not contain any data? (Instead of having a bunch of empty associative arrays in my $data)
为了加载我的文件并将其内容转换为数组,我将 Excel 文件的所有列和行都放在我的数组中,即使是那些没有任何数据的列和行。PHPExcel 库中是否有一种方法或其他东西可以告诉它忽略我的 Excel 工作表中不包含任何数据的单元格?(而不是在 my 中有一堆空的关联数组$data)
回答by TiGR
If your problem is in getting empty columns that go after real data, and you would like to avoid these, you could do something like this:
如果您的问题是获取跟踪真实数据的空列,并且您想避免这些问题,您可以执行以下操作:
$maxCell = $sheet->getHighestRowAndColumn();
$data = $sheet->rangeToArray('A1:' . $maxCell['column'] . $maxCell['row']);
This will return array representing only the area containing real data.
这将返回仅代表包含真实数据的区域的数组。
回答by HMagdy
I have this solution for my case
我有这个解决方案
$maxCell = $objWorksheet->getHighestRowAndColumn();
$data = $objWorksheet->rangeToArray('A1:' . $maxCell['column'] . $maxCell['row']);
return all rows with all empty string as:
返回所有包含空字符串的行:
[1] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
)
To remove these empty rows
删除这些空行
$data = array_map('array_filter', $data);
will return
将返回
[1] => Array ( )
[1] => 数组 ( )
And this is the finale solution:
这是最终的解决方案:
$maxCell = $objWorksheet->getHighestRowAndColumn();
$data = $objWorksheet->rangeToArray('A1:' . $maxCell['column'] . $maxCell['row']);
$data = array_map('array_filter', $data);
$data = array_filter($data);
will return an array with only filled rows .. hope that help
将返回一个只有填充行的数组..希望有所帮助
回答by Mark Baker
No there isn't. The toArray() method returns the first argument (NULL) to represent an empty cell. You can then apply standard PHP array functions such as array_filter() to eliminate empty cells.
不,没有。toArray() 方法返回第一个参数 (NULL) 来表示一个空单元格。然后,您可以应用标准 PHP 数组函数(例如 array_filter())来消除空单元格。
foreach($data as $key => &$row) {
$row = array_filter($row,
function($cell) {
return !is_null($cell);
}
);
if (count($row) == 0) {
unset($data[$key]);
}
}
unset ($row);
This will eliminate every cell that is a NULL (empty) value, and every row that comprises nothing but empty cells. It will preserve the array keys, so your array keys will still give you a cell reference.
这将消除每个为 NULL(空)值的单元格,以及只包含空单元格的每一行。它将保留数组键,因此您的数组键仍将为您提供单元格引用。
Note that an cell containing an empty string is not a null cell, so these will be retained, although the array_filter() callback could be modified to remove them as well.
请注意,包含空字符串的单元格不是空单元格,因此将保留这些单元格,尽管也可以修改 array_filter() 回调以删除它们。

