unable to read data or Configurable Read Filter to a specified col and row using PHPexcel script

StackOverflow https://stackoverflow.com/questions/7306028

  •  25-10-2019
  •  | 
  •  

Question

I have tried using a example file using PHPExcel script to extract data from excel file. Extraction of data was done successfully but not as restricted.

In details about the issue: This (mentioned Below) file it is bound to read a excel file with specific number of rows to a ranged columns i.e ('A','Z'). Rows were taken into count but not the columns. It takes upto column O only for any specified range for column. Even I have tested to give more column range and it displays from column "A" to the extent of column "O" and Not more than "O"

Can any one sort the Issue, and let me know If i am wrong.

Waiting for your valued suggestions and Inputs.

Waiting for the earliest reply

Thanks in advance,

Jimson Jose,

all the code is as shown below

<?php

error_reporting(E_ALL);
set_time_limit(0);

date_default_timezone_set('Europe/London');

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>PHPExcel Reader Example #10</title>

</head>
<body>

<h1>PHPExcel Reader Example #10</h1>
<h2>Simple File Reader Using a Configurable Read Filter</h2>
<?php

set_include_path(get_include_path() . PATH_SEPARATOR . '/import/Classes/');

/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';

$inputFileType = 'Excel2007';
$inputFileName = './book2.xlsx';
$sheetname = 'Sheet2';

class MyReadFilter implements PHPExcel_Reader_IReadFilter
{
    private $_startRow = 0;

    private $_endRow = 0;

    private $_columns = array();

    public function __construct($startRow, $endRow, $columns) {
        $this->_startRow    = $startRow;
        $this->_endRow      = $endRow;
        $this->_columns     = $columns;
    }

    public function readCell($column, $row, $worksheetName = '') {
        if ($row >= $this->_startRow && $row <= $this->_endRow) {
            if (in_array($column,$this->_columns)) {
                return true;
            }
        }
        return false;
    }
}

$filterSubset = new MyReadFilter(9,15,range('G','K'));

echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory with a defined reader type of 

',$inputFileType,'<br />';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
echo 'Loading Sheet "',$sheetname,'" only<br />';
$objReader->setLoadSheetsOnly($sheetname);
echo 'Loading Sheet using configurable filter<br />';
$objReader->setReadFilter($filterSubset);
$objPHPExcel = $objReader->load($inputFileName);

echo '<hr />';

$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
var_dump($sheetData);
?>
<body>
</html>
Was it helpful?

Solution

The toArray() method will display the worksheet from cell A1 to the highest loaded row and column, so even though you haven't loaded rows 1-8, they will still appear as empty cells in the returned array; likewise, columns A-F will also appear as empty cells in the array.

Note that styles are still loaded for all cells in the worksheet, not just for those that fall within your read filter range. This is because all styles are loaded before the cells are loaded, and the read filter only checks cells.

If you want to retuen an array containing only the cells that you loaded via your filter, you should use the rangeToArray() method.

$sheetData = $objPHPExcel->getActiveSheet()->rangeToArray('G9:K15',null,true,true,true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top