0

How to get Excel data into an array in PHP local site. I want to print data in Excel as array in my practice site. I have created Excel in htdocs as test.xlsx. How can I print it's data in PHP?

3
  • stackoverflow.com/questions/563670/reading-an-excel-file-in-php Commented Jun 30, 2015 at 7:16
  • First off, are you talking about a real xlsx file, created with MS Excel? Or simply a csv or html markup file with an extension of .xlsx? Commented Jun 30, 2015 at 7:27
  • Consider using a library like PHPExcel Commented Jun 30, 2015 at 7:27

1 Answer 1

1

First include PhpExcel library into your code then use this

PHPExcel_IOFactory::addSearchLocation($filepath, $savedfilename);
        $objReader = PHPExcel_IOFactory::load($filepath);

        $data = array();
        $worksheet = $objReader->getActiveSheet();
        foreach ($worksheet->getRowIterator(2) as $row) {
            $cellIterator = $row->getCellIterator();
            $cellIterator->setIterateOnlyExistingCells(false);
            foreach ($cellIterator as $cell) {
                $data[$cell->getRow()][$cell->getColumn()] = $cell->getValue();
            }
        }
print_r($data);
Sign up to request clarification or add additional context in comments.

1 Comment

Or you could simply use the HTML Writer and save the output to php://output.... even fewer lines of code

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.