如何读取PHPExcel中的空单元格?

时间:2015-06-09 17:02:37

标签: php codeigniter phpexcel

以下代码简单地忽略了我的excelsheet中的所有空单元格。

有没有办法读取空单元格或将它们替换为“空”值?

请参阅此处的代码--- https://arjunphp.com/how-to-use-phpexcel-with-codeigniter/

$file = './files/test.xlsx';
//load the excel library
$this->load->library('excel');
//read file from path
$objPHPExcel = PHPExcel_IOFactory::load($file);
//get only the Cell Collection
$cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection();

$maxCell = $objPHPExcel->getHighestRowAndColumn();
$newdata = array();
//extract to a PHP readable array format
foreach ($cell_collection as $cell) {

    I tried this following code 
    $newdata = $objPHPExcel->rangeToArray($cell . $maxCell['column'] . $maxCell['row']);
    $newdata = array_map('array_filter', $data);
    $newdata = array_filter($data);

    $column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();
    $row = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow();
    $data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue();
   //header will/should be in row 1 only. of course this can be modified to suit your need.
    if ($row == 1) {
       $header[$row][$column] = $data_value;
    } else {
       $arr_data[$row][$column] = $data_value;
    }
}
//send the data in an array format
$data['header'] = $header;
$data['values'] = $arr_data;

print_r($newdata);

Get this error messgae... Fatal error: Call to undefined method PHPExcel::getHighestRowAndColumn() 

1 个答案:

答案 0 :(得分:0)

您尝试以错误的方式进行操作,这样只能获得最后一个活动单元格的值,如果尝试在中间获取空单元格的值,则将无法获得值。并且您使用了某些PhpSpreadsheet函数,并且有时还尝试使用class对象获取值。再次检查您的代码。