fastadmin 导入Excel 将表格里的时间日期转换时间戳的方法

haooyxiaod 12天前 53

修改 applicationadminlibrarytraitsBackend.php import 方法

        for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
            $values = [];
            for ($currentColumn = 0; $currentColumn < $maxColumnNumber; $currentColumn++) {
                $cell = $currentSheet->getCellByColumnAndRow($currentColumn,$currentRow);
                $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
                if($cell->getDataType()==\PHPExcel_Cell_DataType::TYPE_NUMERIC){  
                    $cellstyleformat = $cell->getStyle($cell->getCoordinate())
                    ->getNumberFormat();  

                    $formatcode = $cellstyleformat->getFormatCode(); 
                    
                    if (preg_match('/^(\[\$[A-Z]*-[0-9A-F]*\])*[hmsdy]/i', $formatcode)) {  
                   
                        $val =strtotime(gmdate("Y-m-d H:i:s", \PHPExcel_Shared_Date::ExcelToPHP($val)));  
                    }
                     
                }
                
                $values[] = is_null($val) ? '' : $val;
            }
            $row = [];
            $temp = array_combine($fields, $values);
            
            foreach ($temp as $k => $v) {
                if (isset($fieldArr[$k]) && $k !== '') {
                    $row[$fieldArr[$k]] = $v;
                }
            }
            if ($row) {
                $insert[] = $row;
            }
        }

image.png

最新回复 (0)
返回