Example of using PHPExcel to identify and format the date format in Excel
- 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 "file resolution fails ";
- $ 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 ($ cell-> 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 );
- }
- // Echo $ value, $ formatcode ,'
';
-
- }
- $ Result [$ sheetname] [$ row-1] [$ col] = $ value;
- }
- }
- }
- Return 0;
- }
The date judgment mainly includes the following parts:
- $ Cell = $ objWorksheet-> getCellByColumnAndRow ($ col, $ row );
- $ Value = $ cell-> getValue ();
- If ($ cell-> getDataType () = PHPExcel_Cell_DataType: TYPE_NUMERIC ){
- $ Cellstyleformat = $ cell-> getParent ()-> getStyle ($ cell-> 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 );
- }
- }
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 |