Laravel-excel: ¿Cómo puedo combinar dos archivos xlsx y descargar el combinado?

Creado en 9 oct. 2017  ·  3Comentarios  ·  Fuente: Maatwebsite/Laravel-Excel

Por favor, anteponga a su problema uno de los siguientes: [ERROR] [PROPUESTA] [PREGUNTA].

Versión de paquete, versión de Laravel

Comportamiento esperado

Comportamiento real

Seguimiento de la pila de excepciones

Captura de pantalla del archivo de Excel

Pasos para reproducir el comportamiento

Comentario más útil

@ sarpkaya-xx prueba 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();

Todos 3 comentarios

Hola @kurianic , el siguiente ejemplo leerá todas las hojas de los libros de trabajo A y B, y las copiará en un nuevo libro de trabajo C. Asegúrese de que A y B no contengan hojas con el mismo nombre.

// 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

Hola @stephanecoinon , quiero agregar la misma hoja varias veces, para evitar nombres duplicados, llamé $ sheet-> setTitle. pero me da un error "La hoja no existe". cuando se llama a addExternalSheet justo después de setTitle.

$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 prueba 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();
¿Fue útil esta página
0 / 5 - 0 calificaciones