skip to Main Content

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?

2

Answers


  1. 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 MaatwebsiteExcelFacadesExcel;
    use IlluminateSupportCollection;
    
    // 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.

    Login or Signup to reply.
  2. An alternative way is to use another library – https://packagist.org/packages/avadim/fast-excel-reader

    This library is read-only data

    use avadimFastExcelReaderExcel;
    
    $excel = Excel::open($file);
    $sheet = $excel->sheet('Users');
    
    // Read all rows in two-dimensional array (ROW x COL)
    $result = $excel->readRows();
    

    If your file is too large, you can read it row by row

    $sheet = $excel->sheet();
    foreach ($sheet->nextRow() as $rowNum => $rowData) {
        // $rowData is array ['A' => ..., 'B' => ...]
        // handling of $rowData here
        // ...
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search