至尊技术网 - PhpSpreadsheet https://www.zzwws.cn/tag/PhpSpreadsheet/ ThinkPHP6 excel表导入导出 https://www.zzwws.cn/archives/6389/ 2023-01-03T14:12:00+08:00 composer下载phpspreadsheetcomposer require phpoffice/phpspreadsheet PhpSpreadsheet中文简介phpexcel由于版本陈旧性能低下官方放弃维护,转而开发PhpSpreadsheet用了最新得psr标准因而对php版本不向下兼容需要注意!PhpSpreadsheet是一个用纯PHP编写的库,提供了一组类,使您可以读取和写入不同的电子表格文件格式PhpSpreadsheet提供了丰富的API接口,可以设置诸多单元格以及文档属性,包括样式、图片、日期、函数等等诸多应用,总之你想要什么样的Excel表格,PhpSpreadsheet都能做到使用PhpSpreadsheet开发的PHP要求7.1或更高版本,并且支持链式操作PhpSpreadsheet 支持的文件格式文件路径extend/Excel.php<?php use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Spreadsheet; use think\exception\ValidateException; class Excel { /** * 导入excel表格 * @param array $file 文件路径 * @return array|string */ protected function importExcel($file) { try { // 截取后缀 $fileExtendName = substr(strrchr($file, '.'), 1); // 有Xls和Xlsx格式两种 if ($fileExtendName == 'xlsx') { $objReader = IOFactory::createReader('Xlsx'); } else { $objReader = IOFactory::createReader('Xls'); } // 设置文件为只读 $objReader->setReadDataOnly(TRUE); // 读取文件,tp6默认上传的文件,在runtime的相应目录下,可根据实际情况自己更改 $objPHPExcel = $objReader->load($_SERVER['DOCUMENT_ROOT'] . $file); //excel中的第一张sheet $sheet = $objPHPExcel->getSheet(0); // 取得总行数 $highestRow = $sheet->getHighestRow(); // 取得总列数 $highestColumn = $sheet->getHighestColumn(); Coordinate::columnIndexFromString($highestColumn); $lines = $highestRow - 1; if ($lines <= 0) { return '数据不能为空'; } // 直接取出excle中的数据 $data = $objPHPExcel->getActiveSheet()->toArray(); // 删除第一个元素(表头) array_shift($data); // 返回结果 return $data; } catch (ValidateException $e) { return $e->getMessage(); } } /** * 导出excel表格 * @param array $header 设置表头数据 * @param array $data 生成的表格数据 * @param bool $type 文件类型,true为Xlsx,false为Xls,默认为true * @param string $fileName 文件名,默认为数据 */ protected function export($header = [], $data = [], $type = true, $fileName = "数据") { // 实例化类 $preadsheet = new Spreadsheet(); // 创建sheet $sheet = $preadsheet->getActiveSheet(); // 生成表头字母 $letter = []; $n = 0; for ($i = 'A'; $i <= 'Z'; $i++) { if ($n<count($header)){ $letter[] = $i; }else{ break; } $n++; } // 循环设置表头数据 foreach ($header as $k => $v) { // 解决长数字自动转科学计数法 if(is_numeric($v) && strlen($v) > 11){ $sheet->setCellValueExplicit($letter[$k].'1',$v,'s'); } $sheet->setCellValue($letter[$k].'1', $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"); } } 调用$excel = new Excel(); // 导出 $header = ['姓名','性别']; $data = [['小王','男'],['小李','男']]; $excel->export($header,$data); // 导入 $data = $excel->importExcel('/storage/picture/20221222/77d80064c35db092c8124a13a7f6fcd5.xlsx'); if(is_array($data)){ print_r($data); }else{ echo $data; }