Skip to content

Commit

Permalink
Fix:公式表头
Browse files Browse the repository at this point in the history
  • Loading branch information
ogenes committed Dec 12, 2023
1 parent 0758e82 commit e41f346
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
49 changes: 49 additions & 0 deletions example/FormulaExample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

use Ogenes\Exceler\ExportClient;

require "vendor/autoload.php";

/**
* Created by exceler.
* User: Ogenes
* Date: 2023/12/12
*/
class FormulaExample
{
/**
* @throws \PhpOffice\PhpSpreadsheet\Exception
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/
public function run(): string
{
$data['sheet1'] = [
['goodsName' => '半裙', 'price' => 1490, 'actualStock' => 2],
['goodsName' => '半裙', 'price' => 1590, 'actualStock' => 1]
];

$config['sheet1'] = [
['bindKey' => 'goodsName', 'columnName' => '商品名称'],
['bindKey' => '={price}*{actualStock}', 'columnName' => '库存额'], //会自动转化为公式,所在行的 price * actualStock
['bindKey' => 'price', 'columnName' => '售价'],
['bindKey' => 'actualStock', 'columnName' => '实际库存'],
];
$client = ExportClient::getInstance();
$client->setStyleWidth(50);
$client->setStyleUnit('');
return $client->setFilepath(__DIR__ . '/file/' . date('Y/m/d/'))
->setFilename('cellDemo' . date('His'))
->setData($data)
->setConfig($config)
->export();
}
}


try {
$filepath = (new CellExample())->run();
print_r($filepath);
} catch (\Exception $e) {
print_r($e->getMessage());
}
echo PHP_EOL;
11 changes: 6 additions & 5 deletions src/ExportClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ExportClient extends ExportService
{
protected $config = [];
protected $data = [];
protected $columnIndexMap = [];

/**
* @desc 请指定导出配置
Expand Down Expand Up @@ -143,6 +144,7 @@ public function output()

$writer = IOFactory::createWriter($excel, 'Xlsx');
$writer->save('php://output');
$this->columnIndexMap = [];
}

/**
Expand Down Expand Up @@ -186,6 +188,7 @@ protected function formatHeader(Spreadsheet $excel): array
->setAutoSize(true)
->setWidth($this->width, $this->unit);
}
$this->columnIndexMap[$columnItem['bindKey']] = $columnIndex;
$columnIndex++;
}
$excel->createSheet();
Expand Down Expand Up @@ -221,7 +224,6 @@ protected function formatContent(Spreadsheet $excel, array $sheetIndexMap)
$rowIndex = 2;
$maxColumn = 'A';
$maxRow = 2;
$columnMap = [];
$sheetData = $this->getImageArr($sheetData, $sheetConfig);
foreach ($sheetData as $row) {
$columnIndex = 'A';
Expand All @@ -239,10 +241,10 @@ protected function formatContent(Spreadsheet $excel, array $sheetIndexMap)
$text = str_replace(
array_map(static function ($key) {
return "{{$key}}";
}, array_keys($columnMap)),
}, array_keys($this->columnIndexMap)),
array_map(static function ($val) use ($rowIndex) {
return "{$val}{$rowIndex}";
}, $columnMap),
}, $this->columnIndexMap),
$item['bindKey']
);
} else {
Expand Down Expand Up @@ -271,7 +273,6 @@ protected function formatContent(Spreadsheet $excel, array $sheetIndexMap)
$hyperlink && $sheet->getCell($columnIndex . $rowIndex)->getHyperlink()->setUrl($hyperlink);
$comment && $sheet->getComment($columnIndex . $rowIndex)->getText()->createTextRun($comment);
$maxColumn = $columnIndex;
$columnMap[$item['bindKey']] = $columnIndex;
$columnIndex++;
}
$maxRow = $rowIndex;
Expand Down Expand Up @@ -355,4 +356,4 @@ protected function getImageArr(array $sheetData, array $sheetConfig): array
return $sheetData;
}

}
}

0 comments on commit e41f346

Please sign in to comment.