tp6导入表格officeExcel数据,tp6导出Excel表格使用phpoffice

tp6导入数据,使用phpoffice

  1. 进入根目录composer安装phpoffice:composer require phpoffice/phpspreadsheet 安装comoser请点这里-> composer安装与使用

  2. 根据tp6开发手册,获取上传文件,验证文件大小、名称等,读取内容,存储数据,返回结果;基本的流程就是这样,详细的代码如下(如果你的代码是基于tp6我认为可以复制直接用,改一下return的内容就可以):

// 获取表单上传文件
        $file[] = request()->file('file');
        try {
            // 验证文件大小,名称等是否正确
            validate(['file' => 'filesize:51200|fileExt:xls,xlsx'])
                ->check($file);
            $savename =\think\facade\Filesystem::putFile( 'topic', $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(runtime_path().'uploads/'.$savename);
            $sheet = $objPHPExcel->getSheet(0);   //excel中的第一张sheet
            $highestRow = $sheet->getHighestRow();       // 取得总行数
            $highestColumn = $sheet->getHighestColumn();   // 取得总列数
            \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn);
            $lines = $highestRow - 1;
            if ($lines <= 0) {
                echo('数据不能为空!');
                exit();
            }
            //循环读取excel表格,整合成数组。如果是不指定key的二维,就用$data[i][j]表示。
            for ($j = 2; $j <= $highestRow; $j++) {
                $data[$j - 2] = [
                    'id' => trim($objPHPExcel->getActiveSheet()->getCell("A" . $j)->getValue()),
                    'title' => trim($objPHPExcel->getActiveSheet()->getCell("B" . $j)->getValue()),
                ];
            }
            // 实例化model
            $model = new DataModel();
            // 批量添加数据
            if ($model->saveAll($createData)) {
                return 200;
            }
            return 500;
        } catch (\think\exception\ValidateException $e) {
            return $e->getMessage();
        }

tp6导入数据,使用phpoffice/phpspreadsheet

2.具体方法内容如下,如果是tp6应该是可以直接复制粘贴,只把自己相应的参数改一下就可以了,具体代码如下


public function exportExcel()
    {
        // 查询要导出的数据
        $model = new ErrorModel();
        $data = $model
            ->field('id,api_member_id,title,Chinese,English,example,translate,desc')
            ->select();
        // 实例化
        $spreadsheet = new Spreadsheet();
        // 获取活动单元格
        $sheet = $spreadsheet->getActiveSheet();
 
        // 获取单元格
        $cellA = $sheet->getCell('A1');
        // 设置单元格的值
        $cellA->setValue('ID');
        // 设置 A 列 列宽
        $sheet->getColumnDimension('A')->setWidth(10);
        // 设置第一行 行高
        $sheet->getRowDimension(1)->setRowHeight(20);
 
        $cellB = $sheet->getCell('B1');
        $cellB->setValue('记录人');
        $sheet->getColumnDimension('B')->setWidth(20);
 
        $cellC = $sheet->getCell('C1');
        $cellC->setValue('题目名称');
        $sheet->getColumnDimension('C')->setWidth(10);
 
        $cellD = $sheet->getCell('D1');
        $cellD->setValue('中文');
        $sheet->getColumnDimension('D')->setWidth(20);
 
        $cellE = $sheet->getCell('E1');
        $cellE->setValue('英文');
        $sheet->getColumnDimension('E')->setWidth(40);
 
        $cellE = $sheet->getCell('F1');
        $cellE->setValue('其他');
        $sheet->getColumnDimension('F')->setWidth(40);
 
        $cellF = $sheet->getCell('G1');
        $cellF->setValue('例句');
        $sheet->getColumnDimension('G')->setWidth(30);
 
        $cellG = $sheet->getCell('H1');
        $cellG->setValue('例句翻译');
        $sheet->getColumnDimension('H')->setWidth(30);
 
        $cellH = $sheet->getCell('I1');
        $cellH->setValue('注释');
        $sheet->getColumnDimension('I')->setWidth(30);
 
 
        // 设置样式 标题
        $styleArray = [
            'alignment' => [
                'horizontal' => 'center', //水平居中
                'vertical' => 'center', //垂直居中
            ],
            'font' => [
                'name' => '黑体',
                'bold' => false,
                'size' => 10
            ]
        ];
        // 设置样式 正文
        $styleArrayBody = [
            'alignment' => [
                'horizontal' => 'center', //水平居中
                'vertical' => 'center', //垂直居中
            ],
            'font' => [
                'name' => '宋体',
                'bold' => false,
                'size' => 10
            ]
        ];
        // 应用样式
        $sheet->getStyle('A1')->applyFromArray($styleArray);
        $sheet->getStyle('B1')->applyFromArray($styleArray);
        $sheet->getStyle('C1')->applyFromArray($styleArray);
        $sheet->getStyle('D1')->applyFromArray($styleArray);
        $sheet->getStyle('E1')->applyFromArray($styleArray);
        $sheet->getStyle('F1')->applyFromArray($styleArray);
        $sheet->getStyle('G1')->applyFromArray($styleArray);
        $sheet->getStyle('H1')->applyFromArray($styleArray);
        $sheet->getStyle('I1')->applyFromArray($styleArray);
 
        // 从 A2 开始填充数据
        foreach ($data as $k => $v) {
            $n = $k + 2;
            // 获取单元格
            $cellA = $sheet->getCell('A' . $n);
            // 设置单元格的值
            $cellA->setValue($v['id']);
 
            $cellB = $sheet->getCell('B' . $n);
            $cellB->setValue('api_member_id');
 
            $cellC = $sheet->getCell('C' . $n);
            $cellC->setValue($v['title']);
 
            $cellD = $sheet->getCell('D' . $n);
            $cellD->setValue($v['Chinese']);
 
            $cellD = $sheet->getCell('E' . $n);
            $cellD->setValue($v['English']);
 
            $cellD = $sheet->getCell('F' . $n);
            $cellD->setValue($v['else']);
 
            $cellE = $sheet->getCell('G' . $n);
            $cellE->setValue($v['example']);
 
            $cellF = $sheet->getCell('H' . $n);
            $cellF->setValue($v['else']);
 
            $cellG = $sheet->getCell('I' . $n);
            $cellG->setValue($v['desc']);
        }
 
        $file_name = '导出数据.xlsx';
        // 实例化导出类
        header('Content-Type:application/vnd.ms-excel');
        header('Content-Disposition:attachment;filename=' . $file_name);
        header('Cache-Control:max-age=0');
 
        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
    }

你可能感兴趣的:(tp6导入表格officeExcel数据,tp6导出Excel表格使用phpoffice)