Laravel-excel: 如何合并两个 xlsx 文件并下载合并的文件?

创建于 2017-10-09  ·  3评论  ·  资料来源: Maatwebsite/Laravel-Excel

请为您的问题添加以下前缀之一:[BUG] [PROPOSAL] [QUESTION]。

包版,Laravel 版

预期行为

实际行为

异常堆栈跟踪

Excel 文件的屏幕截图

重现行为的步骤

最有用的评论

@sarpkaya-xx 试试addSheet()

    Excel::create('workbook', function ($excel) {
        // Create first "original" sheet
        $original = $excel->sheet('Sheet 1', function ($sheet) {
            $sheet->fromArray([
                ['foo' => 'bar']
            ]);
        });
        // Copy the first sheet 4 times
        for ($sheetCount = 2; $sheetCount <= 5; $sheetCount++) {
            $copy = $original->getSheet()->copy();
            $copy->setTitle("Sheet {$sheetCount}");
            $excel->addSheet($copy);
        }
    })->save();

所有3条评论

@kurianic ,下面的示例将读取工作簿 A 和 B 中的所有工作表,并将它们复制到新工作簿 C。请确保 A 和 B 不包含同名工作表。

// Load the workbooks to merge in a collection.
// This example is assuming they're stored in the Laravel storage folder.
$workbooks = collect([
    'workbookA.xlsx',
    'workbookB.xlsx',
])->map(function ($filename) {
    return Excel::load(storage_path($filename));
});

// Create merged workbook
$workbookC = Excel::create('workbookC', function ($excel) use ($workbooks) {
    // For each workbook to be merged
    $workbooks->each(function ($workbook) use ($excel) {
        // Get all the sheets
        collect($workbook->getAllSheets())->each(function ($sheet) use ($excel) {
            // And add them to the merged workbook
            $excel->addExternalSheet($sheet);
        });
    });
})->save(); // save merged workbook to storage/exports/workbookC.xlsx

@stephanecoinon ,我想多次添加相同的工作表,以防止名称重复,我调用了 $sheet->setTitle。 但它给了我一个错误“工作表不存在”。 在 setTitle 之后调用 addExternalSheet 时。

$sheet_original->setTitle("SHEET ".$i); $sheets[$i] = $sheet_original; $sheets[$i]->cell('I64', function($cell) use($i) { $cell->setValue($i); }); $excel->addExternalSheet($sheets[$i] );

@sarpkaya-xx 试试addSheet()

    Excel::create('workbook', function ($excel) {
        // Create first "original" sheet
        $original = $excel->sheet('Sheet 1', function ($sheet) {
            $sheet->fromArray([
                ['foo' => 'bar']
            ]);
        });
        // Copy the first sheet 4 times
        for ($sheetCount = 2; $sheetCount <= 5; $sheetCount++) {
            $copy = $original->getSheet()->copy();
            $copy->setTitle("Sheet {$sheetCount}");
            $excel->addSheet($copy);
        }
    })->save();
此页面是否有帮助?
0 / 5 - 0 等级

相关问题

disto picture disto  ·  3评论

amine8ghandi8amine picture amine8ghandi8amine  ·  3评论

muhghazaliakbar picture muhghazaliakbar  ·  3评论

lucatamtam picture lucatamtam  ·  3评论

wwendorf picture wwendorf  ·  3评论