问题
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