Convert Figma logo to code with AI

mk-j logoPHP_XLSXWriter

Lightweight XLSX Excel Spreadsheet Writer in PHP

1,839
665
1,839
44

Top Related Projects

A pure PHP library for reading and writing spreadsheet files

4,220

Read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way

🚀 PHP Extension for creating and reader XLSX files.

A Python module for creating Excel XLSX files.

Quick Overview

PHP_XLSXWriter is a lightweight PHP library for creating Excel XLSX files. It focuses on efficiency and low memory usage, making it suitable for generating large spreadsheets with minimal resource consumption.

Pros

  • Low memory usage, allowing for creation of large spreadsheets
  • Fast performance compared to other PHP Excel libraries
  • Simple API for easy integration into existing projects
  • No external dependencies required

Cons

  • Limited formatting options compared to more comprehensive Excel libraries
  • Lacks advanced features like charts or pivot tables
  • Not actively maintained (last commit was in 2019)
  • Limited documentation and examples available

Code Examples

Creating a simple spreadsheet:

$writer = new XLSXWriter();
$writer->writeSheetHeader('Sheet1', array('First Name' => 'string', 'Last Name' => 'string', 'Age' => 'integer'));
$writer->writeSheetRow('Sheet1', array('John', 'Doe', 35));
$writer->writeSheetRow('Sheet1', array('Jane', 'Smith', 28));
$writer->writeToFile('example.xlsx');

Adding multiple sheets:

$writer = new XLSXWriter();
$writer->writeSheetHeader('Sheet1', array('Name' => 'string', 'Score' => 'integer'));
$writer->writeSheetRow('Sheet1', array('Alice', 95));
$writer->writeSheetHeader('Sheet2', array('Product' => 'string', 'Price' => 'price'));
$writer->writeSheetRow('Sheet2', array('Widget', 9.99));
$writer->writeToFile('multi_sheet.xlsx');

Using cell styles:

$styles = array('font-style' => 'bold', 'fill' => '#F0F0F0');
$writer = new XLSXWriter();
$writer->writeSheetHeader('Sheet1', array('Column1' => 'string', 'Column2' => 'string'), $styles);
$writer->writeSheetRow('Sheet1', array('Value1', 'Value2'));
$writer->writeToFile('styled.xlsx');

Getting Started

  1. Install the library using Composer:

    composer require mk-j/php_xlsxwriter
    
  2. Include the library in your PHP script:

    require 'vendor/autoload.php';
    use XLSXWriter;
    
  3. Create a new XLSXWriter instance and start writing your spreadsheet:

    $writer = new XLSXWriter();
    $writer->writeSheetHeader('Sheet1', array('Column1' => 'string', 'Column2' => 'integer'));
    $writer->writeSheetRow('Sheet1', array('Value1', 42));
    $writer->writeToFile('output.xlsx');
    

Competitor Comparisons

A pure PHP library for reading and writing spreadsheet files

Pros of PhpSpreadsheet

  • More comprehensive feature set, including support for various file formats (XLSX, XLS, ODS, CSV, etc.)
  • Actively maintained with regular updates and a large community
  • Extensive documentation and better support for complex spreadsheet operations

Cons of PhpSpreadsheet

  • Higher memory usage and slower performance, especially for large datasets
  • Steeper learning curve due to its extensive API and features
  • Larger codebase and more dependencies, which may increase project complexity

Code Comparison

PHP_XLSXWriter:

$writer = new XLSXWriter();
$writer->writeSheetHeader('Sheet1', array('Col1' => 'string', 'Col2' => 'string'));
$writer->writeSheetRow('Sheet1', array('A1', 'B1'));
$writer->writeToFile('example.xlsx');

PhpSpreadsheet:

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Col1')->setCellValue('B1', 'Col2');
$sheet->setCellValue('A2', 'A1')->setCellValue('B2', 'B1');
$writer = new Xlsx($spreadsheet);
$writer->save('example.xlsx');

Both libraries offer Excel file creation capabilities, but PHP_XLSXWriter is more lightweight and focused on XLSX generation, while PhpSpreadsheet provides a broader range of features and file format support at the cost of increased complexity and resource usage.

4,220

Read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way

Pros of Spout

  • Supports reading and writing multiple formats (XLSX, CSV, ODS)
  • Memory-efficient, suitable for large datasets
  • Actively maintained with regular updates

Cons of Spout

  • Slower performance for simple XLSX writing tasks
  • More complex API, steeper learning curve

Code Comparison

PHP_XLSXWriter:

$writer = new XLSXWriter();
$writer->writeSheetHeader('Sheet1', ['Column1' => 'string', 'Column2' => 'integer']);
$writer->writeSheetRow('Sheet1', ['Value1', 42]);
$writer->writeToFile('output.xlsx');

Spout:

$writer = WriterEntityFactory::createXLSXWriter();
$writer->openToFile('output.xlsx');
$writer->addRow(WriterEntityFactory::createRowFromArray(['Column1', 'Column2']));
$writer->addRow(WriterEntityFactory::createRowFromArray(['Value1', 42]));
$writer->close();

Both libraries offer straightforward ways to create XLSX files, but PHP_XLSXWriter has a slightly simpler API for basic tasks. Spout provides more flexibility and features, making it suitable for more complex scenarios and larger datasets. The choice between the two depends on specific project requirements, such as file format support, performance needs, and the complexity of the data manipulation required.

🚀 PHP Extension for creating and reader XLSX files.

Pros of php-ext-xlswriter

  • Higher performance due to being a PHP extension written in C
  • Supports more advanced Excel features like charts and data validation
  • Smaller memory footprint, especially for large files

Cons of php-ext-xlswriter

  • Requires installation of a PHP extension, which may not be possible in all hosting environments
  • Less portable across different PHP setups compared to a pure PHP solution
  • Steeper learning curve due to more complex API

Code Comparison

PHP_XLSXWriter:

$writer = new XLSXWriter();
$writer->writeSheetHeader('Sheet1', array('Col1' => 'string', 'Col2' => 'string'));
$writer->writeSheetRow('Sheet1', array('A1', 'B1'));
$writer->writeToFile('example.xlsx');

php-ext-xlswriter:

$config = ['path' => './example.xlsx'];
$excel = new \Vtiful\Kernel\Excel($config);
$filePath = $excel->fileName('tutorial01.xlsx', 'sheet1')
    ->header(['Item', 'Cost'])
    ->data([['Rent', 1000], ['Gas', 100]])
    ->output();

Both libraries offer straightforward APIs for creating Excel files, but php-ext-xlswriter provides more advanced features at the cost of a slightly more complex setup and usage.

A Python module for creating Excel XLSX files.

Pros of XlsxWriter

  • More comprehensive feature set, including support for charts, images, and advanced formatting options
  • Better performance for large datasets due to optimized memory usage
  • Extensive documentation and examples available

Cons of XlsxWriter

  • Requires Python, which may not be suitable for PHP-based projects
  • Steeper learning curve due to more complex API

Code Comparison

PHP_XLSXWriter:

$writer = new XLSXWriter();
$writer->writeSheetHeader('Sheet1', array('Col1' => 'string', 'Col2' => 'integer'));
$writer->writeSheetRow('Sheet1', array('Value1', 100));
$writer->writeToFile('example.xlsx');

XlsxWriter:

import xlsxwriter

workbook = xlsxwriter.Workbook('example.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write('A1', 'Col1')
worksheet.write('B1', 'Col2')
worksheet.write('A2', 'Value1')
worksheet.write('B2', 100)
workbook.close()

Both libraries offer straightforward ways to create Excel files, but XlsxWriter provides more granular control over cell formatting and worksheet structure. PHP_XLSXWriter has a simpler API, making it easier to use for basic tasks in PHP environments. XlsxWriter's Python-based approach allows for more advanced features and better performance, but may require additional setup in PHP projects.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

PHP_XLSXWriter

This library is designed to be lightweight, and have minimal memory usage.

It is designed to output an Excel compatible spreadsheet in (Office 2007+) xlsx format, with just basic features supported:

  • supports PHP 5.2.1+
  • takes UTF-8 encoded input
  • multiple worksheets
  • supports currency/date/numeric cell formatting, simple formulas
  • supports basic cell styling
  • supports writing huge 100K+ row spreadsheets

Never run out of memory with PHPExcel again.

Simple PHP CLI example:

$data = array(
    array('year','month','amount'),
    array('2003','1','220'),
    array('2003','2','153.5'),
);

$writer = new XLSXWriter();
$writer->writeSheet($data);
$writer->writeToFile('output.xlsx');

Simple/Advanced Cell Formats:

$header = array(
  'created'=>'date',
  'product_id'=>'integer',
  'quantity'=>'#,##0',
  'amount'=>'price',
  'description'=>'string',
  'tax'=>'[$$-1009]#,##0.00;[RED]-[$$-1009]#,##0.00',
);
$data = array(
    array('2015-01-01',873,1,'44.00','misc','=D2*0.05'),
    array('2015-01-12',324,2,'88.00','none','=D3*0.05'),
);

$writer = new XLSXWriter();
$writer->writeSheetHeader('Sheet1', $header );
foreach($data as $row)
	$writer->writeSheetRow('Sheet1', $row );
$writer->writeToFile('example.xlsx');

50000 rows: (1.4s, 0MB memory usage)

include_once("xlsxwriter.class.php");
$writer = new XLSXWriter();
$writer->writeSheetHeader('Sheet1', array('c1'=>'integer','c2'=>'integer','c3'=>'integer','c4'=>'integer') );
for($i=0; $i<50000; $i++)
{
    $writer->writeSheetRow('Sheet1', array($i, $i+1, $i+2, $i+3) );
}
$writer->writeToFile('huge.xlsx');
echo '#'.floor((memory_get_peak_usage())/1024/1024)."MB"."\n";
rowstimememory
500001.4s0MB
1000002.7s0MB
1500004.1s0MB
2000005.7s0MB
2500007.0s0MB

Simple cell formats map to more advanced cell formats

simple formatsformat code
string@
integer0
dateYYYY-MM-DD
datetimeYYYY-MM-DD HH:MM:SS
timeHH:MM:SS
price#,##0.00
dollar[$$-1009]#,##0.00;[RED]-[$$-1009]#,##0.00
euro#,##0.00 [$€-407];[RED]-#,##0.00 [$€-407]

Basic cell styles have been available since version 0.30

styleallowed values
fontArial, Times New Roman, Courier New, Comic Sans MS
font-size8,9,10,11,12 ...
font-stylebold, italic, underline, strikethrough or multiple ie: 'bold,italic'
borderleft, right, top, bottom, or multiple ie: 'top,left'
border-stylethin, medium, thick, dashDot, dashDotDot, dashed, dotted, double, hair, mediumDashDot, mediumDashDotDot, mediumDashed, slantDashDot
border-color#RRGGBB, ie: #ff99cc or #f9c
color#RRGGBB, ie: #ff99cc or #f9c
fill#RRGGBB, ie: #eeffee or #efe
haligngeneral, left, right, justify, center
valignbottom, center, distributed