Rad
Rad

Reputation: 4921

Get a cell type using PHPExcel

Hi I have this code to read form excel file and add to database using joomla

try 
        {
            $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
            $objReader = PHPExcel_IOFactory::createReader($inputFileType);
            $objReader = PHPExcel_IOFactory::createReader($inputFileType);
            $objPHPExcel = $objReader->load($inputFileName);
        } 
        catch(Exception $e) {
            die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
        }

        $sheet = $objPHPExcel->getSheet(0); 
        $highestRow = $sheet->getHighestRow(); 
        $highestColumn = $sheet->getHighestColumn();

        for($i = 11; $i <= $highestRow; $i++)
        {
$rowData = $sheet->rangeToArray('A' . $i . ':' . $highestColumn . $i,
                                                NULL,
                                                TRUE,
                                                True
                                                );
                $row = $rowData[0];
$valeur1 = $row[8];
}

I open the file and I read it each row and for each row I read each cell

well some time the row[8] is a formated number for exemple (100100.5) some times not like (100,100.5) or something else Well I need to know the cell type to make some change if the cell is not a number.

Upvotes: 1

Views: 6340

Answers (1)

Mark Baker
Mark Baker

Reputation: 212402

Using

$rowData = $sheet->rangeToArray(
    'A' . $i . ':' . $highestColumn . $i,
    NULL,
    True,
    False
);

will give you raw values back from the range rather than formatted values

Alternatively, for an individual cell, you can use

$sheet->getCell('A8')->getDataType();

to get the actual datatype stored in the cell. The list of datatypes is defined in Classes/PHPExcel/Cell/DataType.php

const TYPE_STRING2  = 'str';
const TYPE_STRING   = 's';
const TYPE_FORMULA  = 'f';
const TYPE_NUMERIC  = 'n';
const TYPE_BOOL     = 'b';
const TYPE_NULL     = 'null';
const TYPE_INLINE   = 'inlineStr'; // Rich text
const TYPE_ERROR    = 'e';

Upvotes: 5

Related Questions