Boost PHP Excel Performance with the High‑Speed xlswriter Extension
The article introduces xlswriter, a high‑performance PHP C extension for reading and writing Excel 2007+ XLSX files across major operating systems, compares it with PHPExcel, presents benchmark results, explains PECL installation, and provides detailed code examples for exporting, importing, and chart generation.
Overview
xlswriter is a high‑performance PHP C extension that enables reading and writing Excel 2007+ XLSX files. It runs on Linux, FreeBSD, OpenBSD, macOS, and Windows.
Why Use xlswriter
Compared with PHPExcel, xlswriter consumes far less memory. PHPExcel often fails on large datasets unless memory_limit is increased, which can significantly slow down processing.
Features
Write
100% compatible Excel XLSX files
Full Excel formatting support
Cell merging
Custom worksheet names
Filters
Charts
Data validation and drop‑down lists
Worksheet PNG/JPEG images
Memory‑optimized mode for large files
Cross‑platform (Linux, FreeBSD, OpenBSD, macOS, Windows)
32‑bit and 64‑bit builds
FreeBSD license
Only dependency is zlib
Read
Complete data read
Cursor‑based reading
Read by data type
Benchmark
Test environment
Macbook Pro 13 inch, Intel Core i5, 16GB 2133MHz LPDDR3 Memory, 128GB SSD StorageExport 1,000,000 rows (27 columns, all strings, length 19)
Default mode: 29 s, 2083 MB memory
Memory‑optimized mode: 52 s, < 1 MB memory
Import 1,000,000 rows (1 column, INT)
Full mode: 3 s, 558 MB memory
Cursor mode: 2.8 s, < 1 MB memory
Installation
Recommended installation via PECL: $ pecl install xlswriter If you encounter the error “No releases available for package \"pecl.php.net/xlswriter\"”, run the command with root privileges: sudo pecl install xlswriter After installation, add the extension to php.ini:
extension = xlswriter.soIDE Helper
composer require viest/php-ext-xlswriter-ide-helper:dev-masterUsage Examples
Exporting a file
Note: If a file with the same name exists in the target directory, it will be overwritten.
<?php
/**
* @desc Export file
*/
declare(strict_types=1);
$config = [
'path' => '/home/www/build' // xlsx save path
];
$excel = new \Vtiful\Kernel\Excel($config);
$filePath = $excel->fileName('tutorial01.xlsx', 'sheet1')
->header(['Item', 'Cost'])
->data([
['Rent', 1000],
['Gas', 100],
['Food', 300],
['Gym', 50],
])->output();
var_dump($filePath);Output:
string(31) "/home/www/build/tutorial01.xlsx"Reading a file
<?php
/**
* @desc Read file
*/
declare(strict_types=1);
$config = [
'path' => '/home/www/build' // xlsx read path
];
$excel = new \Vtiful\Kernel\Excel($config);
$data = $excel->openFile('tutorial01.xlsx')
->openSheet()
->getSheetData();
var_dump($data);Resulting array (truncated for brevity):
array(5) {
[0]=> array(2) { [0]=> string(4) "Item" [1]=> string(4) "Cost" }
[1]=> array(2) { [0]=> string(4) "Rent" [1]=> int(1000) }
...
}Creating a chart (column chart)
<?php
/**
* @desc chart.php example
*/
declare(strict_types=1);
$config = [
'path' => '/home/www/build'
];
$fileObject = (new \Vtiful\Kernel\Excel($config))->fileName('chart.xlsx');
$fileHandle = $fileObject->getHandle();
$chart = new \Vtiful\Kernel\Chart($fileHandle, \Vtiful\Kernel\Chart::CHART_COLUMN);
$chartResource = $chart->series('Sheet1!$A$1:$A$5')
->series('Sheet1!$B$1:$B$5')
->series('Sheet1!$C$1:$C$5')
->toResource();
$filePath = $fileObject->data([
[1, 2, 3],
[2, 4, 6],
[3, 6, 9],
[4, 8,12],
[5,10,15],
])->insertChart(0, 3, $chartResource)->output();Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
