Laravel Excel/PHP Excel:导入、修改、下载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27659170/
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:36:03  来源:igfitidea点击:

Laravel Excel/PHP Excel: Import, modify, download

phplaravelphpexcellaravel-excel

提问by LuMa

I'm struggeling with Laravel-Excel(Laravel Package of PHPExcel).

我正在努力使用Laravel-Excel(PHPExcel 的 Laravel 包)。

I'm not able to modify a file. My file has one workbook (called template). I want to load the document, modify it and let the user download it:

我无法修改文件。我的文件有一个工作簿(称为模板)。我想加载文档,修改它并让用户下载它:

Excel::selectSheets('template') -> load($source, function($file)
{

}) -> download('xls');

Code above works so far. But I don't get how to fill cells. I don't want to export an eloquent model directly, I need to follow a certain structure. The Excel file was not created by me, I just need to fill it with data.

到目前为止,上面的代码有效。但我不知道如何填充单元格。我不想直接导出一个eloquent模型,我需要遵循一定的结构。Excel 文件不是我创建的,我只需要用数据填充它。

So how can I for example fill cell A2 with the firstname of the current user?

那么我怎样才能用当前用户的名字填充单元格 A2 呢?

I search for something like

我在寻找类似的东西

Excel::cell('A2') -> content = $user -> name;

This is just an example of what I want to achive, this is not working code!

这只是我想要实现的一个例子,这不是工作代码!

Thanks, LuMa

谢谢,卢马

采纳答案by Laravelian

Laravel-Excel runs off of PHPExcel, and you can run native PHPExcel methods on the objects exposed by Laravel-Excel.

Laravel-Excel 运行于 PHPExcel,您可以在 Laravel-Excel 公开的对象上运行原生 PHPExcel 方法。

From the docs, to get the cell: http://www.maatwebsite.nl/laravel-excel/docs/export#cells

从文档中获取单元格:http: //www.maatwebsite.nl/laravel-excel/docs/export#cells

\Excel::create('Document', function($excel) {
    $excel->sheet('Sheet', function($sheet) {
        $sheet->cell('A2', function($cell) {
            $cell->setValue('this is the cell value.');
        });
    });
})->download('xls');

And, here is a screenshot of the $cell instance showing what else can be called on it:

而且,这是 $cell 实例的屏幕截图,显示了可以对其调用的其他内容:

Kint dump of the $cell instance

$cell 实例的 Kint 转储

回答by Mayra Lozano

I use that in this way:

我以这种方式使用它:

Excel::load('template_orden_compra.xls', function($doc){
   $sheet = $doc->getActiveSheet();
   $sheet->setCellValue('A2', $user->name);
})
->store('xls', storage_path('Excel'));

回答by Vishal S. Pawar

You can use following for multiple sheets.

您可以对多张纸使用以下内容。

$somevalue = array('abc', 'xyz');

Excel::load('file.xlsx', function($file) use($somevalue) {
   $file->sheet( 'sheetName', function ($sheet) use($somevalue){
        $sheet->setCellValue('A1', $somevalue);
   });
})->export('xlsx');

回答by Mantas D

You can use native PHP Excel functions:

您可以使用本机 PHP Excel 函数:

Excel::load('file.xls', function($excel)
{
    $excel->getActiveSheet()->setCellValue('A1', 'cell value here');
}) -> download('xls');