Laravel-excel: 2つのxlsxファイルをマージして結合したものをダウンロードするにはどうすればよいですか?

作成日 2017年10月09日  ·  3コメント  ·  ソース: Maatwebsite/Laravel-Excel

問題の前に次のいずれかを付けてください:[バグ] [提案] [質問]。

パッケージバージョン、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 評価