Convert Figma logo to code with AI

PHPOffice logoPhpSpreadsheet

A pure PHP library for reading and writing spreadsheet files

13,250
3,418
13,250
191

Top Related Projects

4,220

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

Lightweight XLSX Excel Spreadsheet Writer in PHP

Parse and retrieve data from Excel XLSx files

🚀 PHP Extension for creating and reader XLSX files.

Quick Overview

PHPOffice/PhpSpreadsheet is a library written in pure PHP for reading, writing, and creating spreadsheet files. It supports multiple file formats including Excel (XLSX, XLS, XLSM), OpenDocument Spreadsheet (ODS), CSV, and HTML. This library is a successor to PHPExcel and offers improved performance and features.

Pros

  • Supports multiple spreadsheet file formats
  • Actively maintained with regular updates
  • Extensive documentation and community support
  • Improved performance compared to its predecessor, PHPExcel

Cons

  • Can be memory-intensive for large spreadsheets
  • Steeper learning curve compared to simpler CSV libraries
  • Some advanced Excel features may not be fully supported
  • Requires PHP 7.2 or higher

Code Examples

Creating a new spreadsheet:

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World!');

$writer = new Xlsx($spreadsheet);
$writer->save('hello_world.xlsx');

Reading an existing spreadsheet:

use PhpOffice\PhpSpreadsheet\IOFactory;

$spreadsheet = IOFactory::load("existing_file.xlsx");
$sheet = $spreadsheet->getActiveSheet();
$value = $sheet->getCell('A1')->getValue();
echo $value;

Applying styles to cells:

use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Border;

$sheet->getStyle('A1:B2')->applyFromArray([
    'font' => ['bold' => true],
    'fill' => ['fillType' => Fill::FILL_SOLID, 'color' => ['rgb' => 'FFFF00']],
    'borders' => ['allBorders' => ['borderStyle' => Border::BORDER_THIN]]
]);

Getting Started

  1. Install PhpSpreadsheet using Composer:

    composer require phpoffice/phpspreadsheet
    
  2. Create a new PHP file and include the Composer autoloader:

    require 'vendor/autoload.php';
    
  3. Use the library in your code:

    use PhpOffice\PhpSpreadsheet\Spreadsheet;
    use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
    
    $spreadsheet = new Spreadsheet();
    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setCellValue('A1', 'Hello World!');
    
    $writer = new Xlsx($spreadsheet);
    $writer->save('hello_world.xlsx');
    

This will create a new Excel file named 'hello_world.xlsx' with "Hello World!" in cell A1.

Competitor Comparisons

4,220

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

Pros of Spout

  • Faster and more memory-efficient for large files
  • Simpler API for basic read/write operations
  • Streaming approach for handling big datasets

Cons of Spout

  • Limited formatting options compared to PhpSpreadsheet
  • Fewer advanced features (e.g., charts, formulas)
  • Read-only support for XLSX files (write support for CSV and ODS)

Code Comparison

PhpSpreadsheet example:

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World!');
$writer = new Xlsx($spreadsheet);
$writer->save('hello_world.xlsx');

Spout example:

$writer = WriterFactory::create(Type::XLSX);
$writer->openToFile('hello_world.xlsx');
$writer->addRow(['Hello World!']);
$writer->close();

Both libraries offer ways to create and manipulate spreadsheets, but Spout's API is more streamlined for simple operations. PhpSpreadsheet provides more flexibility and advanced features at the cost of increased complexity and resource usage. Choose Spout for high-performance, memory-efficient operations on large datasets, while PhpSpreadsheet is better suited for complex spreadsheet manipulations with extensive formatting options.

Lightweight XLSX Excel Spreadsheet Writer in PHP

Pros of PHP_XLSXWriter

  • Lightweight and focused solely on writing XLSX files
  • Faster performance and lower memory usage for large datasets
  • Simpler API for basic spreadsheet creation tasks

Cons of PHP_XLSXWriter

  • Limited functionality compared to PhpSpreadsheet's extensive feature set
  • Lacks support for reading existing spreadsheets or other file formats
  • Less active development and community support

Code Comparison

PHP_XLSXWriter:

$writer = new XLSXWriter();
$writer->writeSheetHeader('Sheet1', array('Name' => 'string', 'Age' => 'integer'));
$writer->writeSheetRow('Sheet1', array('John Doe', 30));
$writer->writeToFile('example.xlsx');

PhpSpreadsheet:

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Name')->setCellValue('B1', 'Age');
$sheet->setCellValue('A2', 'John Doe')->setCellValue('B2', 30);
$writer = new Xlsx($spreadsheet);
$writer->save('example.xlsx');

Both libraries offer straightforward ways to create simple spreadsheets, but PhpSpreadsheet provides more flexibility and advanced features at the cost of increased complexity and resource usage. PHP_XLSXWriter is better suited for generating large, simple spreadsheets quickly, while PhpSpreadsheet excels in handling complex spreadsheet operations and working with existing files.

Parse and retrieve data from Excel XLSx files

Pros of simplexlsx

  • Lightweight and simple to use
  • No external dependencies required
  • Faster parsing for large files

Cons of simplexlsx

  • Limited functionality compared to PhpSpreadsheet
  • Less actively maintained and fewer contributors
  • Lacks advanced features like writing Excel files

Code Comparison

simplexlsx:

$xlsx = SimpleXLSX::parse('file.xlsx');
foreach ($xlsx->rows() as $row) {
    print_r($row);
}

PhpSpreadsheet:

$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load("file.xlsx");
$worksheet = $spreadsheet->getActiveSheet();
foreach ($worksheet->getRowIterator() as $row) {
    // Process row
}

simplexlsx focuses on simplicity and ease of use, making it ideal for basic Excel file parsing. It's lightweight and has no dependencies, which can be advantageous in certain scenarios. However, it lacks the extensive feature set and active development of PhpSpreadsheet.

PhpSpreadsheet offers a more comprehensive solution for working with spreadsheets, including advanced features like writing Excel files and manipulating cell styles. It has a larger community and more frequent updates but comes with a steeper learning curve and additional dependencies.

Choose simplexlsx for quick and simple Excel parsing tasks, while PhpSpreadsheet is better suited for more complex spreadsheet operations and long-term project maintenance.

🚀 PHP Extension for creating and reader XLSX files.

Pros of php-ext-xlswriter

  • Significantly faster performance due to being a PHP extension written in C
  • Lower memory usage, especially for large spreadsheets
  • Simpler API for basic spreadsheet operations

Cons of php-ext-xlswriter

  • Less feature-rich compared to PhpSpreadsheet
  • Requires compilation and installation as a PHP extension
  • Limited support for complex formatting and advanced Excel features

Code Comparison

PhpSpreadsheet:

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World');
$writer = new Xlsx($spreadsheet);
$writer->save('hello_world.xlsx');

php-ext-xlswriter:

$excel = new \Vtiful\Kernel\Excel(['path' => './']);
$filePath = $excel->fileName('hello_world.xlsx')
    ->header(['A1' => 'Hello World'])
    ->output();

Both libraries allow for creating and manipulating Excel files, but php-ext-xlswriter offers a more concise API for basic operations. PhpSpreadsheet provides more flexibility and features at the cost of increased complexity and resource usage.

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

PhpSpreadsheet

Build Status Code Quality Code Coverage Total Downloads Latest Stable Version License Join the chat at https://gitter.im/PHPOffice/PhpSpreadsheet

PhpSpreadsheet is a library written in pure PHP and offers a set of classes that allow you to read and write various spreadsheet file formats such as Excel and LibreOffice Calc.

Installation

See the install instructions.

Documentation

Read more about it, including install instructions, in the official documentation. Or check out the API documentation.

Please ask your support questions on StackOverflow, or have a quick chat on Gitter.

Patreon

I am now running a Patreon to support the work that I do on PhpSpreadsheet.

Supporters will receive access to articles about working with PhpSpreadsheet, and how to use some of its more advanced features.

Posts already available to Patreon supporters:

  • The Dating Game
    • A look at how MS Excel (and PhpSpreadsheet) handle date and time values.
  • Looping the Loop
    • Advice on Iterating through the rows and cells in a worksheet.

And for Patrons at levels actively using PhpSpreadsheet:

  • Behind the Mask
    • A look at Number Format Masks.

The Next Article (currently Work in Progress):

  • Formula for Success
    • How to debug formulae that don't produce the expected result.

My aim is to post at least one article each month, taking a detailed look at some feature of MS Excel and how to use that feature in PhpSpreadsheet, or on how to perform different activities in PhpSpreadsheet.

Planned posts for the future include topics like:

  • Tables
  • Structured References
  • AutoFiltering
  • Array Formulae
  • Conditional Formatting
  • Data Validation
  • Value Binders
  • Images
  • Charts

After a period of six months exclusive to Patreon supporters, articles will be incorporated into the public documentation for the library.

PHPExcel vs PhpSpreadsheet ?

PhpSpreadsheet is the next version of PHPExcel. It breaks compatibility to dramatically improve the code base quality (namespaces, PSR compliance, use of latest PHP language features, etc.).

Because all efforts have shifted to PhpSpreadsheet, PHPExcel will no longer be maintained. All contributions for PHPExcel, patches and new features, should target PhpSpreadsheet master branch.

Do you need to migrate? There is an automated tool for that.

License

PhpSpreadsheet is licensed under MIT.