Formato de hora de importación de phpexcel y procesamiento de formato numérico

/ ** 
 * Notas: 导入 excel 文件
 * Usuario: hly 
 * Fecha: 2020/4/14 17:56 
 * / 
function importXlsx () 
{ 
    // 引入 phpexcel 
    require getcwd (). '/PHPExcel.php'; 
    $ file_name = 'test.xlsx'; // 模拟 上传 后 的 文件
    $ objReader = \ PHPExcel_IOFactory :: createReader ('Excel2007'); 
    $ objPHPExcel = $ objReader-> load ($ nombre_archivo, $ codificación = 'utf-8'); 
    $ hoja = $ objPHPExcel-> getSheet (0); 
    $ highestRow = $ hoja-> getHighestRow (); // 取得 总行 数
    $ datos = array (); 
    for ($ i = 2; $ i <= $ higherRow; $ i ++) {  
        $ data [$ i-2] ['title'] = getCellValue ($ objPHPExcel-> getActiveSheet () -> getCell ("A". $ i));
        $ data [$ i - 2] ['date_time'] = getCellValue ($ objPHPExcel-> getActiveSheet () -> getCell ("B". $ i), 'Ymd H: i: s');
    salida (json_encode ($ datos)); 
} 

función getCellValue ($ cell, $ date_format = "Ymd H: i: s") 
{ 
    $ value = $ cell-> getValue (); 
    if ($ cell-> getDataType () == \ PHPExcel_Cell_DataType :: TYPE_NUMERIC) { 
        // 版本 过低 的 话 请 加上 getParent 例 : $ cell-> getParent () -> getStyle ($ cell-> getCoordinate ()) - > getNumberFormat (); 
        $ cell_style_format = $ cell-> getStyle ($ cell-> getCoordinate ()) -> getNumberFormat (); // 不需要 getParent 
        $ format_code = $ cell_style_format-> getFormatCode (); 
        if (preg_match ('/ ^ ([$ [AZ] * - [0-9A-F] *]) * [hmsdy] / i',
            $ value = gmdate ($ date_format, \ PHPExcel_Shared_Date :: ExcelToPHP ($ value)); // 格式化 日期
        } else { 
            $ value = \ PHPExcel_Style_NumberFormat :: toFormattedString ($ value, $ format_code); // 格式化 数字
        } 
    } 
    return $ valor; 
} 

importXlsx ();

  

Supongo que te gusta

Origin www.cnblogs.com/handle/p/12699660.html
Recomendado
Clasificación