tandrezone / xslttojson
Convert XLSX files with multiple sheets to JSON
dev-main
2026-04-21 15:39 UTC
Requires
- phpoffice/phpspreadsheet: ^2.4
Requires (Dev)
- phpunit/phpunit: ^11
This package is not auto-updated.
Last update: 2026-06-03 14:28:29 UTC
README
Convert an XLSX file (with one or more sheets) to JSON. Every worksheet becomes a key in the output object whose value is an array of row objects. The first row of each sheet is used as the header – its cell values become the property names for all subsequent rows.
Requirements
- PHP 8.1 or higher
- Composer
Installation
composer require tandrezone/xslttojson
Or clone and install locally:
git clone https://github.com/tandrezone/xslttojson.git
cd xslttojson
composer install
Library usage
<?php require 'vendor/autoload.php'; use XlsxToJson\XlsxToJson; $result = XlsxToJson::convert('/path/to/data.xlsx'); echo json_encode($result, JSON_PRETTY_PRINT);
Output format
Given a workbook with two sheets:
| Employees | Name | Department | Salary |
|---|---|---|---|
| Alice | Engineering | 90000 | |
| Bob | Marketing | 75000 |
| Offices | City | Country | Headcount |
|---|---|---|---|
| Paris | France | 42 | |
| London | UK | 65 |
XlsxToJson::convert() returns:
{
"Employees": [
{ "Name": "Alice", "Department": "Engineering", "Salary": "90000" },
{ "Name": "Bob", "Department": "Marketing", "Salary": "75000" }
],
"Offices": [
{ "City": "Paris", "Country": "France", "Headcount": "42" },
{ "City": "London", "Country": "UK", "Headcount": "65" }
]
}
CLI usage
Usage: xslttojson <file> [options]
Arguments:
file Path to the .xlsx file to convert
Options:
-o, --output Path to write the JSON output (defaults to stdout)
-h, --help Show this help message
Examples
Print JSON to stdout:
php bin/xslttojson data.xlsx
Write JSON to a file:
php bin/xslttojson data.xlsx -o output.json
Running tests
composer test