Member-only story
Reading Excel Files Without Packages in Laravel: A Step-by-Step Guide
Introduction:
Reading Excel files in Laravel usually involves packages like PhpSpreadsheet
or Laravel Excel
. But what if you want to keep your application lean, reduce dependencies, or gain control over the parsing process? This guide will walk you through reading Excel files without using any external packages in Laravel. We’ll parse the raw file and extract the data, making it a simple and efficient solution for basic use cases.
Prerequisites:
To follow along, you should have a basic understanding of:
- Laravel framework
- PHP file handling and XML parsing basics
Excel File Structure:
Excel files (XLSX) are essentially compressed ZIP files with a specific structure. Each XLSX file contains:
- Shared Strings (
xl/sharedStrings.xml
):-Stores all unique strings in the file, referenced by index. - Worksheet Files (
xl/worksheets/sheet1.xml
, etc.): Contains cell data and references to shared strings.
Step-by-Step Guide
Step 1: Extracting the Contents:
Since XLSX files are ZIP archives, we can extract them using PHP’s ZipArchive
class.