PHPExcel how to use data from reader

14,036

Solution 1

Please read the documentation chapter 4.5 (included in the PHPExcel download package)

Solution 2

For anyone else who wants a more helpful answer, following the code in the example above, here is a quick snippet that will parse the active worksheet into a multi-dimensional array (this example ignores the ReadFilter that the OP created).

$data =  array();
$worksheet = $objPHPExcel->getActiveSheet();
foreach ($worksheet->getRowIterator() as $row) {
  $cellIterator = $row->getCellIterator();
  $cellIterator->setIterateOnlyExistingCells(false);
  foreach ($cellIterator as $cell) {
    $data[$cell->getRow()][$cell->getColumn()] = $cell->getValue();
  }
}
var_dump($data);

BTW The reason I say "more helpful answer" is that I also had a hard time figuring out the PHPExcel documentation (which consists of 6 separate documents and no real API-like reference) the first time around.

The OP says he "read all the docs", but I am guessing that, like me, he read through the file called PHPExcel User Documentation - Reading Spreadsheet Files.doc which explains how to read in a spreadsheet, but does not cover using the contents.

To find that out, you have to dig into PHPExcel developer documentation.doc and get down to part 4 (which is where I found the example above).

Yup, my fault for not being thorough, but clearly this is a recurring issue. :-)

Solution 3

for reading excel file you can use this

require_once('PHPExcel.php');
$input_file_type = PHPExcel_IOFactory::identify($excel_file);
$obj_reader = PHPExcel_IOFactory::createReader($input_file_type);
$obj_reader->setReadDataOnly(true);
        
$objPHPExcel = $obj_reader->load($excel_file);
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$highest_row = $objWorksheet->getHighestRow();
$highest_col = $objWorksheet->getHighestColumn();
//$highest_col_index = PHPExcel_Cell::columnIndexFromString($highest_col);
        
// start $row from 2, if you want to skip header
for ($counter = 2; $counter <= $highest_row; $counter++)
{
    $row = $objWorksheet->rangeToArray('A'.$counter.':'.$highest_col.$counter);
    $row = reset($row);            
}

Share:
14,036
Mirgorod
Author by

Mirgorod

Updated on June 04, 2022

Comments

  • Mirgorod
    Mirgorod almost 2 years

    I'm trying to understand how to get data from cells in PHPExcel but I have no idea. I read all documentation and I made this:

    <?php
    include('PHPExcel/Classes/PHPExcel/Reader/Excel2007.php');
    class MyReadFilter implements PHPExcel_Reader_IReadFilter
    {
        public function readCell($column, $row, $worksheetName = '') {
            // Read title row and rows 20 - 30
            if ($row == 1 || ($row >= 20 && $row <= 30)) {
                return true;
            }
    
            return false;
        }
    }
    $objReader = new PHPExcel_Reader_Excel2007();
    $objReader->setReadFilter( new MyReadFilter() );
    $objReader->setReadDataOnly(true);
    $objPHPExcel = $objReader->load("sample_mymails222.xlsx");
    print_r($objPHPExcel);
    ?>
    

    print_r show very big array. I think there is some functions to get data from cell in $objPHPExcel.

    How to do it?

    Thanx!

  • Mark Baker
    Mark Baker about 12 years
    There is a full API reference in the /Documentation/API directory in the production distributions... it's amazing how many people are completely oblivious to its existence, despite the names of the directories
  • sergioviniciuss
    sergioviniciuss over 11 years
    @MarkBaker PHPExcel is excellent! I cannot complaim about anything! I'd just give a sugestion (and I know you really don't need to do it), but I think people don't think it's easy to find the PHPExcel documentation because of the way it's being shown.In my opinion, Codeigniter's user guide is one of the best references of a great, clean and easy place to find all about codeigniter..The way you can search for information there, the examples,etc.In PHPExcel, it's a lil bit harder to do a search, but nothing really difficult. Almost all my questions here, I found the answer doing a better search
  • PlayHardGoPro
    PlayHardGoPro almost 9 years
    I know its been a looong time since you posted this. But I cant find any help. How do I print the data in $row ? I always get Undefined Offset. PLease some help =\
  • PlayHardGoPro
    PlayHardGoPro almost 9 years
    Really thank you for answering dude. I just solved the problem, found a document.doc with some explanation... Thank you sir !! <3