php 导出文件 PhpSpreadsheet

2023-12-20 16:56:06
    /**
     * 导出
     * @return void
     */
    public function detailExport()
    {

        $reinventory_id = $this->request->param('reinventory_id');
        if (!$reinventory_id) {
            return $this->ajaxReturn($this->errorCode, '参数异常', []);

        }

        $data = $this->model->with(['item'])
            ->where([
                'company_id' => $this->USER['company_id'],
                'reinventory_id' => $reinventory_id
            ])
            ->find();

        if (!$data) {
            return $this->ajaxReturn($this->errorCode, '数据不存在', []);
        }
        $data = $data->toArray();

        //导出库位到excel
        $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();

        $fieldList=[
            ['name'=>'产品名','field'=>'product_name'],
            ['name'=>'库位号','field'=>'kw_no'],
            ['name'=>'盘点数量','field'=>'qty'],
            ['name'=>'WMS原有库存','field'=>'wms_original_difference'],
            ['name'=>'WMS差异库存','field'=>'wms_difference'],
            ['name'=>'金蝶原有库存','field'=>'jd_original_difference'],
            ['name'=>'金蝶差异库存','field'=>'jd_difference'],
        ];

        $sheet->setCellValue(getTag(1).'1','行号');
        foreach($fieldList as $key=>$val){
            $sheet->setCellValue(getTag($key+2).'1',$val['name']);
        }
        //excel表主体内容
        foreach($data['item'] as $k=>$v){
            $sheet->setCellValueExplicit(getTag(1).($k+2),$k+1,\PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
            foreach($fieldList as $kk=>$vv){
                $sheet->setCellValueExplicit(getTag($kk+2).($k+2),$v[$vv['field']],\PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
            }
        }
        @mkdir(public_path().'/tmpxls/');
        $filename = 'tmpxls/reinventory'.date('YmdHis',time()).'.xlsx';;
        $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
        $writer->save(public_path().$filename);
        $res['path']=$filename;
        return $this->ajaxReturn($this->successCode,'返回成功',$res);



    }
    ```
    ![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/85c477f9321c4c3894c2c6841504597e.png)

文章来源:https://blog.csdn.net/qq_19598963/article/details/135110686
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。