开发者

How can I join Excel documents using PHPExcel?

I'm using PHPExcel to dynamically generate order receipts.

I'd like to be able to generate a "summary" Excel 开发者_Python百科file, containing all the order receipts (one per worksheet).

Is there a way to "join" two (or more) Excel documents into one with PHPExcel ?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜