Laravel,如何基于此代码修改/编辑Excel?

时间:2018-09-20 05:11:25

标签: php laravel

我在控制器中有一个工作代码,如下所示,问题是当我要编辑其内容时,它无法保存。

控制器

$rows = Excel::load($file_path, function($reader) use ($column_number)
{
    $reader->takeColumns($column_number);
    $reader->noHeading();

})->get();

$rows = $rows->toArray();

查看内容

dump($rows[0][0]); // this will display A1 cell and it is working fine

但是当我尝试这样做

$rows[0][0] = "Test"; // this will not write the cell A1
dump($rows); // but it will display as shown in picture below

enter image description here

2 个答案:

答案 0 :(得分:1)

通过工作表索引加载文件并附加值

$download_file_name = 'DataFile';
 Excel::selectSheetsByIndex(1)->load($file_path, function($reader) use () {
    $reader->sheet('Sheet1', function($sheet) use () {
        //if you want to add value append single value
        $sheet->SetCellValue("A7", "Exporter");
        //if want to append row
        $sheet->appendRow($dispColArray);                  
                     });
}, 'UTF-8')->setFilename($download_file_name)->download('xls');

您还可以将以下功能用于图纸样式

  1. 用于工作表样式

    $sheet->setStyle(array(
        'font' => array(
            'name' => 'Calibri',
            'size' => 9,
            'background' => '#ffffff'
        )
    ));
    
  2. 合并单元格和设计

    $sheet->mergeCells('A1:D4');
    $sheet->cells('A1:D4', function($cells) {
        $cells->setBackground('#ffffff');
    
    });
    

答案 1 :(得分:0)

您可以在Excel Load中修改值。

$rows = Excel::load($file_path, function($reader)
{
    $reader->sheet('sheet1', function($sheet){
        $sheet->cells('E4', function($cell) {
            $cell->setValue('my value');
        });
    });     

})->store('xls');