作者:mobiledu2502918997 | 来源:互联网 | 2023-09-17 14:58
PHP导入Excel考勤表安装PHPExcel引入IOFactory.php考勤表如下上传,接收,移动Excel文件加载文件获取Excel中想要的数据然后把得到的数据放入数据库中进
PHP导入Excel考勤表
- 安装PHPExcel
- 引入IOFactory.php
- 考勤表如下
- 上传,接收,移动 Excel文件
- 加载文件
- 获取Excel中想要的数据
安装PHPExcel
使用composer安装:
composer require phpoffice/phpexcel
引入IOFactory.php
require_once(ROOT_PATH . 'vendor/phpoffice/phpexcel/Classes/PHPExcel/IOFactory.php');
考勤表如下
Excel考勤表:
上传,接收,移动 Excel文件
<div class="layui-form-item" style="width: 99%"><form action="{:url('index/Attesgerbdfb/attendancsdfsscel_in')}" enctype="multipart/form-data" method="post"><input type="file" name="excel" /><span>覆盖 </span><input type="checkbox" value="1" name="yes" id="yes" checked><span> 不覆盖 </span><input type="checkbox" value="2" name="no" id="no"><input type="submit" value="上传" /></form></div>
$file = request()->file('excel');
ini_set('max_execution_time', '0');
$info = $file->move(ROOT_PATH . 'public' . DS . 'upload'.DS.'excel');
$path = $info->getSaveName();
$extension = $info->getExtension();
if( $extension =='xlsx' )
{
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
}else{
$objReader = PHPExcel_IOFactory::createReader('Excel5');
}
加载文件
$filename = ROOT_PATH . 'public' . DS . 'upload'.DS.'excel'.DS.$path;$objReader ->setReadDataOnly(true); $objPHPExcel = $objReader->load($filename);$sheetCount = $objPHPExcel->getSheetCount();
获取Excel中想要的数据
$one = array('A','B','D','G','I','K','M');
$two = array('P','Q','S','V','X','Z','AB');
$three = array('AE','AF','AH','AK','AM','AO','AQ');
$arr = array();
$brr = array();
$crr = array();
for($i=2;$i<$sheetCount;$i++){
$sheet = $objPHPExcel->getSheet($i);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
$arr[$i]['depart'] = $sheet->getCell("B4")->getValue();
$arr[$i]['username'] = $sheet->getCell("J4")->getValue();
$arr[$i]['date'] = $sheet->getCell("B5")->getValue();
$brr[$i]['depart'] = $sheet->getCell("Q4")->getValue();
$brr[$i]['username'] = $sheet->getCell("Y4")->getValue();
$brr[$i]['date'] = $sheet->getCell("Q5")->getValue();
$crr[$i]['depart'] = $sheet->getCell("AF4")->getValue();
$crr[$i]['username'] = $sheet->getCell("AN4")->getValue();
$crr[$i]['date'] = $sheet->getCell("AF5")->getValue();$drr = array();
$err = array();
$frr = array();
for($j=13;$j<=$highestRow;$j++)
{
foreach ($one as $k_one=>$v_one){
$d = $sheet->getCell("A".$j)->getValue();if(empty($d)){break;}
$x = $sheet->getCell($v_one.$j)->getValue();
if(is_float($x)){
$drr[$j][$k_one] = gmdate('H:i',\PHPExcel_Shared_Date::ExcelToPHP($x));}else{
$drr[$j][$k_one] = $sheet->getCell($v_one.$j)->getValue();}}
foreach ($two as $k_two=>$v_two){$e = $sheet->getCell("P".$j)->getValue();if(empty($e)){break;}$y = $sheet->getCell($v_two.$j)->getValue();if(is_float($y)){
$err[$j][$k_two] = gmdate('H:i',\PHPExcel_Shared_Date::ExcelToPHP($y));}else{$err[$j][$k_two] = $sheet->getCell($v_two.$j)->getValue();}}foreach ($three as $k_three=>$v_three){
$f = $sheet->getCell("AE".$j)->getValue();if(empty($f)){break;}
$z = $sheet->getCell($v_three.$j)->getValue();
if(is_float($z)){
$frr[$j][$k_three] = gmdate('H:i',\PHPExcel_Shared_Date::ExcelToPHP($z));}else{
$frr[$j][$k_three] = $sheet->getCell($v_three.$j)->getValue();}}}$arr[$i]['attend'] = $drr;$brr[$i]['attend'] = $err;$crr[$i]['attend'] = $frr;}
$zrr = array_merge($arr,$brr,$crr);
然后把得到的数据放入数据库中进行后续操作;