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?
-
stackoverflow.com/questions/563670/reading-an-excel-file-in-phpKeith A– Keith A2015-06-30 07:16:23 +00:00Commented 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?Mark Baker– Mark Baker2015-06-30 07:27:06 +00:00Commented Jun 30, 2015 at 7:27
-
Consider using a library like PHPExcelMark Baker– Mark Baker2015-06-30 07:27:40 +00:00Commented Jun 30, 2015 at 7:27
Add a comment
|
1 Answer
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);
1 Comment
Mark Baker
Or you could simply use the HTML Writer and save the output to php://output.... even fewer lines of code