DEV Community

viest
viest

Posted on

PHP high-performance Excel extension, don't worry about memory consumption, don't worry about the speed is too slow.

xlswriter is a PHP C Extension that can be used to write text, numbers, formulas and hyperlinks to multiple worksheets in an Excel 2007+ XLSX file. It supports features such as:

Writer:

  • 100% compatible Excel XLSX files.
  • Full Excel formatting.
  • Merged cells.
  • Defined names.
  • Autofilters.
  • Charts.
  • Data validation.
  • Worksheet PNG/JPEG images.
  • Memory optimization mode for writing large files.
  • Works on Linux, FreeBSD, OpenBSD, OS X, Windows.
  • Compiles for 32 and 64 bit.
  • FreeBSD License.
  • The only dependency is on zlib.

Reader:

  • Full read data.
  • Cursor read data.
  • Read by data type.

Benchmark

Test environment: Macbook Pro 13 inch, Intel Core i5, 16GB 2133MHz LPDDR3 Memory, 128GB SSD Storage.

Export

Two memory modes export 1 million rows of data (27 columns, data is string)

  • Normal mode: only 29S is needed, and the memory only needs 2083MB;
  • Fixed memory mode: only need 52S, memory only needs <1MB;

Import

1 million rows of data (1 columns, data is inter)

  • Full mode: Just 3S, the memory is only 558MB;
  • Cursor mode: Just 2.8S, memory is only <1MB;

Install

pecl install xlswriter

Quick start

Writer:

$config = ['path' => '/home/viest'];
$excel  = new \Vtiful\Kernel\Excel($config);

// fileName will automatically create a worksheet, 
// you can customize the worksheet name, the worksheet name is optional
$filePath = $excel->fileName('tutorial01.xlsx', 'sheet1')
    ->header(['Item', 'Cost'])
    ->data([
        ['Rent', 1000],
        ['Gas',  100],
        ['Food', 300],
        ['Gym',  50],
    ])
    ->output();

Reader:

$config   = ['path' => '/home/viest'];
$excel    = new \Vtiful\Kernel\Excel($config);

$data = $excel->openFile('tutorial.xlsx')
    ->openSheet()
    ->getSheetData();

var_dump($data)

Link

GitHub: https://github.com/viest/php-ext-xlswriter
Document: https://xlswriter-docs.viest.me

Top comments (0)