在php中使用phpspreadsheet写入多表Excel

时间:2019-02-21 07:07:21

标签: excel phpspreadsheet

我设法使用phpspreadsheet写入excel文件...但仅适用于单张纸。我尝试创建多张纸,但是失败了。感谢您的帮助。 下面是我的代码。。谢谢。

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()

让我知道哪里出了问题。.我似乎找不到它-,-

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

此代码能够创建多个工作表,添加内容并保存该文件。试试看。

<?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创建工作表1。

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

要创建下一张表,您需要再次使用$ spreadsheet,

 $nextsheet=$spreadsheet->createSheet();

但是您使用

 $sheet->createSheet();