PHPExcel - creating multiple sheets by iteration

后端 未结 5 1540
无人共我
无人共我 2020-12-29 03:25

I\'m trying to create multiple sheets by iteration in phpexcel:

$i=0;

while ($i < 10) {

// Add new sheet
$objWorkSheet = $objPHPExcel->createSheet();         


        
5条回答
  •  南笙
    南笙 (楼主)
    2020-12-29 04:06

    In case you haven't come to a conclusion... I took Henrique's answer and gave a better logic solution. This is completely compatible with PHPSpreadSheet in case someone is using PHPSpreadSheet or PHPExcel.

    $spreadOrPhpExcel = new SpreadSheet(); // or new PHPExcel();
    print_in_sheet($spreadOrPhpExcel);
    
    function print_in_sheet($spread)
    {
        $sheet = 0;
    
        foreach( getData() as $report => $value ){
            # If number of sheet is 0 then no new worksheets are created
            if( $sheet > 0 ){
                $spread->createSheet();
            }
    
            # Index for the worksheet is setted and a title is assigned
            $wSheet = $spread->setActiveSheetIndex($sheet)->setTitle($report);
    
            # Printing data
            $wSheet->setCellValue("A1", "Hello World!");
    
            # Index number is incremented for the next worksheet
            $sheet++;
        }
    
        return $spread;
    }
    

提交回复
热议问题