MENU

Importing Excel Files (XLSX) into a Laravel Project

Importing Excel Files (XLSX) into a Laravel Project
Pierre Mukisa Nov 05, 2024 | Laravel Tutorial Article Basics
Learn how to import Excel files (XLSX) into your Laravel project using PhpSpreadsheet. A robust alternative to Laravel Excel for seamless data handling! #Laravel #PhpSpreadsheet #WebDevelopment

In many scenarios, developers need to work with Excel files, especially when dealing with data import/export functionalities in web applications. For instance, you might need to import user data, sales records, or any structured information that is commonly stored in Excel spreadsheets.

Recently, I faced challenges while trying to import an Excel file using the popular "Laravel Excel" package. After two days of troubleshooting various issues, I decided to explore an alternative: PhpSpreadsheet.

What is PhpSpreadsheet?

PhpSpreadsheet is a library that allows you to read and write spreadsheet files in various formats, including XLSX, XLS, CSV, and more. It is a powerful tool that provides an extensive set of features for manipulating spreadsheet data, making it an excellent choice for projects that require Excel file handling.

Installing PhpSpreadsheet with XAMPP

Here's a step-by-step guide to install PhpSpreadsheet in your Laravel project while using XAMPP:

Step 1: Install PhpSpreadsheet via Composer

Open your terminal and navigate to your Laravel project directory. Run the following command to install PhpSpreadsheet:


composer require phpoffice/phpspreadsheet

Step 2: Configuring PHP Settings
Before you start using PhpSpreadsheet, ensure that your PHP settings support the library. You may need to adjust your php.ini file: Locate your php.ini file. In XAMPP, it's typically found in the C:\xampp\php\ directory. Open php.ini in a text editor. Make sure the following extensions are enabled by removing the semicolon (;) at the beginning of the lines if they are commented out:

extension=php_zip.dll
extension=php_xml.dll
extension=php_gd2.dll
Save the php.ini file and restart your Apache server through the XAMPP control panel.


Step 3: Importing an Excel File
Now that you have PhpSpreadsheet installed and configured, you can use it to import an Excel file. Here’s a simple example of how to import an XLSX file located in the public folder of your Laravel project:
In your controller, add the following method to handle the import:
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PhpOffice\PhpSpreadsheet\IOFactory;

class ExcelImportController extends Controller
{
    public function import()
    {
        $filePath = public_path('yourfile.xlsx'); // Adjust the filename accordingly
        $spreadsheet = IOFactory::load($filePath);
        $data = $spreadsheet->getActiveSheet()->toArray();

        // Use dd() to display the imported data
        dd($data);
    }
}

Conclusion
With PhpSpreadsheet, you can efficiently handle Excel file imports in your Laravel project. This library provides a robust alternative to "Laravel Excel," allowing you to read and manipulate spreadsheet data seamlessly. Simply install the package, configure your PHP settings, and you’re ready to import Excel files with ease!


Share this post