EasySwoole导入导出Execl表格
需求:easyswoole实现的服务端要实现execl表格的导入以及导出,从而实现人员以及成绩的变更
安装 execl 组件
这里有两种选择,一种就是经常使用的phpoffice/phpspreadsheet,而另一种则是xlswriter,本文章使用的则是phpoffice/phpspreadsheet
根据自己的php版本安装对应版本的 phpoffice/phpspreadsheet
基本使用
execl导入
public function upload(){
try {
$file = $this->request()->getUploadedFile('file'); $path = EASYSWOOLE_ROOT.'/Static/Uploads'; if(!is_dir($path)){
File::createDirectory($path); } $path = $path.'/'.$file->getClientFilename(); $file->moveTo($path); $spreadsheet = IOFactory::load($path); //读取默认工作表 $worksheet = $spreadsheet->getSheet(0); //取得一共有多少行 $allRow = $worksheet->getHighestRow(); $data = []; //清空用户表 UserModel::create()->destroy(null,true); for($i = 2; $i <= $allRow; $i++) {
$data['user_name'] = $spreadsheet->getActiveSheet()->getCell('B'.$i)->getValu