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

问题解决

相关推荐
ServBay4 小时前
一个下午,一台电脑,终结你 90% 的 Symfony 重复劳动
后端·php·symfony
kali-Myon10 小时前
2025春秋杯网络安全联赛冬季赛-day2
python·安全·web安全·ai·php·pwn·ctf
IOsetting11 小时前
金山云主机添加开机路由
运维·服务器·开发语言·网络·php
kali-Myon11 小时前
2025春秋杯网络安全联赛冬季赛-day1
java·sql·安全·web安全·ai·php·web
kali-Myon11 小时前
2025春秋杯网络安全联赛冬季赛-day3
python·安全·web安全·ai·php·web·ctf
xqqxqxxq12 小时前
Java IO 核心:BufferedReader/BufferedWriter & PrintStream/PrintWriter 技术笔记
java·笔记·php
静听山水14 小时前
Redis的Pipeline (管道)
数据库·redis·php
hzb6666614 小时前
unictf2026
开发语言·javascript·安全·web安全·php
JaguarJack14 小时前
PHP 应用遭遇 DDoS 攻击时会发生什么 从入门到进阶的防护指南
后端·php·服务端
YUJIANYUE1 天前
PHP纹路验证码
开发语言·php