Get only 2 column from XLS file

Topics: Developer Forum
Aug 9, 2012 at 4:42 PM

My project work fine

Now i want take from my xls file only the column A and D, every one row

this is the class :

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(0,0,range('A','D'));


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);

but don't work
i have one row null

Coordinator
Aug 9, 2012 at 4:58 PM

In Excel, row numbers start at 1

Aug 9, 2012 at 5:07 PM

ok thanks

$filterSubset = new MyReadFilter(1,10,range('A','D'));
but i want all the row and only column A and column D.

Coordinator
Aug 9, 2012 at 8:34 PM

If you want all rows, then you only test the column

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

Aug 10, 2012 at 6:37 AM

I made changes:

class MyReadFilter implements PHPExcel_Reader_IReadFilter
{
	private $_columns = array();
	public function __construct($startRow, $endRow, $columns) {
	
		$this->_columns		= $columns;
	}
public function readCell($column, $row, $worksheetName = '') {
    if (in_array($column,$this->_columns)) {
        return true;
    }
    return false;

	}
}

$filterSubset = new MyReadFilter(range('A','D'));

but the result is:

"","","","","","","","","","","","","","","","","","","","","",""

thanks

Coordinator
Aug 10, 2012 at 6:44 AM
Edited Aug 10, 2012 at 6:45 AM

Columns B and C still exist in the loaded worksheet, they're just empty.... PHPExcel doesn't "shuffle" unread columns so that they are consecutive so cell D1 is still cell D1, likewise unread rows will also still exist but contain only empty cells. This technique is for memory saving, not for compacting data.

Your constructor for the read filter, and the statement that instantiates it don't match.

I don't know what you expect to see... your "result" is meaningless unless you know what the original sheet contained.

Aug 10, 2012 at 7:12 AM

ok i change

class MyReadFilter implements PHPExcel_Reader_IReadFilter
{
	private $_columns = array();
	public function __construct( $columns) {
	
		$this->_columns		= $columns;
	}
public function readCell($column, $row, $worksheetName = '') {
    if (in_array($column,$this->_columns)) {
        return true;
    }
    return false;

	}
}


$filterSubset = new MyReadFilter(range('A','D'));

now the result is:

"Codice MinSan","Codice EAN","Descrizione","Giacenza","","","","","","","","","","","","","","","","","","""

908009448    ","             ","GERME GRANO CONC OLEOSO 100ML           ","1","","","","","","","","","","","","","","","","","","""

912543737    ","8029041141116","DEFENCE SUN LTT 50  PROT M ALT          ","1","","","","","","","","","","","","","","","","","","""

907037509    ","             ","DERMO-LABOCAINA POLVERE 75G             ","1","","","","","","","","","","","","","","","","","","""

939181602    ","             ","EUCLOINTIMA ATTIVA 200ML                ","47","","","","","","","","","","","","","","","","","","" ecc...........

his take all column

i want take only column A ("Codice Minsan") and column D ("Giacenza")

thanks