在 php 中使用 php 电子表格写入多表 excel

Write to multisheet excel using phpspreadsheet in php

我设法使用 phpspreadsheet 写入 excel 文件...但仅针对单个 sheet。我尝试创建 multisheet 但失败了。感谢您的帮助。 以下是我的代码..提前致谢。

public function exportAll()(
{
    $e = 2; // Row start at 2
    $spreadsheet = new Spreadsheet();
    $sheet = $spreadsheet->getActiveSheet();

    $data1 = $this->model_general->get_data1();
    $sheet->setActiveSheetIndex(0);

    //Excel Heading
    $sheet->setCellValue('A1', 'Date');
    $sheet->setCellValue('B1', 'Details');
    // ... set heading ...

    // Excel Data
    if(!empty($data1)):
    foreach ($data1 as row)
    {
        $sheet->setCellValue('A'.$e, $row->date);
        $sheet->setCellValue('B'.$e, $row->details);
        // ... set data ...
        $e++;
    }
    endif;

    // Assign Excel Name
    $sheet->setTitle('Data1');

    $sheet->createSheet();

    $data2 = $this->model_general->get_data2();
    $sheet->setActiveSheetIndex(1);

    //Excel Heading
    $sheet->setCellValue('A1', 'Date');
    $sheet->setCellValue('B1', 'Details');
    // ... set heading ...

    // Excel Data
    if(!empty($data2)):
    foreach ($data2 as row)
    {
        $sheet->setCellValue('A'.$e, $row->date);
        $sheet->setCellValue('B'.$e, $row->details);
        // ... set data ...
        $e++;
    }
    endif;

    // Assign Excel Name
    $sheet->setTitle('Data2');

    $sheet->createSheet();

    $data3 = $this->model_general->get_data3();
    $sheet->setActiveSheetIndex(2);

    //Excel Heading
    $sheet->setCellValue('A1', 'Date');
    $sheet->setCellValue('B1', 'Details');
    // ... set heading ...

    // Excel Data
    if(!empty($data3)):
    foreach ($data3 as row)
    {
        $sheet->setCellValue('A'.$e, $row->date);
        $sheet->setCellValue('B'.$e, $row->details);
        // ... set data ...
        $e++;
    }
    endif;

    // Assign Excel Name
    $sheet->setTitle('Data3');

    // Set first sheet as active sheet
    $sheet->setActiveSheetIndex(0);

    $writer = new Xlsx($spreadsheet);
    $filename = 'All Data';
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="'. $filename .'.xlsx"');
    header('Cache-Control: max-age=0');
    $writer->save('php://output'); // download file

}

现在..此代码在 $sheet->setActiveSheetIndex(0);

处出错
Call to undefined method PhpOffice\PhpSpreadsheet\Worksheet\Worksheet::setActiveSheetIndex()

让我知道哪里出错了..我好像找不到-,-

$worksheet1 = $spreadsheet->createSheet();
$worksheet1->setTitle('Another sheet');

https://phpspreadsheet.readthedocs.io/en/latest/topics/recipes/#creating-worksheets-in-a-workbook

此代码能够创建多个 sheet、添加内容并保存该文件。试试这个。

<?php
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet; 
use PhpOffice\PhpSpreadsheet\Writer\Xls; 

$spreadsheet = new Spreadsheet(); 

$sheet = $spreadsheet->getActiveSheet();  
$sheet->setCellValue('A1', 'Sheet1');  

$sheet_2=$spreadsheet->createSheet();
$sheet_2->setCellValue('B1', 'Sheet2'); 

$sheet_3=$spreadsheet->createSheet();
$sheet_3->setCellValue('B5', 'Sheet3'); 

$writer = new Xls($spreadsheet);  

$writer->save('gfg1.xls'); 
?>

在您的代码中,您启动 Spreadsheet() 并从 $spreadsheet.This 创建 sheet 1 是正确的。

 $spreadsheet = new Spreadsheet();
 $sheet = $spreadsheet->getActiveSheet();

要创建下一个sheet,您需要再次使用$spreadsheet,

 $nextsheet=$spreadsheet->createSheet();

但你使用

 $sheet->createSheet();