Update 12-export.md

This commit is contained in:
Dan Harrin
2025-06-02 13:40:13 +01:00
parent 476b92c7f1
commit d8c69cc5b5
+22 -11
View File
@@ -656,10 +656,10 @@ public function makeXlsxRow(array $values, ?Style $style = null): Row
}
```
You can replace `Row::fromValues()` with `Row::fromValuesWithStyles()`, which allows you to pass an array of styles for each cell in the row:
When a user exports, they can choose which columns to export. As such, the `$this->columnMap` property may be used to determine which columns are being exported and in which order. You can replace `Row::fromValues()` with an array of `Cell` objects, which allow you to style them individually using [OpenSpout `Style` objects](https://github.com/openspout/openspout/blob/4.x/docs/documentation.md#styling). A `StyleMerger` can be used to merge the default style with the custom style for a cell, allowing you to apply additional styles on top of the default ones:
```php
use OpenSpout\Common\Entity\Row;
use OpenSpout\Common\Entity\Cell;use OpenSpout\Common\Entity\Row;
use OpenSpout\Common\Entity\Style\Style;
use OpenSpout\Writer\Common\Manager\Style\StyleMerger;
@@ -670,18 +670,29 @@ public function makeXlsxRow(array $values, ?Style $style = null): Row
{
$styleMerger = new StyleMerger();
return Row::fromValuesWithStyles($values, $style, [
2 => (new Style())->setFontUnderline(),
3 => $styleMerger->merge(
(new Style())->setFontSize(16),
$style,
),
]);
$cells = [];
foreach (array_keys($this->columnMap) as $columnIndex => $column) {
$cells[] = match ($column) {
'name' => Cell::fromValue(
$values[$columnIndex],
$styleMerger->merge(
(new Style())->setFontUnderline(),
$style,
),
),
'price' => Cell::fromValue(
$values[$columnIndex],
(new Style())->setFontSize(12),
),
default => Cell::fromValue($values[$columnIndex]),
},
}
return new Row($cells, $style);
}
```
An [OpenSpout `Style` object](https://github.com/openspout/openspout/blob/4.x/docs/documentation.md#styling) is used to define the style for each cell. The column index should index the array of styles. A `StyleMerger` can be used to merge the default style with the custom style for a cell, allowing you to apply additional styles on top of the default ones.
### Customizing the XLSX writer
If you want to pass "options" to the [OpenSpout XLSX `Writer`](https://github.com/openspout/openspout/blob/4.x/docs/documentation.md#column-widths), you can return an `OpenSpout\Writer\XLSX\Options` instance from the `getXlsxWriterOptions()` method of the exporter class: