Write to multisheet excel using phpspreadsheet in php

旧街凉风 提交于 2019-12-25 00:22:31

问题


I manage to write into excel file using phpspreadsheet... but for single sheet only. I try to create multisheet but failed.. appreciate your help to assist. Below are my codes..Thanks in advance.

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

}

Right now.. this code gives error at $sheet->setActiveSheetIndex(0);

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

Let me know where it goes wrong.. i seems cant find it -,-


回答1:


This code able to create multiple sheet, add content, and save that file. Try this out.

<?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'); 
?>

In your code , You initiate Spreadsheet() and create sheet 1 from $spreadsheet.This is correct.

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

To create next sheet, you need to use $spreadsheet again,

 $nextsheet=$spreadsheet->createSheet();

but you use

 $sheet->createSheet();



回答2:


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

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



来源:https://stackoverflow.com/questions/54801141/write-to-multisheet-excel-using-phpspreadsheet-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!