PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 4, 2022

[FIXED] How to get excel sheet cell dataType in PHP using PHPExcel_IOFactory

 October 04, 2022     excel, php, phpexcel     No comments   

Issue

Here I get all cell values from excel sheet(test.xlsx) using PHPExcel libraries. But I'm not getting the value of dataType. I need excel sheet data with this dataType(String or Number Or Date).

My Code:

require_once 'Classes/PHPExcel.php';
$file_path = "test.xlsx";
try {
    $objPHPExcel = PHPExcel_IOFactory::load($file_path);
} catch(Exception $e) {
    die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);

print_r($sheetData);

Advise Please!


Solution

You can retrieve the datatype for a cell by calling the getDataType() method for a cell:

$objPHPExcel->getActiveSheet()
    ->getCell('A1')
    ->getDataType();

This can return values of:

- TYPE_STRING2  = 'str';          // string
- TYPE_STRING   = 's';            // string
- TYPE_FORMULA  = 'f';            // formula
- TYPE_NUMERIC  = 'n';            // number
- TYPE_BOOL     = 'b';            // boolean
- TYPE_NULL     = 'null';         // null
- TYPE_INLINE   = 'inlineStr';    // rich text string
- TYPE_ERROR    = 'e';            // error

The constants for these datatypes are defined in the PHPExcel_Cell_DataType class


Date doesn't have a specific datatype, it's simply a number as far as Excel is concerned: you need to look at the number format mask for the cell to differentiate between a float and a date.

PHPExcel provides a method for this

PHPExcel_Shared_Date::isDateTime($objPHPExcel->getActiveSheet()->getCell('A1'));


Answered By - Mark Baker
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing