使用 Laravel-Excel 将行附加到现有 Excel 文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25793179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:05:43  来源:igfitidea点击:

Append Rows to existing Excel document using Laravel-Excel

laravellaravel-4phpexcel

提问by Glad To Help

I have an existing Excel document, to which I want to append some data using Laravel-Excel. The package has nice documentation but unfortunately there is no full example showing how to do that, but only partial demonstration of manipulating rows.

我有一个现有的 Excel 文档,我想使用Laravel-Excel向其中附加一些数据。该包有很好的文档,但不幸的是没有完整的示例展示如何做到这一点,但只有部分操作演示。

What I am attempting to do is:

我正在尝试做的是:

  1. open the existing document
  2. get the first sheet, append new
  3. close the document
  1. 打开现有文档
  2. 获取第一张表,追加新
  3. 关闭文档

The code:

编码:

Excel::load($path . '/exported.xls', function($reader){
                    $sheet = $reader->getActiveSheet();
                    // Manipulate third row
                    $sheet->row(3, array(
                            'test1', 'test2'
                        ));
                });

Which results in

这导致

Call to undefined method PHPExcel_Worksheet::row()

调用未定义的方法 PHPExcel_Worksheet::row()

Has anyone succeeded appending data with this package?

有没有人成功地用这个包附加数据?

采纳答案by Glad To Help

Right now it is not possible to edit an Excel file like I intended to. Source: package creator.

现在不可能像我想要的那样编​​辑 Excel 文件。来源:包创建者

回答by Patrick Brouwers

Laravel Excel 1.2.0 added support for appending rows (so modifying existing Excel files)

Laravel Excel 1.2.0 添加了对追加行的支持(因此修改现有的 Excel 文件)

http://www.maatwebsite.nl/laravel-excel/docs/import#edit

http://www.maatwebsite.nl/laravel-excel/docs/import#edit

The correct code would be:

正确的代码是:

Excel::load($path . '/exported.xls', function($reader)
{
    $reader->sheet(function($sheet) 
    {
        // Manipulate third row
        $sheet->row(3, array(
            'test1', 'test2'
        ));
    });
})->export('xls');

回答by user1669496

It looks like you would use...

看起来你会用...

$sheet->appendRow(array(
    'appended', 'appended'
));

Found in the docs here... http://www.maatwebsite.nl/laravel-excel/docs/export

在这里的文档中找到... http://www.maatwebsite.nl/laravel-excel/docs/export