Example of using PHPExcel to identify and format the date format in Excel

Source: Internet
Author: User
Tags php excel
Example of using PHPExcel to identify and format the date format in Excel

  1. Function Read_Excel_File2 ($ file_name, & $ result ){
  2. Require_once 'include/PHPExcel/Classes/PHPExcel/IOFactory. php ';
  3. $ Result = null;
  4. $ ObjReader = PHPExcel_IOFactory: createReader ('excel5 ');
  5. // $ ObjReader-> setReadDataOnly (true );
  6. Try {
  7. $ ObjPHPExcel = $ objReader-> load ($ file_name );
  8. } Catch (Exception $ e ){}
  9. If (! Isset ($ objPHPExcel) return "file resolution fails ";
  10. $ AllobjWorksheets = $ objPHPExcel-> getAllSheets ();
  11. Foreach ($ allobjWorksheets as $ objWorksheet ){
  12. $ Sheetname = $ objWorksheet-> getTitle ();
  13. $ HighestRow = $ objWorksheet-> getHighestRow (); // e.g. 10
  14. $ HighestColumn = $ objWorksheet-> getHighestColumn ();
  15. $ HighestColumnIndex = PHPExcel_Cell: columnIndexFromString ($ highestColumn );
  16. For ($ row = 1; $ row <= $ highestRow; ++ $ row ){
  17. For ($ col = 0; $ col <= $ highestColumnIndex; ++ $ col ){
  18. $ Cell = $ objWorksheet-> getCellByColumnAndRow ($ col, $ row );
  19. $ Value = $ cell-> getValue ();
  20. If ($ cell-> getDataType () = PHPExcel_Cell_DataType: TYPE_NUMERIC ){
  21. $ Cellstyleformat = $ cell-> getParent ()-> getStyle ($ cell-> getCoordinate ()-> getNumberFormat ();
  22. $ Formatcode = $ cellstyleformat-> getFormatCode ();
  23. If (preg_match ('/^ (\ [\ $ [A-Z] *-[0-9A-F] * \]) * [hmsdy]/I', $ formatcode )){
  24. $ Value = gmdate ("Y-m-d", PHPExcel_Shared_Date: ExcelToPHP ($ value ));
  25. } Else {
  26. $ Value = PHPExcel_Style_NumberFormat: toFormattedString ($ value, $ formatcode );
  27. }
  28. // Echo $ value, $ formatcode ,'
    ';
  29. }
  30. $ Result [$ sheetname] [$ row-1] [$ col] = $ value;
  31. }
  32. }
  33. }
  34. Return 0;
  35. }

The date judgment mainly includes the following parts:

  1. $ Cell = $ objWorksheet-> getCellByColumnAndRow ($ col, $ row );
  2. $ Value = $ cell-> getValue ();
  3. If ($ cell-> getDataType () = PHPExcel_Cell_DataType: TYPE_NUMERIC ){
  4. $ Cellstyleformat = $ cell-> getParent ()-> getStyle ($ cell-> getCoordinate ()-> getNumberFormat ();
  5. $ Formatcode = $ cellstyleformat-> getFormatCode ();
  6. If (preg_match ('/^ (\ [\ $ [A-Z] *-[0-9A-F] * \]) * [hmsdy]/I', $ formatcode )){
  7. $ Value = gmdate ("Y-m-d", PHPExcel_Shared_Date: ExcelToPHP ($ value ));
  8. } Else {
  9. $ Value = PHPExcel_Style_NumberFormat: toFormattedString ($ value, $ formatcode );
  10. }
  11. }

The above PHPExcel version is 1.7.2.

Articles you may be interested in: examples of common PHPExcel methods examples of php excel export simple examples use phpexcel class library export excelphpExcel use methods share phpexcel export excel classic instance PHPExcel Read excel file example phpexcel class library instance support (excel2003 excel2007) phpexcel example code for exporting data phpexcel the code for importing data to the database phpexcel quick Development Guide (good) phpExcel Chinese help Manual (knowledge points) how to export the excel color from phpexcel is different from the color on the webpage solution: Use PHPExcel in CI to export data to Excel

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.