How can I join Excel documents using PHPExcel?

一曲冷凌霜 提交于 2019-12-30 01:08:07

问题


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 worksheet).

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


回答1:


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 ) );.



来源:https://stackoverflow.com/questions/7286223/how-can-i-join-excel-documents-using-phpexcel

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