<?php // +---------------------------------------------------------------------- author:wjf // +---------------------------------------------------------------------- declare (strict_types=1); namespace app\common\service; use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use PhpOffice\PhpSpreadsheet\IOFactory; use think\facade\Filesystem; use PhpOffice\PhpSpreadsheet\Spreadsheet; /** * 导入导出类 * Class Goods * @package app\store\service */ class Excel extends BaseService { /**导入 * @param string $filename * @return array|string|void */ public static function importExcel($filename = "") { $file[] = $filename; try { // 验证文件大小,名称等是否正确 validate(['file' => 'filesize:51200|fileExt:xls,xlsx']) ->check($file); // 将文件保存到本地(可在fileSystem配置文件中配置) $savename = Filesystem::disk('public')->putFile( '', $file[0]); // 截取后缀 $fileExtendName = substr(strrchr($savename, '.'), 1); // 有Xls和Xlsx格式两种 if ($fileExtendName == 'xlsx') { $objReader = IOFactory::createReader('Xlsx'); } else { $objReader = IOFactory::createReader('Xls'); } // 设置文件为只读 $objReader->setReadDataOnly(TRUE); // 读取文件,tp6默认上传的文件,在runtime的相应目录下,可根据实际情况自己更改 $objPHPExcel = $objReader->load(public_path() .'uploads/'. $savename); //excel中的第一张sheet $sheet = $objPHPExcel->getSheet(0); // 取得总行数 $highestRow = $sheet->getHighestRow(); // 取得总列数 $highestColumn = $sheet->getHighestColumn(); Coordinate::columnIndexFromString($highestColumn); $lines = $highestRow - 1; if ($lines <= 0) { echo('数据不能为空!'); exit(); } // 直接取出excle中的数据 $data = $objPHPExcel->getActiveSheet()->toArray(); // 删除第一个元素(表头) array_shift($data); // 返回结果 return $data; } catch (\Exception $e) { return $e->getMessage(); } } /**导出 * @param array $header * @param bool $type * @param array $data * @param string $fileName * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception */ public static function export(array $header = [], bool $type = true, array $data = [], string $fileName = "") { // 实例化类 $preadsheet = new Spreadsheet(); // 创建sheet $sheet = $preadsheet->getActiveSheet(); // 循环设置表头数据 foreach ($header as $k => $v) { $sheet->setCellValue($k, $v); } // 生成数据 $sheet->fromArray($data, null, "A2"); // 样式设置 $sheet->getDefaultColumnDimension()->setWidth(12); // 设置下载与后缀 if ($type) { header("Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); $type = "Xlsx"; $suffix = "xlsx"; } else { header("Content-Type:application/vnd.ms-excel"); $type = "Xls"; $suffix = "xls"; } // 激活浏览器窗口 header("Content-Disposition:attachment;filename=$fileName.$suffix"); //缓存控制 header("Cache-Control:max-age=0"); // 调用方法执行下载 $writer = IOFactory::createWriter($preadsheet, $type); // 数据流 $writer->save("php://output"); } }
控制器调用方法
/** * @return Json * @throws Exception */ public function import(): Json { $files = $this->request->file('file'); // 调用类库,读取excel中的内容 $rows = Excel::importExcel($files); if (count($rows) < 1) { return $this->renderError('无数据'); } return $this->renderSuccess([]); } public function export() { // 设置表格的表头数据 $header = ["A1" => "编号", "B1" => "姓名", "C1" => "年龄"]; // 假设下面这个数组从数据库查询出的二维数组 $data = [ [1,'欧阳',18], [2,'张三',19], [3,'李四',22], [4,'王五',19], [5,'赵六',29] ]; // 保存文件的类型 $type= true; // 设置下载文件保存的名称 $fileName = '信息'.time(); // 调用方法导出excel Excel::export($header,$type,$data,$fileName); }
标签:sheet,data,thinkphp6,return,header,导入,封装,array,type From: https://www.cnblogs.com/wujf-myblog/p/17072857.html