How to read excel data in Laravel?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
How to Read Excel Data in Laravel: From Raw Files to Database Records
Dealing with file uploads and data parsing in web applications can often be frustrating. You upload an Excel file, you try to read it using basic PHP functions like file(), and instead of clean data, you get gibberish or raw binary content. This is a very common hurdle when developers first start integrating file processing into frameworks like Laravel.
If you are trying to import data from Excel (.xlsx or .csv) into your database, relying on simple file reading functions will almost always lead to errors. The key is not just reading the file, but properly parsing its complex structure. As a senior developer, I can tell you that for structured formats like Excel, you need specialized tools.
This guide will walk you through the professional, robust way to handle Excel data in Laravel by introducing the necessary libraries and best practices.
Why Simple File Reading Fails
When you use functions like file($request->file->getRealPath()) or dd($file), you are simply getting the raw bytes of the file. An .xlsx file is actually a ZIP archive containing XML files that define the sheets, rows, and cells. PHP’s built-in file functions cannot interpret this complex structure; they just see binary data, which is why you see those "horrible things" instead of readable numbers and text.
To extract meaningful data from these formats, we must use a dedicated library designed specifically for spreadsheet manipulation.
The Solution: Using PhpSpreadsheet
The gold standard for handling Microsoft Excel files in PHP environments is the PhpSpreadsheet library. It allows you to read, write, and manipulate all major spreadsheet formats (XLSX, XLSX, ODS) seamlessly. This provides the necessary structure to extract cell values accurately.
Step 1: Installation
First, you need to install PhpSpreadsheet via Composer:
composer require phpoffice/phpspreadsheet
Step 2: Reading and Parsing the Excel File in Laravel
In your Laravel controller method, instead of raw file reading, you will load the uploaded file into a PhpSpreadsheet object.
Here is how you can modify your import function to correctly handle an uploaded Excel file:
use Illuminate\Http\Request;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
class ExcelImportController extends Controller
{
public function import_results(Request $request)
{
$this->validate($request, [
'file' => 'required|mimes:csv,xls,xlsx'
]);
// 1. Get the uploaded file instance
$file = $request->file('file');
try {
// 2. Create a temporary path for the file (optional but good practice)
$filePath = $file->getRealPath();
// 3. Load the spreadsheet using PhpSpreadsheet
$spreadsheet = IOFactory::load($filePath);
// 4. Access the first sheet
$sheet = $spreadsheet->getActiveSheet();
// 5. Iterate through the data rows
$data = [];
foreach ($sheet->getRowIterator() as $row) {
$cellIterator = $row->getCellIterator();
$rowData = [];
foreach ($cellIterator as $cell) {
$rowData[] = $cell->getValue();
}
$data[] = $rowData;
}
// 6. Process the data (e.g., insert into the database)
// Example: Insert $data into your database model...
// YourModel::insert($data);
return response()->json(['message' => 'Excel data successfully processed.']);
} catch (ReaderException $e) {
return response()->json(['error' => 'Error reading file: ' . $e->getMessage()], 500);
} catch (\Exception $e) {
return response()->json(['error' => 'An unexpected error occurred: ' . $e->getMessage()], 500);
}
}
}
Best Practices for Data Import
When dealing with large datasets, always focus on efficiency and error handling.
- Use Streams (For Large Files): For extremely large Excel files, loading the entire file into memory at once can cause issues. Explore PhpSpreadsheet's ability to handle reading from streams if you are working with massive amounts of data.
- Error Handling is Crucial: As demonstrated above, wrap your file operations in
try...catchblocks. If a user uploads a corrupted file or an unsupported format, your application should gracefully report the error instead of crashing. This makes your application much more robust—a core principle when building scalable applications with Laravel. - Database Mapping: Before inserting data, ensure you have a clear mapping between the columns in your Excel sheet and the fields in your database table. This prevents runtime errors during the final insertion step.
Conclusion
Stop trying to treat complex file formats like Excel as simple text files. By adopting a robust library like PhpSpreadsheet, you transform raw binary data into structured, usable PHP objects. This approach is essential for building reliable data ingestion pipelines in Laravel. Remember, leveraging powerful community packages is how you build elegant and maintainable solutions, just as with the extensive ecosystem provided by laravelcompany.com. Happy coding!