如何使用PHPExcel联接Excel文档? [英] How can I join Excel documents using PHPExcel?

查看:93
本文介绍了如何使用PHPExcel联接Excel文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PHPExcel 动态生成订单收据.

I'm using PHPExcel to dynamically generate order receipts.

我希望能够生成一个摘要" Excel文件,其中包含所有订单收据(每个工作表一个).

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

是否可以使用PHPExcel将两个(或多个)Excel文档合并"为一个文档?

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

推荐答案

在Excel中,标签"被称为工作表".您可以在PHPExcel中创建包含多个工作表的Excel工作簿.

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

作为参考,关于SO的另一个答案是

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

关于Codeplex的帖子有添加外部工作表的示例.不过,我不确定是否需要所有重命名的舞蹈. (Codeplex链接鼓励您克隆工作表并复制克隆的工作表,以免将其从原始工作簿中删除,但是除非您将输出写入源工作簿,否则我认为这不会成为问题.)认为这样的事情应该起作用:

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

然后您可以致电createMasterWorkbook( 'receipts.xlsx', array( 1, 2, 3 ) );.

这篇关于如何使用PHPExcel联接Excel文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆