PhpSpreadsheet:尽管在 setCellValueByColumnAndRow 中使用整数列索引,但仍出现“无效单元格坐标”错误

问题描述 投票:0回答:1

我遇到了 PhpSpreadsheet 的问题,在尝试从 Laravel 项目导出 Excel 报告时收到“无效单元格坐标”错误,

特别是像“01”这样的坐标。我检查了我的代码并确保我在 setCellValueByColumnAndRow 中使用整数列索引:

这是我的控制器方法的完整代码:

    public function getFestivalPlanRegister($id, $author = null)
        {
            if (request()->has('excel_export')){
                $festival = $festival->where('festival_plan.fsp_condition_plan', '=', 'completed');
            }
            
            if (request()->has('excel_export')) {
                $spreadsheet = new Spreadsheet();
                $spreadsheet->setActiveSheetIndex(0)->setRightToLeft(true);
                $activeSheet = $spreadsheet->getActiveSheet();
    
                $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
                $active_sheet = $spreadsheet->getActiveSheet();
                $active_sheet->setRightToLeft(true);
    
                $style = [
                    'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER],
                ];
    
                // Define the header row and style
                $headerColumns = [
                   'Registration ID', 'Project Tracking Code', 'Farsi Title of the Project', 
                   'English Title of the Project', 'Section',
                   'Axis', 'Challenge', 'Lead Author Name', 'Research Location', 'Lead Author 
                    Contact Number',
                    'Lead Author Email', 'Lead Author Address', 'Lead Author Postal Code', 'Lead 
                     Author Province',
                     'Lead Author City', 'Lead Author Academic Level', 'Lead Author Educational 
                     Level',
                     'Project Status', 'Collaborator Name', 'Collaborator National ID', 
                     'Collaborator Academic Level',
                     'Collaborator Educational Level', 'Collaborator Province', 'Collaborator 
                      City', 'Collaborator Educational Institution',
                     'Supervisor Name', 'Supervisor Contact Number', 'Supervisor Email', 
                     'Supervisor National ID',
                     'Supervisor Address', 'Supervisor Postal Code', 'Supervisor Academic Level', 
                     'Supervisor Field of Study',
                     'Supervisor Province', 'Supervisor City', 'First Stage Average Score', 
                     'Second Stage Average Score',
                     'Third Stage Average Score', 'Status'
                ];
    
                $style = [
                    'alignment' => ['horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER],
                ];
    
                $row = 1;
                // Apply header row
                foreach ($headerColumns as $column => $header) {
                    $activeSheet->setCellValue("{$column}{$row}", $header);
                    $activeSheet->getStyle("{$column}{$row}")->applyFromArray($style);
                }
    
                $row = 2;
    
                foreach ($festival as $item) {
                    $activeSheet->setCellValueByColumnAndRow(1, $row, $item->fsp_code);
                    $activeSheet->getStyleByColumnAndRow(1, $row)->applyFromArray($style);
    
                    $activeSheet->setCellValueByColumnAndRow(2, $row, $item->fsp_id);
                    $activeSheet->getStyleByColumnAndRow(2, $row)->applyFromArray($style);

                    ...
                    
                    $row++;
                }
                
                $filename = 'festival_report.xlsx';
                header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
                header("Content-Disposition: attachment;filename=\"$filename\"");
                header('Cache-Control: max-age=0');
    
                $writer = new Xlsx($spreadsheet);
                $writer->save('php://output');
                die();
            }
            return $festival;
        }

这是我得到的错误:

单元格坐标无效 01

截图:

所以请帮我解决这个问题...我真的陷入了这一点!


我仍在努力解决这个错误,我在这里编写了 Spreadsheet.php 类方法:

$spreadsheet = new Spreadsheet();
$spreadsheet->setActiveSheetIndex(0)->setRightToLeft(true);

$activeSheet = $spreadsheet->getActiveSheet();
            
public function getActiveSheet()
    {
        return $this->getSheet($this->activeSheetIndex);
    }

public function getSheet($pIndex)
    {
        if (!isset($this->workSheetCollection[$pIndex])) {
            $numSheets = $this->getSheetCount();

            throw new Exception(
                "Your requested sheet index: {$pIndex} is out of bounds. The actual number of sheets is {$numSheets}."
            );
        }

        return $this->workSheetCollection[$pIndex];
    }
    
public function getSheetCount()
    {
        return count($this->workSheetCollection);
    }
php laravel laravel-5.8 maatwebsite-excel laravel-excel
1个回答
0
投票

问题似乎出在你的

foreach
循环中,我使用了
\PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex()
方法将数字列索引转换为其相应的Excel列字母,试试这个

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;

public function getFestivalPlanRegister($id, $author = null) {
    if (request()->has('excel_export')) {
        $festival = $festival->where('festival_plan.fsp_condition_plan', '=', 'completed');
    }

    if (request()->has('excel_export')) {
        $spreadsheet = new Spreadsheet();
        $spreadsheet->setActiveSheetIndex(0)->setRightToLeft(true);
        $activeSheet = $spreadsheet->getActiveSheet();

        $style = [
            'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER],
        ];

        // Define the header row and style
        $headerColumns = [
            // ... your header columns here ...
        ];

        $row = 1;
        // Apply header row
        foreach ($headerColumns as $columnIndex => $header) {
            $columnLetter = Coordinate::stringFromColumnIndex($columnIndex + 1);
            $activeSheet->setCellValue("{$columnLetter}{$row}", $header);
            $activeSheet->getStyle("{$columnLetter}{$row}")->applyFromArray($style);
        }

        $row = 2;

        foreach ($festival as $item) {
            $columnLetter = Coordinate::stringFromColumnIndex(1);
            $activeSheet->setCellValue("{$columnLetter}{$row}", $item->fsp_code);
            $activeSheet->getStyle("{$columnLetter}{$row}")->applyFromArray($style);

            // ... continue with other columns ...

            $row++;
        }

        $filename = 'festival_report.xlsx';
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header("Content-Disposition: attachment;filename=\"$filename\"");
        header('Cache-Control: max-age=0');

        $writer = new Xlsx($spreadsheet);
        $writer->save('php://output');
        exit;
    }

    return $festival;
}
© www.soinside.com 2019 - 2024. All rights reserved.