💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## Spreadsheet 支持excel 函数 公式使用 ``` <?php namespace app # 给类文件的命名空间起个别名 use PhpOffice\PhpSpreadsheet\Spreadsheet; # Xlsx类 将电子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; # 实例化 Spreadsheet 对象 $spreadsheet = new Spreadsheet(); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('A1','10'); $sheet->setCellValue('B1','15'); $sheet->setCellValue('C1','20'); $sheet->setCellValue('D1','25'); $sheet->setCellValue('E1','30'); $sheet->setCellValue('G1','35'); $sheet->setCellValue('A2', '总数:'); $sheet->setCellValue('B2', '=SUM(A1:G1)'); $sheet->setCellValue('A3', '平均数:'); $sheet->setCellValue('B3', '=AVERAGE(A1:G1)'); $sheet->setCellValue('A4', '最小数:'); $sheet->setCellValue('B4', '=MIN(A1:G1)'); $sheet->setCellValue('A5', '最大数:'); $sheet->setCellValue('B5', '=MAX(A1:G1)'); $sheet->setCellValue('A6', '最大数:'); $sheet->setCellValue('B6', '\=MAX(A1:G1)'); // 使用转义字符 // 批量赋值 $sheet->setCellValue('A1','ID'); $sheet->setCellValue('B1','姓名'); $sheet->setCellValue('C1','年龄'); $sheet->setCellValue('D1','身高'); $sheet->fromArray( [ [1,'欧阳克','18岁','188cm'], [2,'黄蓉','17岁','165cm'], [3,'郭靖','21岁','180cm'] ], 3, 'A2' ); // 合并单元格 合并后,赋值只能给A1,开始的坐标。 $sheet->mergeCells('A1:B5'); $sheet->getCell('A1')->setValue('欧阳克'); # Xlsx类 将电子表格保存到文件 $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx'); // 客户端文件下载 header('Content-Type:application/vnd.ms-excel'); header('Content-Disposition:attachment;filename=1.xls'); header('Cache-Control:max-age=0'); $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls'); $writer->save('php://output'); ``` ### 读取表格文件 ``` <?php namespace app; # 创建读操作 $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx'); # 打开文件、载入excel表格 $spreadsheet = $reader->load('1.xlsx'); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); # 获取 单元格值 和 坐标 $cellC1 = $sheet->getCell('B2'); echo '值: ', $cellC1->getValue(),PHP_EOL; echo '坐标: ', $cellC1->getCoordinate(),PHP_EOL; $sheet->setCellValue('B2','欧阳锋'); # 获取 单元格值 和 坐标 $cellC2 = $sheet->getCell('B2'); echo '值: ', $cellC2->getValue(),PHP_EOL; echo '坐标: ', $cellC2->getCoordinate(); ``` #### 导入功能 ``` <?php $file = $_FILES['file']['tmp_name']; # 载入composer自动加载文件 require 'vendor/autoload.php'; # 载入方法库 require 'function.php'; # 创建读操作 $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx'); # 打开文件、载入excel表格 $spreadsheet = $reader->load($file); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); # 获取总列数 $highestColumn = $sheet->getHighestColumn(); # 获取总行数 $highestRow = $sheet->getHighestRow(); # 列数 改为数字显示 $highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn); $log = []; for($a=2;$a<$highestRow;$a++){ $title = $sheet->getCellByColumnAndRow(1,$a)->getValue(); $cat_fname = $sheet->getCellByColumnAndRow(2,$a)->getValue(); $cat_name = $sheet->getCellByColumnAndRow(3,$a)->getValue(); $price = $sheet->getCellByColumnAndRow(4,$a)->getValue(); $img = $sheet->getCellByColumnAndRow(5,$a)->getValue(); $cat_fid = find('shop_cat','id','name="'.$cat_fname.'"'); $cat_id = find('shop_cat','id','name="'.$cat_name.'"'); $data = [ 'title' => $title, 'cat_fid' => $cat_fid['id'], 'cat_id' => $cat_id['id'], 'price' => $price, 'img' => $img, 'add_time' => time(), ]; $ins = insert('shop_list',$data); if($ins){ $log[] = '第'.$a.'条,插入成功'; }else{ $log[] = '第'.$a.'条,插入失败'; } } echo json_encode(['code'=>0,'msg'=>'成功','data'=>$log]); ```