Skip to content

Lightweight and very fast XLSX Excel Spreadsheet Writer in PHP

License

Notifications You must be signed in to change notification settings

aVadim483/fast-excel-writer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GitHub Release Packagist Downloads GitHub License Static Badge

FastExcelWriter Logo

fastest memory saving multiple worksheet
book and sheet protection multiple charts styling and image insertion

FastExcelWriter v.6

FastExcelWriter is a part of the FastExcelPhp Project which consists of

Introduction

This library is designed to be lightweight, super-fast and requires minimal memory usage.

FastExcelWriter creates Excel compatible spreadsheets in XLSX format (Office 2007+), with many features supported:

  • Takes UTF-8 encoded input
  • Multiple worksheets
  • Supports currency/date/numeric cell formatting, formulas and active hyperlinks
  • Supports most styling options for cells, rows, columns - colors, borders, fonts, etc.
  • You can set the height of the rows and the width of the columns (including auto width calculation)
  • You can add formulas, notes and images in you XLSX-files
  • Supports workbook and sheet protection with/without passwords
  • Supports page settings - page margins, page size
  • Inserting multiple charts
  • Supports data validations

Jump To:

Installation

Use composer to install FastExcelWriter into your project:

composer require avadim/fast-excel-writer

Changes In Version 6

  • Data Validation support

Important changes in version 6.1

  • Sheet::setRowOptions(), Sheet::setColOptions(), Sheet::setRowStyles() and Sheet::setColStyles() are deprecated, instead of them you should use other functions: setRowStyle(), setRowStyleArray(), setRowDataStyle(), setRowDataStyleArray(), setColStyle(), setColStyleArray(), setColDataStyle(), setColDataStyleArray()
  • The behavior of the Sheet::setRowStyle() and Sheet::setColStyle() has changed, they now set styles for the entire row or column (even if they are empty)

Changes In Version 5

  • The general news is Chart support

Important changes in version 5.8

Before v.5.8

$sheet->writeCell(12345); // The number 12345 will be written into the cell
$sheet->writeCell('12345'); // The number 12345 will also be written here

In version 5.8 and later

$sheet->writeCell(12345); // The number 12345 will be written into the cell
$sheet->writeCell('12345'); // Here the string '12345' will be written into the cell

If you want to keep the previous behavior for backward compatibility, you should use option 'auto_convert_number' when creating a workbook.

$excel = Excel::create(['Sheet1'], ['auto_convert_number' => true]);
$sheet = $excel->sheet();
$sheet->writeCell('12345'); // String '12345' will be automatically converted to a number

Changes In Version 4

  • Now the library works even faster
  • Added a fluent interface for applying styles.
  • New methods and code refactoring

Usage

You can find usage examples below or in /demo folder

Simple Example

use \avadim\FastExcelWriter\Excel;

$data = [
    ['2003-12-31', 'James', '220'],
    ['2003-8-23', 'Mike', '153.5'],
    ['2003-06-01', 'John', '34.12'],
];

$excel = Excel::create(['Sheet1']);
$sheet = $excel->sheet();

// Write heads
$sheet->writeRow(['Date', 'Name', 'Amount']);

// Write data
foreach($data as $rowData) {
    $rowOptions = [
        'height' => 20,
    ];
    $sheet->writeRow($rowData, $rowOptions);
}

$excel->save('simple.xlsx');

Also, you can download generated file to client (send to browser)

$excel->download('download.xlsx');

Advanced Example

use \avadim\FastExcelWriter\Excel;

$head = ['Date', 'Name', 'Amount'];
$data = [
    ['2003-12-31', 'James', '220'],
    ['2003-8-23', 'Mike', '153.5'],
    ['2003-06-01', 'John', '34.12'],
];
$headStyle = [
    'font' => [
        'style' => 'bold'
    ],
    'text-align' => 'center',
    'vertical-align' => 'center',
    'border' => 'thin',
    'height' => 24,
];

$excel = Excel::create(['Sheet1']);
$sheet = $excel->sheet();

// Write the head row (sets style via array)
$sheet->writeHeader($head, $headStyle);

// The same result with new fluent interface
$sheet->writeHeader($head)
    ->applyFontStyleBold()
    ->applyTextAlign('center', 'center')
    ->applyBorder(Style::BORDER_STYLE_THIN)
    ->applyRowHeight(24);

// Sets columns options - format and width (the first way)
$sheet
    ->setColFormats(['@date', '@text', '0.00'])
    ->setColWidths([12, 14, 5]);

// The seconds way to set columns options
$sheet
    // column and options
    ->setColDataStyle('A', ['format' => '@date', 'width' => 12])
    // column letter in lower case
    ->setColDataStyle('b', ['format' => '@text', 'width' => 24])
    // column can be specified by number
    ->setColDataStyle(3, ['format' => '0.00', 'width' => 15, 'color' => '#090'])
;

// The third way - all options in multilevel array (first level keys point to columns)
$sheet
    ->setColDataStyle([
        'A' => ['format' => '@date', 'width' => 12],
        'B' => ['format' => '@text', 'width' => 24],
        'C' => ['format' => '0.00', 'width' => 15, 'color' => '#090'],
    ]);

$rowNum = 1;
foreach($data as $rowData) {
    $rowOptions = [
        'height' => 20,
    ];
    if ($rowNum % 2) {
        $rowOptions['fill-color'] = '#eee';
    }
    $sheet->writeRow($rowData, $rowOptions);
}

$excel->save('simple.xlsx');

Adding Notes

There are currently two types of comments in Excel - comments and notes (see The difference between threaded comments and notes). Notes are old style comments in Excel (text on a light yellow background). You can add notes to any cells using method addNote()

$sheet1->writeCell('Text to A1');
$sheet1->addNote('A1', 'This is a note for cell A1');

$sheet1->writeCell('Text to B1')->addNote('This is a note for B1');
$sheet1->writeTo('C4', 'Text to C4')->addNote('Note for C1');

// If you specify a range of cells, then the note will be added to the left top cell
$sheet1->addNote('E4:F8', "This note\nwill added to E4");

// You can split text into multiple lines
$sheet1->addNote('D7', "Line 1\nLine 2");

You can change some note options. Allowed options of a note are:

  • width - default value is '96pt'
  • height - default value is '55.5pt'
  • fill_color - default value is '#FFFFE1'
  • show - default value is false
$sheet1->addNote('A1', 'This is a note for cell A1', ['width' => '200pt', 'height' => '100pt', 'fill_color' => '#ffcccc']);

// Parameters "width" and "height" can be numeric, by default these values are in points
// The "fill_color" parameter can be shortened
$noteStyle = [
    'width' => 200, // equivalent to '200pt'
    'height' => 100, // equivalent to '100pt'
    'fill_color' => 'fcc', // equivalent to '#ffcccc'
];
$sheet1->writeCell('Text to B1')->addNote('This is a note for B1', $noteStyle);

// This note is visible when the Excel workbook is displayed
$sheet1->addNote('C8', 'This note is always visible', ['show' => true]);

Also, you can use rich text in notes

$richText = new \avadim\FastExcelWriter\RichText('here is <c=f00>red</c> and <c=00f>blue</c> text');
$sheet1->addNote('C8', $richText);

For more information on using rich text, see here: Using Rich Text

Adding Images

// Insert an image to the cell A1
$sheet1->addImage('A1', 'path/to/file');

// Insert an image to the cell B2 and set with to 150 pixels (height will change proportionally)
$sheet1->addImage('B2', 'path/to/file', ['width' => 150]);

// Set height to 150 pixels (with will change proportionally)
$sheet1->addImage('C3', 'path/to/file', ['height' => 150]);

// Set size in pixels
$sheet1->addImage('D4', 'path/to/file', ['width' => 150, 'height' => 150]);

// Add hyperlink to the image
$sheet1->addImage('D4', 'path/to/file', ['width' => 150, 'height' => 150, 'hyperlink' => 'https://www.google.com/']);

Shared Strings

By default, strings are written directly to sheets. This increases the file size a little, but speeds up data writing and saves memory. If you want strings to be written to the shared string xml, you need to use the 'shared_string' option.

$excel = Excel::create([], ['shared_string' => true]);

FastExcelWriter vs PhpSpreadsheet

PhpSpreadsheet is a perfect library with wonderful features for reading and writing many document formats. FastExcelWriter can only write and only in XLSX format, but does it very fast and with minimal memory usage.

FastExcelWriter:

  • 7-9 times faster
  • uses less memory by 8-10 times
  • supports writing huge 100K+ row spreadsheets

Benchmark of PhpSpreadsheet (P) and FastExcelWriter (F), spreadsheet generation without styles

Rows x Cols Time P Time F Memory P Memory F
1000 x 5 0.98 sec 0.19 sec 2,048 Kb 2,048 Kb
1000 x 25 4.68 sec 1.36 sec 14,336 Kb 2,048 Kb
5000 x 25 23.19 sec 3.61 sec 77,824 Kb 2,048 Kb
10000 x 50 105.8 sec 13.02 sec 256,000 Kb 2,048 Kb

Do you want to support FastExcelWriter?

if you find this package useful you can support and donate to me for a cup of coffee:

  • USDT (TRC20) TSsUFvJehQBJCKeYgNNR1cpswY6JZnbZK7
  • USDT (ERC20) 0x5244519D65035aF868a010C2f68a086F473FC82b
  • ETH 0x5244519D65035aF868a010C2f68a086F473FC82b

Or just give me a star on GitHub :)