phpexcel 导入 时间格式 和 数值格式 处理
/** * 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();
相关推荐
winvon 2020-04-14
美谷niceValley 2019-11-19
wangjiao00 2019-07-25
nrdoo 2019-07-24
Elvis技术小屋 2019-07-24
swainyj 2019-09-02
matiantian 2014-10-30
抠脚强 2017-06-09
hlyrain 2019-06-28
十方地藏 2016-04-20
dreamon00 2019-06-28
wodeccu 2019-06-26
dreamon00 2019-06-21
hlyrain 2019-06-21
抠脚强 2019-06-21
wodeccu 2011-08-25
samtrue 2016-07-25
swainyj 2018-11-07