Backend Development 5 min read

Using Lysice XlsWriter to Add Charts, Cell Styles, and Worksheet Settings in PHP

This guide explains how to implement the WithCharts contract to configure various chart types, apply default and column‑level cell styles, set worksheet zoom and grid‑line options, and use the WithFilter interface for range filtering when generating Excel files with the Lysice XlsWriter PHP library.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Using Lysice XlsWriter to Add Charts, Cell Styles, and Worksheet Settings in PHP

The chart types supported by Lysice XlsWriter are defined in Lysice\XlsWriter\Supports\Chart with a Chart_ prefix. To add charts to a document, your export class must implement the WithCharts contract and provide a charts() method.

Example export class:

class ChartsExport implements FromArray, WithCharts {
    // implementation details
}

Typical chart configuration (area, histogram, bar, line, doughnut, radar, etc.) looks like this:

public function charts(){
    return [
        [
            'type'   => Constants::CHART_AREA_PERCENT,
            'title'  => 'Chart Title',
            'style'  => 47,
            'xName'  => 'X name',
            'yName'  => 'Y name',
            'row'    => 0,
            'column' => 3,
            'series' => [
                ['name' => 'name1', 'data' => '=Sheet1!$B$1:$B$6'],
                ['name' => 'name2', 'data' => '=Sheet1!$A$1:$A$6'],
                ['name' => 'name3', 'data' => '=Sheet1!$D$1:$D$6'],
                ['name' => 'name4', 'data' => '=Sheet1!$C$1:$C$6']
            ]
        ]
    ];
}

For a pie chart, only the first series element is used, so a single array of data is sufficient, and you can also assign categories by specifying the cells that contain them.

Automatic filtering is achieved by implementing the WithFilter interface and returning the cell range to filter:

public function filter(): string {
    return 'A1:D1';
}

Cell styling can be defined globally via a defaultFormats() method that returns an array of DefaultFormat objects. The first format applies to the header when two formats are returned; the second applies to the body.

public function defaultFormats(): array {
    $formatOne = DefaultFormat::create()->border(12)->background(Constants::COLOR_MAGENTA);
    $formatTwo = DefaultFormat::create()->underline(Constants::UNDERLINE_SINGLE)->wrap();
    return [$formatOne, $formatTwo];
}

Column‑level style constants are available in \Lysice\XlsWriter\Supports\Constants , including border styles (e.g., Constants::BORDER_THIN , Constants::BORDER_DASHED ), alignment options ( Constants::ALIGN_LEFT , Constants::ALIGN_CENTER , etc.), font settings ( font($fontName) , fontColor($color) ), background colors, number formats, underline, wrap, and strikeout.

Worksheet zoom can be set by implementing the WithZoom interface; the returned integer must be between 10 and 400:

public function zoom(){
    return 300;
}

Grid‑line visibility is controlled via the WithGridLine interface:

public function gridLine(){
    return Constants::GRIDLINES_HIDE_ALL;
}

For complete implementation details, refer to the official documentation at https://xlswriter-docs.viest.me/zh-cn . The article concludes with a friendly reminder to like and share if the content was helpful.

PHPExcelChartxlswriterCell StylingLysiceWorksheet
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.