phpexcellaravellaravel-10laravel-excel

Is there a class just for reading excel file using Laravel Excel?


I've a laravel 10 project. And I'm using Laravel Excel Package.

I read the documentation, it's divided into Imports and Exports. There isn't any mention for just reading the file. I mean, I know we can make adjustments in Imports, but I wanted to know any proper standard way just for reading the file & manipulating data.

Is there a way we can just create a class file for multiple Spreedsheets like it's being done for Imports and Exports, just for reading and manipulating data, rather than importing it to database?


Solution

  • Answer:

    Here is a solution to read and manipulate data from spreadsheet files directly in Laravel using the Laravel Excel package. This is particularly useful when you need to work with data from Excel files without importing them into a database.

    Step 1: Install Laravel Excel

    composer require maatwebsite/excel
    use Maatwebsite\Excel\Facades\Excel;
    use Illuminate\Support\Collection;
    
    // Read the Excel file
    $excelSpreadSheetData = Excel::toCollection(null, 'your-spreadsheet.xlsx');
    
    // In `$excelSpreadSheetData`, you get all the sheets available in your Excel file.
    // Suppose that in your Excel file, there are 3 sheets (User, Employee, Salary).
    // You can access the data like this:
    
    $userData = $excelSpreadSheetData[0];
    $employeeData = $excelSpreadSheetData[1];
    $salaryData = $excelSpreadSheetData[2];
    

    This approach allows you to work directly with data from Excel files in Laravel, and it can be useful for tasks that don't involve importing data into a database.

    I hope this helps anyone looking to work with Excel data in Laravel.