44 lines
1.9 KiB
PHP
44 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Business;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Style\Fill;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
|
|
|
class StoreReceivingAccountTemplate
|
|
{
|
|
public function download(string $tab): StreamedResponse
|
|
{
|
|
$headers = StoreReceivingAccountWorkbookImporter::headers()[$tab] ?? null;
|
|
abort_if($headers === null, 404);
|
|
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$sheet->setTitle(mb_substr(StoreReceivingAccountWorkbookImporter::sheetTitle($tab), 0, 31));
|
|
$lastColumn = Coordinate::stringFromColumnIndex(count($headers));
|
|
$sheet->freezePane('A2');
|
|
$sheet->setAutoFilter("A1:{$lastColumn}1");
|
|
|
|
foreach ($headers as $index => $header) {
|
|
$column = Coordinate::stringFromColumnIndex($index + 1);
|
|
$sheet->setCellValue("{$column}1", $header);
|
|
$sheet->getColumnDimension($column)->setWidth(max(16, min(28, mb_strlen($header) + 8)));
|
|
}
|
|
|
|
$sheet->getRowDimension(1)->setRowHeight(26);
|
|
$sheet->getStyle("A1:{$lastColumn}1")->getFont()->setBold(true)->getColor()->setARGB('FFFFFFFF');
|
|
$sheet->getStyle("A1:{$lastColumn}1")->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setARGB('FF856632');
|
|
|
|
return response()->streamDownload(function () use ($spreadsheet): void {
|
|
(new Xlsx($spreadsheet))->save('php://output');
|
|
$spreadsheet->disconnectWorksheets();
|
|
}, StoreReceivingAccountWorkbookImporter::sheetTitle($tab) . '-店铺收款账户模板.xlsx', [
|
|
'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
'Cache-Control' => 'no-store, no-cache, must-revalidate',
|
|
]);
|
|
}
|
|
}
|