4
<?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 #09</title>

</head>
<body>


<?php

/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/');

/** PHPExcel_IOFactory */
include 'C:\webserver\www\Classes\PHPExcel\IOFactory.php';


$inputFileType = 'Excel5';
//  $inputFileType = 'Excel2007';
//  $inputFileType = 'Excel2003XML';
//  $inputFileType = 'OOCalc';
//  $inputFileType = 'Gnumeric';
$inputFileName = './sampleData/Book1.xls';
$sheetname = 'Data Sheet #3';


class MyReadFilter implements PHPExcel_Reader_IReadFilter
{
    public function readCell($column, $row, $worksheetName = '') {

        if ($row >= 1 && $row <= 1) {
            if (in_array($column,range('A','A'))) {
                return true;
            }
        }
        return false;
    }
}

$filterSubset = new MyReadFilter();

$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setLoadSheetsOnly($sheetname);
$objReader->setReadFilter($filterSubset);
$objPHPExcel = $objReader->load($inputFileName);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
var_dump($sheetData);


?>
<body>
</html>

When i run aboce code it gives me this output

array(1) { [1]=> array(1) { ["A"]=> string(6) "ABCDEF" } }

In above output ABCDEF is first cell data. so i want only ABCDEF not other things as shown above.

2 Answers 2

4

That is because you asked PHPExcel to give you the whole worksheet as an array.

If you just want the value of cell A1 use:

$objPHPExcel->getActiveSheet()->getCell('A1')->getValue();

instead of

$objPHPExcel->getActiveSheet()->toArray(null,true,true,true);

More about this in the documentation, in this case the file "PHPExcel developer documentation.pdf".

You can then also remove the read filter altogether. The cells are read in a lazy manner, so you don't save anything with the read filter if you only access cell A1 directly anyway.

2
  • string(6) "ABCDEF" this output i got by using above line of code you said. Dec 31, 2012 at 4:32
  • Go find out about the difference between var_dump() and echo.
    – AndreKR
    Dec 31, 2012 at 4:36
0

include 'C:\webserver\www\Classes\PHPExcel\IOFactory.php';

this include file which is include by me file in this file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.