抠脚强 2020-04-14
/** * Notes:导入excel文件 * User: hly * Date: 2020/4/14 17:56 */ function importXlsx() { //引入phpexcel require getcwd() . ‘/PHPExcel.php‘; $file_name = ‘test.xlsx‘; //模拟上传后的文件 $objReader = \PHPExcel_IOFactory::createReader(‘Excel2007‘); $objPHPExcel = $objReader->load($file_name, $encode = ‘utf-8‘); $sheet = $objPHPExcel->getSheet(0); $highestRow = $sheet->getHighestRow(); // 取得总行数 $data = array(); for ($i = 2; $i <= $highestRow; $i++) { $data[$i - 2][‘title‘] = getCellValue($objPHPExcel->getActiveSheet()->getCell("A" . $i)); $data[$i - 2][‘date_time‘] = getCellValue($objPHPExcel->getActiveSheet()->getCell("B" . $i), ‘Y-m-d H:i:s‘); } exit(json_encode($data)); } function getCellValue($cell, $date_format = "Y-m-d 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(‘/^([$[A-Z]*-[0-9A-F]*])*[hmsdy]/i‘, $format_code)) { //判断是否为日期类型 $value = gmdate($date_format, \PHPExcel_Shared_Date::ExcelToPHP($value)); //格式化日期 }else{ $value = \PHPExcel_Style_NumberFormat::toFormattedString($value, $format_code); //格式化数字 } } return $value; } importXlsx();