1. Go to this page and download the library: Download kolay/xlsx-stream library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
kolay / xlsx-stream example snippets
use Kolay\XlsxStream\Writers\SinkableXlsxWriter;
use Kolay\XlsxStream\Sinks\FileSink;
// Create writer with file sink
$sink = new FileSink('/path/to/output.xlsx');
$writer = new SinkableXlsxWriter($sink);
// Set headers
$writer->startFile(['Name', 'Email', 'Phone']);
// Write rows
$writer->writeRow(['John Doe', '[email protected]', '+1234567890']);
$writer->writeRow(['Jane Smith', '[email protected]', '+0987654321']);
// Or write multiple rows at once
$writer->writeRows([
['Bob Johnson', '[email protected]', '+1111111111'],
['Alice Brown', '[email protected]', '+2222222222'],
]);
// Finish and close file
$stats = $writer->finishFile();
echo "Generated {$stats['rows']} rows in {$stats['sheets']} sheet(s)";
use Kolay\XlsxStream\Writers\SinkableXlsxWriter;
use Kolay\XlsxStream\Sinks\S3MultipartSink;
use Aws\S3\S3Client;
// Create S3 client
$s3Client = new S3Client([
'region' => 'us-east-1',
'version' => 'latest',
'credentials' => [
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
],
]);
// Create S3 sink with 32MB parts
$sink = new S3MultipartSink(
$s3Client,
'my-bucket',
'exports/report.xlsx',
32 * 1024 * 1024 // 32MB parts for optimal performance
);
$writer = new SinkableXlsxWriter($sink);
// Configure for maximum performance
$writer->setCompressionLevel(1) // Fastest compression
->setBufferFlushInterval(10000); // Flush every 10K rows
$writer->startFile(['ID', 'Name', 'Email', 'Status']);
// Stream millions of rows with constant 32MB memory
User::query()
->select(['id', 'name', 'email', 'status'])
->chunkById(1000, function ($users) use ($writer) {
foreach ($users as $user) {
$writer->writeRow([
$user->id,
$user->name,
$user->email,
$user->status
]);
}
});
$stats = $writer->finishFile();
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Kolay\XlsxStream\Writers\SinkableXlsxWriter;
use Kolay\XlsxStream\Sinks\S3MultipartSink;
use Aws\S3\S3Client;
use App\Models\User;
class ExportUsersJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function handle()
{
// Create S3 client from Laravel config
$s3Config = config('filesystems.disks.s3');
$s3Client = new S3Client([
'region' => $s3Config['region'],
'version' => 'latest',
'credentials' => [
'key' => $s3Config['key'],
'secret' => $s3Config['secret'],
],
]);
// Setup S3 streaming
$filename = 'exports/users-' . now()->format('Y-m-d-H-i-s') . '.xlsx';
$sink = new S3MultipartSink(
$s3Client,
$s3Config['bucket'],
$filename,
32 * 1024 * 1024
);
$writer = new SinkableXlsxWriter($sink);
$writer->setCompressionLevel(1)
->setBufferFlushInterval(10000);
// Export headers
$writer->startFile([
'ID',
'Name',
'Email',
'Created At',
'Status'
]);
// Export data with chunking
User::query()
->orderBy('id')
->chunkById(1000, function ($users) use ($writer) {
foreach ($users as $user) {
$writer->writeRow([
$user->id,
$user->name,
$user->email,
$user->created_at->format('Y-m-d H:i:s'),
$user->status
]);
}
// Clear Eloquent cache periodically (optional)
// You can track rows externally if needed
});
$stats = $writer->finishFile();
\Log::info('Export completed', [
'filename' => $filename,
'rows' => $stats['rows'],
'sheets' => $stats['sheets'],
'bytes' => $stats['bytes']
]);
}
}
use Kolay\XlsxStream\Writers\SinkableXlsxWriter;
// Streams directly to S3 — credentials, region, bucket all from config
$writer = SinkableXlsxWriter::forDisk('s3', 'exports/users.xlsx');
// Or to a local disk — resolves to disk root + path
$writer = SinkableXlsxWriter::forDisk('local', 'exports/users.xlsx');
// Pass S3-specific options (ACL, ContentDisposition, Metadata) as the 3rd arg
$writer = SinkableXlsxWriter::forDisk('s3', 'reports/q4.xlsx', [
'ACL' => 'public-read',
'CacheControl' => 'max-age=3600',
]);
$writer->startFile(['ID', 'Name'])
->writeRow([1, 'Alice'])
->finishFile();
// Only these stay in memory:
$rowBuffer = ''; // Current batch of rows (flushed every 10K rows)
$deflateContext = ...; // Compression context (minimal)
$zipMetadata = []; // File entry metadata (bytes per file)
// Memory components:
$buffer = ''; // 32MB max (uploaded when full)
$parts = []; // Part metadata (ETag + number per part)
// Growth: ~1KB per part × (filesize / 32MB) parts
// Example: 100MB file = 3 parts = ~3KB metadata