Search code examples
phpcellphpexcelimport-from-excel

Get value from cell and save as variable


I downloaded the phpexcel lib. What I want to do is simple: Open an .xls file that is in the same directory, read a specific cell and save the value as a variable (only letters and numbers are in the cell).

What I have so far:

<?php 

/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');

define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');

/** Include PHPExcel */
require_once dirname(__FILE__) . '/PHPExcel_1.8.0_doc/Classes    /PHPExcel.php';

$inputFileName = 'inventory.xls';

/** Load $inputFileName to a PHPExcel Object  **/
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);

// Get cell B8
$objPHPExcel->getActiveSheet()->getCellByColumnAndRow(0, 2)->getValue();

?>

Solution

  • This is literally all you need to do:

    $value = $objPHPExcel->getActiveSheet()->getCellByColumnAndRow(0, 2)->getValue();
    

    I have no idea how you managed to use the PHPExcel library which is full OOP but had no idea how to assign a value to a basic PHP variable...