How can I join Excel documents using PHPExcel?

后端 未结 1 549
甜味超标
甜味超标 2020-12-29 12:04

I\'m using PHPExcel to dynamically generate order receipts.

I\'d like to be able to generate a \"summary\" Excel file, containing all the order receipts (one per wor

1条回答
  •  Happy的楠姐
    2020-12-29 12:37

    In Excel, "tabs" are known as "worksheets". You can create an Excel workbook with multiple worksheets in PHPExcel.

    For reference, another answer on SO has an easy-to-follow guide on how to add additional Worksheets to an existing workbook.

    This post on Codeplex has an example of adding an external sheet. I'm not sure all of the renaming dance is needed though. (The Codeplex link encourages you to clone the worksheet and copy the cloned sheet so as not to remove it from the original workbook, but I don't think that would be an issue unless you're writing output to the source workbook.) I think something like this should work:

    function getReceiptWorksheet( $receiptNumber ) {
        // Do something here to retrieve & return your existing receipt
    }
    
    function createMasterWorkbook( $filename, $receiptNumbers ) {
        $workbook= new PHPExcel();
    
        foreach( $receiptNumbers as $receiptNumber ){
             $worksheet = getReceiptWorksheet( $receiptNumber )
             $workbook->addExternalSheet( $worksheet );
        }
    
        $objWriter = new PHPExcel_Writer_Excel2007($workbook);
        $objWriter->save($filename);
    }
    

    Then you could just call createMasterWorkbook( 'receipts.xlsx', array( 1, 2, 3 ) );.

    0 讨论(0)
提交回复
热议问题