下载
composer require phpoffice/phpexcel
一般导出都都是 数据库某张表 的记录
测试类
public function test() { $pro = new ProductModel(); $list = $pro->field('id,title,class_id,imgs_url,content,create_time')->selectOrFail(); $xlsName = "用户表"; // 文件名 $xlsCell = [ // 列名 ['id', '序号'], ['title', '标题'], ['class_id', '分类id'], ['imgs_url', '图片路径'], ['content', '富文本'], ['create_time', '添加时间'] ];// 表头信息 $this->downloadExcel($xlsName, $xlsCell, $list);// 传递参数 }
封装类, 建议对着一张空白Excel 来看
protected function downloadExcel($Title, $CellNameList, $TableData) { $xlsTitle = iconv('utf-8', 'gb2312', $Title); // excel标题 $fileName = $Title; // 文件名称 $cellNum = count($CellNameList); // 单元格名 个数 $dataNum = count($TableData); // 数据 条数 $obj = new PHPExcel(); $originCell = [ // 所有原生名 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ]; //getActiveSheet(0) 获取第一张表 $obj->getActiveSheet(0) ->mergeCells('A1:' . $originCell[$cellNum - 1] . '1'); //合并单元格A1-F1 变成新的A1 $obj->getActiveSheet(0)->setCellValue('A1', $fileName); // 设置第一张表中 A1的内容 for ($i = 0; $i < $cellNum; $i++) { // 设置第二行 ,值为字段名 $obj->getActiveSheet(0) ->setCellValue($originCell[$i] . '2', $CellNameList[$i][1]); //设置 A2-F2 的值 } // Miscellaneous glyphs, UTF-8 循环写入数据 for ($i = 0; $i < $dataNum; $i++) { for ($j = 0; $j < $cellNum; $j++) { // 设置第三行 ,每一行为 数据库一条数据 $obj->getActiveSheet(0) // 设 A3 值, 值为$TableData[0]['id'] ->setCellValue($originCell[$j] . ($i + 3), $TableData[$i][$CellNameList[$j][0]]); } } //居中 $obj->getActiveSheet(0)->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); ob_end_clean();//这一步非常关键,用来清除缓冲区防止导出的excel乱码 header('pragma:public'); header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xlsx"'); header("Content-Disposition:attachment;filename=$fileName.xlsx"); //"xls"参考下一条备注 $objWriter = \PHPExcel_IOFactory::createWriter($obj, 'Excel2007'); //"Excel2007"生成2007版本的xlsx,"Excel5"生成2003版本的xls 调用工厂类 return $objWriter->save('php://output'); }
PHP7.3会报continue错误 ,把对应文件的continue 改成 continue2 就好.
效果:
导入更加简单
1.文件上传
public function test() { header("content-type:text/html;charset=utf-8"); $file = request()->file('excel'); $info = $file->validate(['size' => 1048576, 'ext' => 'xls,xlsx'])->move('./excels'); //文件上传 if ($info) { //上传成功 $fileName = $info->getSaveName(); //文件名 $filePath = PUBLIC_PATH . '/excels/' . $fileName; //绝对路径 $suffix = $info->getExtension(); //后缀 //判断哪种类型 if ($suffix == "xlsx") $reader = PHPExcel_IOFactory::createReader('Excel2007'); else $reader = PHPExcel_IOFactory::createReader('Excel5'); //xls } else return $file->getError(); $excel = $reader->load($filePath, $encode = 'utf-8'); //载入excel文件 $this->dataImport($excel); //导入数据库 }
2.存入数据库
public function dataImport($excel) { //读取第一张表 $sheet = $excel->getSheet(0); //获取总行数 $row_num = $sheet->getHighestRow(); // 数据条数 //获取总列数 $col_num = $sheet->getHighestColumn(); // 字段个数 //1、excel 方式操作 $data = []; for ($i = 3; $i <= $row_num; $i++) { //i=3是为了 去除大标题和标题 $data[$i]['id'] = $sheet->getCell("A" . $i)->getValue(); $data[$i]['title'] = $sheet->getCell("B" . $i)->getValue(); $data[$i]['class_id'] = $sheet->getCell("C" . $i)->getValue(); $data[$i]['imgs_url'] = $sheet->getCell("D" . $i)->getValue(); $data[$i]['content'] = $sheet->getCell("E" . $i)->getValue(); $data[$i]['create_time'] = strtotime($sheet->getCell("F" . $i)->getValue()); //excel时间操作 //$time = date('Y-m-d H:i',\PHPExcel_Shared_Date::ExcelToPHP($sheet->getCell("B".$i)->getValue())); //$data[$i]['time'] = strtotime($time); } dump($data); //2、数组操作 $list = []; $arr = $sheet->toArray(); array_shift($arr); //去除大标题 array_shift($arr); //去除标题 foreach ($arr as $k => $v) { $list[$k]['id'] = $v[0]; $list[$k]['title'] = $v[1]; $list[$k]['class_id'] = $v[2]; $list[$k]['imgs_url'] = $v[3]; $list[$k]['content'] = $v[4]; $list[$k]['create_time'] = strtotime($v[5]); } dump($list); (new ProductModel)->insertAll($data); }
效果