导出样式

This commit is contained in:
liu
2026-09-03 22:00:58 +08:00
parent 1370926780
commit c986c86ed5
7 changed files with 438 additions and 50 deletions
+78 -24
View File
@@ -10,18 +10,23 @@ use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\WithTitle;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
/**
* 供应商采购明细导出 · 单市场工作表
* 模板列序:品名、汇总、市场、各门店明细(门店列=该供应商有明细的门店);
* 有数据的单元格(品名/汇总/门店数量)填充突出颜色并加粗
* 汇总列(浅黄)/门店数量列(浅蓝)按值条件填色(0 不填、列头固定填色)
*/
class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison, WithStyles, WithTitle
{
/** 有数据单元格填充色(浅黄,突出显示 */
private const string DATA_FILL = 'FFFFFF99';
/** 汇总列填充色(浅黄) */
private const string QUANTITY_FILL = 'FFFFF2CC';
/** 门店数量列填充色(浅蓝) */
private const string STORE_FILL = 'FFDDEBF7';
private ?Collection $rows = null;
@@ -31,8 +36,14 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
/** 列头所在行索引 */
private int $headerRow = 1;
/** @var array<int, array<int, int>> 有数据的单元格:行索引(1 起)=> 列索引(1 起)列表 */
private array $dataCells = [];
/** 数据末行索引 */
private int $lastRow = 1;
/** @var array<int, array{quantity: int, store_quantities: array<int, int>}> 明细行索引 => 填色判断数据 */
private array $rowData = [];
/** @var array{quantity: int, stores: array<int, int>} 合计行填色判断数据 */
private array $summaryTotals = ['quantity' => 0, 'stores' => []];
/**
* @param PurchaseOrderModel $purchase 采购单
@@ -79,7 +90,7 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
$this->specialRows[++$rowIndex] = 'header';
$this->headerRow = $rowIndex;
// 明细行(有数据的单元格记录到 dataCells:品名/汇总/门店数量
// 明细行(0 数量的门店格留空不填色
$totalQuantity = 0;
$storeTotals = array_fill_keys($storeIds, 0);
foreach ($this->productRows as $productRow) {
@@ -88,17 +99,18 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
(int) $productRow['quantity'],
$this->marketLabel,
];
$filled = [1, 2, 3];
foreach ($storeIds as $position => $storeId) {
$storeQuantities = [];
foreach ($storeIds as $storeId) {
$quantity = (int) ($productRow['store_quantities'][$storeId] ?? 0);
$line[] = $quantity > 0 ? $quantity : '';
if ($quantity > 0) {
$filled[] = 4 + $position;
$storeTotals[$storeId] += $quantity;
}
$storeQuantities[$storeId] = $quantity;
$storeTotals[$storeId] += $quantity;
}
$rows[] = $line;
$this->dataCells[++$rowIndex] = $filled;
$this->rowData[++$rowIndex] = [
'quantity' => (int) $productRow['quantity'],
'store_quantities' => $storeQuantities,
];
$totalQuantity += (int) $productRow['quantity'];
}
@@ -108,6 +120,8 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
array_map(static fn (int $storeId): int => $storeTotals[$storeId], $storeIds),
);
$this->specialRows[++$rowIndex] = 'summary';
$this->lastRow = $rowIndex;
$this->summaryTotals = ['quantity' => $totalQuantity, 'stores' => $storeTotals];
return $this->rows = collect($rows);
}
@@ -118,7 +132,8 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
}
/**
* 标题/列头/合计加粗,有数据的单元格填充突出颜色,冻结列头
* 标题/列头/合计加粗;全表居中 + 全边框;
* 汇总列(浅黄)/门店数量列(浅蓝)按值条件填色(0 不填、列头固定填色),冻结列头
*/
public function styles(Worksheet $sheet): array
{
@@ -130,19 +145,46 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
foreach ($widths as $index => $width) {
$sheet->getColumnDimension(Coordinate::stringFromColumnIndex($index + 1))->setWidth($width);
}
$storeCount = count($this->stores);
for ($i = 0; $i < $storeCount; $i++) {
$storeIds = array_map('intval', array_keys($this->stores));
$lastColumn = Coordinate::stringFromColumnIndex(3 + max(count($storeIds), 1));
foreach ($storeIds as $i => $storeId) {
$sheet->getColumnDimension(Coordinate::stringFromColumnIndex(4 + $i))->setWidth(12);
}
// 有数据的单元格:填充突出颜色并加粗(含品名/汇总/门店数量)
foreach ($this->dataCells as $rowIndex => $columns) {
foreach ($columns as $columnIndex) {
$style = $sheet->getStyle(Coordinate::stringFromColumnIndex($columnIndex) . $rowIndex);
$style->getFill()
->setFillType(Fill::FILL_SOLID)
->getStartColor()->setARGB(self::DATA_FILL);
$style->getFont()->setBold(true);
// 全部单元格水平/垂直居中
$sheet->getStyle('A1:' . $lastColumn . $this->lastRow)
->getAlignment()
->setHorizontal(Alignment::HORIZONTAL_CENTER)
->setVertical(Alignment::VERTICAL_CENTER);
// 表格区域(列头 → 合计行)添加所有边框
$sheet->getStyle('A' . $this->headerRow . ':' . $lastColumn . $this->lastRow)
->getBorders()
->getAllBorders()
->setBorderStyle(Border::BORDER_THIN);
// 汇总列(B):有数据浅黄,0 无背景,列头固定浅黄
$this->fillCell($sheet, 'B' . $this->headerRow, self::QUANTITY_FILL);
foreach ($this->rowData as $row => $data) {
if ($data['quantity'] > 0) {
$this->fillCell($sheet, 'B' . $row, self::QUANTITY_FILL);
}
}
if ($this->summaryTotals['quantity'] > 0) {
$this->fillCell($sheet, 'B' . $this->lastRow, self::QUANTITY_FILL);
}
// 门店数量列(D 起):有数据浅蓝,0 无背景,列头固定浅蓝
foreach ($storeIds as $i => $storeId) {
$column = Coordinate::stringFromColumnIndex(4 + $i);
$this->fillCell($sheet, $column . $this->headerRow, self::STORE_FILL);
foreach ($this->rowData as $row => $data) {
if (($data['store_quantities'][$storeId] ?? 0) > 0) {
$this->fillCell($sheet, $column . $row, self::STORE_FILL);
}
}
if (($this->summaryTotals['stores'][$storeId] ?? 0) > 0) {
$this->fillCell($sheet, $column . $this->lastRow, self::STORE_FILL);
}
}
@@ -160,4 +202,16 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
return $styles;
}
/**
* 单元格纯色填充
*/
private function fillCell(Worksheet $sheet, string $coordinate, string $argb): void
{
$sheet->getStyle($coordinate)
->getFill()
->setFillType(Fill::FILL_SOLID)
->getStartColor()
->setARGB($argb);
}
}