Home > Article > Backend Development > PHP processes excel file data
The content of this article is about PHP processing excel file data. It has certain reference value. Now I share it with you. Friends in need can refer to it.
The article mainly records how to use the PHPexcel plug-in to process excel. The file processing process is only used as a reminder. If you encounter problems downloading the plug-in, you can leave a message. Thank you for browsing. The following is the note content:
require_once('./PhpSpreadsheet/vendor/autoload.php');
//创建文件读取类对象 $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx"); /** Advise the Reader that we only want to load cell data **/ //设置为只读模式$reader->setReadDataOnly(true); /** Load $inputFileName to a Spreadsheet Object **/ //加载文件路径,路径为绝对路径 $spreadsheet = $reader->load($file_path); //获取excel表中有多少个sheet $sheetNum = $spreadsheet->getSheetCount(); $sheetData = []; $temp = []; //只获取活跃的sheet //$temp = $spreadsheet->getSheetActive()->toArray(null, true, true, true); //遍历每一个sheet,获取里面的数据内容 for($i = 0; $i < $sheetNum; $i++){ $temp = $spreadsheet->getSheet($i)->toArray(null, true, true, true); foreach ($temp as $v) { if(is_float($v['A'])){ $sheetData[] = $v; } } }
The output format content is as follows:
var_dump($sheetData); /* 'A' => float 41746 'B' => string '玉雪婵娟' (length=12) 'C' => float 236979210 'D' => string '353343073072008' (length=15) 'E' => float 41740 'F' => null*/
//获取excel表中的数据记录 $excel_records$temp = []; $excel_records = []; foreach ($sheetData as $v){ //strtotime(gmdate("Y-m-d", ($sheetData[$i]['F'] -25569) * 86400)) //将表格中的时间字符串转为时间格式 $temp['date'] = gmdate("Y-m-d", ($v['A'] -25569) * 86400);//2018年获取为2014年,尚不明原因 $temp['drname'] = $v['B']; $temp['uid'] = $v['C']; $temp['imei'] = $v['D']; $temp['reg_date'] = $v['E']; $excel_records[] = $temp; }
You can directly process the data later.
Related recommendations:
php processing array key value changes
php method of processing form upload files
The above is the detailed content of PHP processes excel file data. For more information, please follow other related articles on the PHP Chinese website!