如何使用 PHPExcel 合并 Excel 文档?

发布于 2024-12-03 05:20:44 字数 197 浏览 1 评论 0原文

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

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

有没有一种方法可以使用 PHPExcel 将两个(或多个)Excel 文档“连接”为一个?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以为你会在 2024-12-10 05:20:44

在 Excel 中,“选项卡”被称为“工作表”。您可以在 PHPExcel 中创建包含多个工作表的 Excel 工作簿。

作为参考,SO的另一个答案有有关如何向现有工作簿添加其他工作表的简单易懂的指南

Codeplex 上的这篇文章有一个添加外部工作表的示例。但我不确定是否需要所有的重命名舞蹈。 (Codeplex 链接鼓励您克隆工作表并复制克隆的工作表,以免将其从原始工作簿中删除,但我认为这不会成为问题,除非您将输出写入源工作簿。)认为这样的事情应该可行:

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 ) ); 即可。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文