PhpSpreadsheet 导出excel 找不到setCellValueByColumnAndRow

php 复制代码
<?php
namespace app\admin\controller;
use think\facade\Db;
use think\facade\Request;
use app\admin\controller\BaseController;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class Product extends BaseController
{
    public function userorder_list_export(){
        
        $list = Db::name('userorder')->order('id desc')->select()->each(function($item, $key){
            
            $lists = explode(',',$item['product_list']);
            
            $plist = array();
            
            foreach ($lists as $ks => $v){
                
                $id = explode('_',$v)[0];
                
                $pinfo = Db::name('product')->where('id',$id)->find();
                $pinfo['cate_name'] = Db::name('product_cate')->where('id','=',$pinfo['cate_id'])->value('title');
                $pinfo['apps_name'] = Db::name('product_apps')->where('id','=',$pinfo['apps_id'])->value('title');
                $pinfo['diseases_name'] = Db::name('product_diseases')->where('id','in',$pinfo['diseases_id'])->select()->toArray();
                $pinfo['diseases_names'] = Db::name('product_diseasess')->where('id','=',$pinfo['diseases_ids'])->value('title');
                $pinfo['source_name'] = Db::name('product_source')->where('id','=',$pinfo['source'])->value('title');
                $pinfo['format_name'] = Db::name('product_format')->where('id','in',$pinfo['format'])->select()->toArray();
                
                $pinfo['add_num'] = intval(explode('_',$v)[1]);
                array_push($plist,$pinfo);
                
            }
            
            $item['product_lists'] = $plist;
            
            //$item['uinfo'] = Db::name('users')->where('id',$item['user_id'])->find();
            
            $item['create_time'] = date('m-d-Y',$item['create_time']);
            return $item;
        });
        
        $fileName = '会员订单 '.date("Y-m-d",time()) .'导出';
        //实例化spreadsheet对象
        $spreadsheet = new Spreadsheet();
    
        //获取活动工作簿
        $sheet = $spreadsheet->getActiveSheet();
        //设置单元格表头
        $sheet->setCellValue('A1', '序号');
        $sheet->setCellValue('B1', 'Order Number');
        $sheet->setCellValue('C1', 'First name');
        $sheet->setCellValue('D1', 'Last name');
        $sheet->setCellValue('E1', 'Email');
        $sheet->setCellValue('F1', 'Country');
        $sheet->setCellValue('G1', 'Organization Name');
        $sheet->setCellValue('H1', 'Street Address');
        $sheet->setCellValue('I1', 'City');
        $sheet->setCellValue('J1', 'State');
        $sheet->setCellValue('K1', 'Zip Code');
        $sheet->setCellValue('L1', 'Customer Number');
        $sheet->setCellValue('M1', 'mobile');
        $sheet->setCellValue('N1', 'Message');
        $sheet->setCellValue('O1', 'Product List(Product Name / TYPE / Catalog / Number)');
        $sheet->setCellValue('P1', 'Time');
        

        $i = 2;
        foreach($list as $key => $val){
            
            $sheet->setCellValueByColumnAndRow('1',$i,$i-1);
            $sheet->setCellValueByColumnAndRow('2',$i,$val['order_id']);
            $sheet->setCellValueByColumnAndRow('3',$i,$val['first_name']);
            $sheet->setCellValueByColumnAndRow('4',$i,$val['last_name']);
            $sheet->setCellValueByColumnAndRow('5',$i,$val['email']);
            $sheet->setCellValueByColumnAndRow('6',$i,$val['country']);
            $sheet->setCellValueByColumnAndRow('7',$i,$val['organization_name']);
            $sheet->setCellValueByColumnAndRow('8',$i,$val['street_address']);
            $sheet->setCellValueByColumnAndRow('9',$i,$val['city']);
            $sheet->setCellValueByColumnAndRow('10',$i,$val['state']);
            $sheet->setCellValueByColumnAndRow('11',$i,$val['zip_code']);
            $sheet->setCellValueByColumnAndRow('12',$i,$val['customer_number']);
            $sheet->setCellValueByColumnAndRow('13',$i,$val['mobile']);
            $sheet->setCellValueByColumnAndRow('14',$i,$val['message']);
            
            $text = '';
            
            foreach ($val['product_lists'] as $k => $v){
                
                $text .= $v['product_name'].' / ' .$v['cate_name'].' / '.$v['catalog'].' / '.$v['add_num']."\n";
                
            }
            $sheet->setCellValueByColumnAndRow('15',$i,trim($text,"\n"));
            
            $sheet->getStyle('P'.$i)->getAlignment()->setWrapText(true);
            
            $sheet->setCellValueByColumnAndRow('16',$i,$val['create_time']);
            
            $i++;
        
        }
        
        //设置自动列宽
        $spreadsheet->getActiveSheet()->getColumnDimension('A')->setWidth(5);
        $spreadsheet->getActiveSheet()->getColumnDimension('B')->setWidth(25);
        $spreadsheet->getActiveSheet()->getColumnDimension('C')->setWidth(10);
        $spreadsheet->getActiveSheet()->getColumnDimension('D')->setWidth(10);
        $spreadsheet->getActiveSheet()->getColumnDimension('E')->setWidth(25);
        $spreadsheet->getActiveSheet()->getColumnDimension('F')->setWidth(15);
        $spreadsheet->getActiveSheet()->getColumnDimension('G')->setWidth(25);
        $spreadsheet->getActiveSheet()->getColumnDimension('H')->setWidth(15);
        $spreadsheet->getActiveSheet()->getColumnDimension('I')->setWidth(15);
        $spreadsheet->getActiveSheet()->getColumnDimension('J')->setWidth(15);
        $spreadsheet->getActiveSheet()->getColumnDimension('K')->setWidth(15);
        $spreadsheet->getActiveSheet()->getColumnDimension('L')->setWidth(15);
        $spreadsheet->getActiveSheet()->getColumnDimension('M')->setWidth(15);
        $spreadsheet->getActiveSheet()->getColumnDimension('N')->setWidth(15);
        $spreadsheet->getActiveSheet()->getColumnDimension('O')->setWidth(50);
        $spreadsheet->getActiveSheet()->getColumnDimension('P')->setWidth(15);
        
        // //MIME协议,文件的类型,不设置描绘默认html
        // header('Content-Type:application/vnd.openxmlformats-officedoument.spreadsheetml.sheet');
        // //MIME 协议的扩展
        // header("Content-Disposition:attachment;filename={$fileName}.xlsx");
        // //缓存控制
        // header('Cache-Control:max-age=0');
        
        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet,'Xlsx');
        
        $writer->save('./uploads/'.$fileName.'.Xlsx');
        
        $url = Request::domain().'/uploads/'.$fileName.'.Xlsx';
        
        echo apireturn(200,200,'success',$url);
        die;
    }
?>

运行报错

Call to undefined method PhpOffice\PhpSpreadsheet\Worksheet\Worksheet::setCellValueByColumnAndRow

php 复制代码
    public function getCellByColumnAndRow($columnIndex, $row, $createIfNotExists = true){
        $columnLetter = Coordinate::stringFromColumnIndex($columnIndex);
        $coordinate = $columnLetter . $row;

        if ($this->cellCollection->has($coordinate)) {
            return $this->cellCollection->get($coordinate);
        }

        // Create new cell object, if required
        return $createIfNotExists ? $this->createNewCell($coordinate) : null;
    }
    
    /**
     * Set a cell value by using numeric cell coordinates.
     *
     * @param int $columnIndex Numeric column coordinate of the cell
     * @param int $row Numeric row coordinate of the cell
     * @param mixed $value Value of the cell
     *
     * @return Worksheet
     */
    public function setCellValueByColumnAndRow($columnIndex, $row, $value)
    {
        $this->getCellByColumnAndRow($columnIndex, $row)->setValue($value);

        return $this;
    }

尝试复制以上方法到Worksheet.php

问题解决

相关推荐
毕设源码-钟学长2 小时前
【开题答辩全过程】以 基于PHP的动漫社区的设计与实现为例,包含答辩的问题和答案
开发语言·php
qq_411262423 小时前
短时间串口发送网络端怎么接收不到
开发语言·php
wxin_VXbishe3 小时前
springboot旅游信息管理系统-计算机毕业设计源码21675
java·c++·spring boot·python·spring·django·php
小邓睡不饱耶3 小时前
深度实战:Spark GraphX构建用户信任网络,精准锁定高价值目标用户(含完整案例)
大数据·spark·php
杨超越luckly4 小时前
从传统 GIS 向智能/自动化脚本演进:地铁接驳公交识别的 ArcGIS 与 Python 双路径实践
开发语言·arcgis·php·交互·数据可视化
JoySSLLian5 小时前
OV SSL 证书:权威身份验证,让网络交互更可信
开发语言·网络·人工智能·网络协议·tcp/ip·php·ssl
JaguarJack5 小时前
Laravel AI SDK 正式发布
ai·php·laravel·服务端
汉堡包00117 小时前
【网安基础】--内网代理转发基本流程(正向与反向代理)
安全·web安全·php
梁辰兴19 小时前
计算机网络基础:TCP 的拥塞控制
tcp/ip·计算机网络·php·tcp·拥塞控制·计算机网络基础·梁辰兴
Vect__1 天前
TCP Socket编程详解
网络协议·tcp/ip·php