简体   繁体   中英

How to read empty cells in PHPExcel?

This following code simply ignore all empty cells in my excelsheet.

Is there any way to read empty cell or replace them to "Null" values?

See the code here--- 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() 

you're try to wrong way, in this way you only get the last active cell value and if you try to get empty cell cell value in middle you're not able to get value. and you use some PhpSpreadsheet function and also some time you try get value using class object. check your code one more time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM