<? php /** * Created by PhpStorm. * User: Dai * Date: 2018-03-26 * Time: 16:11 */ /** * 批量导出数据 * @param $arr 从数据库查询出来,即要导出的数据 * $name excel表歌名 */ function expExcel($arr, $name) { require_once 'PHPExcel.php'; //实例化 $objPHPExcel = new PHPExcel(); /*右键属性所显示的信息*/ $objPHPExcel->getProperties()->setCreator("Dai")//作者 ->setLastModifiedBy("Dai")//最后一次保存者 ->setTitle('数据EXCEL导出')//标题 ->setSubject('数据EXCEL导出')//主题 ->setDescription('导出数据')//描述 ->setKeywords("excel")//标记 ->setCategory("result file"); //类别 //设置当前的表格 $objPHPExcel->setActiveSheetIndex(0); // 设置表格第一行显示内容 $objPHPExcel->getActiveSheet() ->setCellValue('A1', '序号') ->setCellValue('B1', '姓名') ->setCellValue('C1', '日期') ->setCellValue('D1', '上班时间') ->setCellValue('E1', '下班时间') ->setCellValue('F1', '迟到') ->setCellValue('G1', '早退') ->setCellValue('H1', '加班时间') ->setCellValue('I1', '总加班时间') ->setCellValue('J1', '总迟到时间') ->setCellValue('K1', '总早退时间') //设置第一行为红色字体 ->getStyle('A1:k1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED); $key = 1; $start = 1; $end = 1; /*以下就是对处理Excel里的数据,横着取数据*/ foreach ($arr as $v) { //设置循环从第二行开始 $key++; $objPHPExcel->getActiveSheet() //Excel的第A列,name是你查出数组的键值字段,下面以此类推 ->setCellValue('A' . $key, $key - 1) ->setCellValue('B' . $key, $v['name']) ->setCellValue('C' . $key, $v['date']) ->setCellValue('D' . $key, $v['shang']) ->setCellValue('E' . $key, $v['xia']) ->setCellValue('F' . $key, $v['late']) ->setCellValue('G' . $key, $v['early']) ->setCellValue('H' . $key, $v['add']) ->setCellValue('I' . $key, $v['totalAdd']) ->setCellValue('J' . $key, $v['totalLate']) ->setCellValue('K' . $key, $v['totalEarly']); if ($v['isAddDate']) { $objPHPExcel->getActiveSheet() ->getStyle('A' . $key . ':H' . $key . '') ->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID); $objPHPExcel->getActiveSheet() ->getStyle('A' . $key . ':H' . $key . '') ->getFill()->getStartColor()->setARGB('FF808080'); } if ($v['late']) { $objPHPExcel->getActiveSheet() ->getStyle('A' . $key . ':H' . $key . '') ->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID); $objPHPExcel->getActiveSheet() ->getStyle('A' . $key . ':H' . $key . '') ->getFill()->getStartColor()->setARGB('5FB000'); } if ($v['early']) { $objPHPExcel->getActiveSheet() ->getStyle('A' . $key . ':H' . $key . '') ->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID); $objPHPExcel->getActiveSheet() ->getStyle('A' . $key . ':H' . $key . '') ->getFill()->getStartColor()->setARGB('0017C405'); } if (!empty($v['totalAdd'])) { //将单元格进行合并 $objPHPExcel->getActiveSheet()->mergeCells('I' . $start . ':I' . $end . ''); $objPHPExcel->getActiveSheet()->mergeCells('J' . $start . ':J' . $end . ''); $objPHPExcel->getActiveSheet()->mergeCells('K' . $start . ':K' . $end . ''); //将合并的单元格内容垂直方式设置为垂直居中 $objPHPExcel->getActiveSheet()->getStyle('I' . $start . ':K' . $end . '')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); $end += 1; $start = $end; } else { $end += 1; } } //设置当前的表格 $objPHPExcel->setActiveSheetIndex(0); header('Content-Type: application/vnd.ms-excel'); //文件类型 header('Content-Disposition: attachment;filename="' . $name . '.xls"'); //文件名 header('Cache-Control: max-age=0'); header('Content-Type: text/html; charset=utf-8'); //编码 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //excel 2003 $objWriter->save('php://output'); exit; } /***********调用**********************/ header("Content-type:text/html;charset=utf-8"); //连接数据库 try { $dsn = 'mysql:host=localhost;dbname=kaoqing'; $username = 'root'; $passwd = 'root'; $pdo = new PDO($dsn, $username, $passwd); $pdo->query("SET NAMES utf8"); } catch (PDOException $e) { echo $e->getMessage(); } //先获取数据 $sql = "select * from time2"; //先查询一下phone表当中的数据 $res = $pdo->prepare($sql); $s = $res->execute(); if (false == $s) { echo "查询失败"; exit; } $a = $res->fetchAll(PDO::FETCH_ASSOC); //excel表格名 $name = date('Y-m-d', time()) . "打卡表"; //调用 expExcel($a, $name);