Applying phpexcel to discriminate and format date formats in Excel

Source: Internet
Author: User
Use Phpexcel to discriminate and format date formats in Excel
Recently, I was doing an import function, using the Phpexcel class library in the system, reading the uploaded Excel file, then mapping the fields, and finally importing the data in Excel into the system. Among them, about the date format of the import, make me make a difficult.
First, how to import the date, because the date format in the Phpexcel is obtained by the number, not the date string, so there is a lot of code on the Internet to convert the numbers obtained in Excel into the Unix time used by PHP, in fact, it does not have to be so troublesome. There are ways to do this in Phpexcel: phpexcel_shared_date::exceltophp ($value)。 can be used directly.
If it ends here, then this article is a bit too boring, in the development, I have not yet a problem to make a difficult, that is how to automatic discriminant of Excel cells is a date type, as usual, I Google again, there is no good article to solve the problem, so I looked at the Phpexcel document and source, while groping, finally I found the method, nonsense not to say, I first put the code:
function Read_excel_file2 ($file _name,& $result) {require_once ' include/phpexcel/classes/phpexcel/iofactory.php    ';    $result =null;    $objReader = Phpexcel_iofactory::createreader (' Excel5 ');//$objReader->setreaddataonly (TRUE);    try{$objPHPExcel = $objReader->load ($file _name);    }catch (Exception $e) {} if (!isset ($objPHPExcel)) return "Cannot parse file";    $allobjWorksheets = $objPHPExcel->getallsheets ();        foreach ($allobjWorksheets as $objWorksheet) {$sheetname = $objWorksheet->gettitle (); $highestRow = $objWorksheet->gethighestrow (); e.g. 10$highestcolumn = $objWorksheet->gethighestcolumn (); $highestColumnIndex = Phpexcel_cell::        Columnindexfromstring ($highestColumn);                for ($row = 1; $row <= $highestRow; + + $row) {for ($col = 0; $col <= $highestColumnIndex; + + $col) {                $cell = $objWorksheet->getcellbycolumnandrow ($col, $row);                $value = $cell->getvalue (); if ($cell->getdatatYpe () ==phpexcel_cell_datatype::type_numeric) {$cellstyleformat = $cell->getparent ()->getstyle ($cel                    L->getcoordinate ())->getnumberformat ();                    $formatcode = $cellstyleformat->getformatcode (); if (Preg_match ('/^ (\[\$[a-z]*-[0-9a-f]*\]) *[hmsdy]/i ', $formatcode)) {$value =gmdate ("y-m-d", Phpexc                    el_shared_date::exceltophp ($value));                    }else{$value =phpexcel_style_numberformat::toformattedstring ($value, $formatcode); }//Echo $value, $formatcode, '
'; } $result [$sheetname] [$row -1][$col]= $value; }}} return 0;}

Among them, the part about date judgment is mainly the following parts:
$cell = $objWorksheet->getcellbycolumnandrow ($col, $row); $value = $cell->getvalue (); if ($cell->getdatatype ( ) {       $cellstyleformat = $cell->getparent ()->getstyle ($cell, ==phpexcel_cell_datatype::type_numeric) Getcoordinate ())->getnumberformat ();       $formatcode = $cellstyleformat->getformatcode ();       if (Preg_match ('/^ (\[\$[a-z]*-[0-9a-f]*\]) *[hmsdy]/i ', $formatcode)) {             $value =gmdate ("y-m-d", Phpexcel_shared_ date::exceltophp ($value));       } else{             $value =phpexcel_style_numberformat::toformattedstring ($value, $formatcode);       }}


By the way, my phpexcel version is 1.7.2.

1/F xiaoluozi513 2012-10-28

if (Preg_match ('/^ (\[\$[a-z]*-[0-9a-f]*\]) *[hmsdy]/i ', $formatcode))
I can't understand the basis of this judgment, $formatcode value seems to be "general"

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.