最终效果
核心代码
<?php
use PhpOffice\PhpSpreadsheet\Cell\DataValidation;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\NamedRange;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
?>
class Excel extends Controller
{
public function exportselectexcel()
{
$header = [
'A1' => '门店',
'B1' => '公寓',
'C1' => '门牌号',
'D1' => '记账类型',
'E1' => '记账项目',
'F1' => '支出类型',
'G1' => '费用分类',
'H1' => '金额',
'I1' => '记账日期',
'J1' => '收款人',
'K1' => '收款人帐号',
'L1' => '开户支行',
'M1' => '备注'
];
$oneData = [
[
'id' => 1,
'title' => '我是A',
'children' =>
[
[
'id' => 2,
'title' => '我是A的下级A1',
'children' =>
[
[
'id' => 3,
'title' => '我是A1的下级A11'
],
[
'id' => 4,
'title' => '我是A1的下级A12'
]
]
],
[
'id' => 5,
'title' => '我是A的下级A2',
'children' =>
[
[
'id' => 6,
'title' => '我是A2的下级A21'
],
[
'id' => 7,
'title' => '我是A2的下级A22'
]
]
]
]
],
[
'id' => 1,
'title' => '我是B',
'children' =>
[
[
'id' => 2,
'title' => '我是B的下级B1',
'children' =>
[
[
'id' => 3,
'title' => '我是B1的下级B11'
],
[
'id' => 4,
'title' => '我是B1的下级B12'
]
]
],
[
'id' => 5,
'title' => '我是B的下级B2',
'children' =>
[
[
'id' => 6,
'title' => '我是B2的下级B21'
],
[
'id' => 7,
'title' => '我是B2的下级B22'
]
]
]
]
],
];
$keepType = '支出';
$spreadsheet = new Spreadsheet();
$sheetMain = $spreadsheet->getsheet(0);
$sheetMain->setTitle('Excel导出下拉框-示例');
$sheetMain->getPageSetup()->setHorizontalCentered(true);
$sheetMain->getPageSetup()->setVerticalCentered(false);
foreach ($header as $key => $value) {
$sheetMain->setCellValue($key, $value);
}
$sheetOne = $spreadsheet->createSheet(1);
$sheetTwo = $spreadsheet->createSheet(2);
$sheetTwoCol = 0;
foreach ($oneData as $key => $value) {
$row = 1;
$sheetOne->setCellValue(Coordinate::stringFromColumnIndex(bcadd($key, 1, 0)) . $row, $value['title']);
if (!empty($value['children'])) {
foreach ($value['children'] as $value2) {
$row2 = 1;
$sheetOne->setCellValue(Coordinate::stringFromColumnIndex(bcadd($key, 1, 0)) . ++$row, $value2['title']);
$sheetTwo->setCellValue(Coordinate::stringFromColumnIndex(bcadd($sheetTwoCol, 1, 0)) . $row2, $value2['title']);
if (!empty($value2['children'])) {
foreach ($value2['children'] as $value3) {
$sheetTwo->setCellValue(Coordinate::stringFromColumnIndex(bcadd($sheetTwoCol, 1, 0)) . ++$row2, $value3['title']);
}
$spreadsheet->addNamedRange(new NamedRange($value2['title'], $sheetTwo, Coordinate::stringFromColumnIndex(bcadd($sheetTwoCol, 1, 0)) . '2:' . Coordinate::stringFromColumnIndex(bcadd($sheetTwoCol, 1, 0)) . $row2));
}
$sheetTwoCol++;
}
$spreadsheet->addNamedRange(new NamedRange($value['title'], $sheetOne, Coordinate::stringFromColumnIndex(bcadd($key, 1, 0)) . '2:' . Coordinate::stringFromColumnIndex(bcadd($key, 1, 0)) . $row));
}
}
$spreadsheet->addNamedRange(new NamedRange('oneData', $sheetOne, 'A1:' . Coordinate::stringFromColumnIndex(bcadd(count($oneData), 1, 0)) . '1'));
$sheetOne->setSheetState(Worksheet::SHEETSTATE_HIDDEN);
$sheetTwo->setSheetState(Worksheet::SHEETSTATE_HIDDEN);
$maxRows = 5;
foreach (range(2, bcadd($maxRows, 1, 0)) as $row) {
$this->setValidation($sheetMain, "A${row}", '=oneData');
$this->setValidation($sheetMain, "B${row}", "=INDIRECT(A${row})");
$this->setValidation($sheetMain, "C${row}", "=INDIRECT(B${row})");
$this->setValidation($sheetMain, "D${row}", '"' . $keepType . '"');
$this->setInputRule($sheetMain, "H${row}", DataValidation::TYPE_DECIMAL);
$this->setInputRule($sheetMain, "I${row}", DataValidation::TYPE_TIME);
}
$styleArray = ['font' => ['bold' => true], 'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER]];
$sheetMain->getStyle('A1:M1')->applyFromArray($styleArray);
$sheetMain->getColumnDimension('A')->setWidth(25);
$sheetMain->getColumnDimension('B')->setWidth(25);
$sheetMain->getColumnDimension('C')->setWidth(25);
$sheetMain->getColumnDimension('D')->setWidth(20);
$sheetMain->getColumnDimension('E')->setWidth(25);
$sheetMain->getColumnDimension('F')->setWidth(25);
$sheetMain->getColumnDimension('G')->setWidth(25);
$sheetMain->getColumnDimension('H')->setWidth(20);
$sheetMain->getColumnDimension('I')->setWidth(20);
$sheetMain->getColumnDimension('J')->setWidth(18);
$sheetMain->getColumnDimension('K')->setWidth(22);
$sheetMain->getColumnDimension('L')->setWidth(25);
$sheetMain->getColumnDimension('M')->setWidth(30);
$filename = date('YmdHis') . 'Excel下拉框-示例.xlsx';
ob_end_clean();
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $filename . '"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: cache, must-revalidate');
header('Pragma: public');
$writer = IOFactory ::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
exit;
}
private function setValidation(Worksheet $sheet, $cellPoint, $format)
{
$validation = $sheet->getCell($cellPoint)->getDataValidation();
$validation->setType(DataValidation::TYPE_LIST);
$validation->setErrorStyle(DataValidation::STYLE_INFORMATION);
$validation->setAllowBlank(false);
$validation->setShowInputMessage(true);
$validation->setShowErrorMessage(true);
$validation->setShowDropDown(true);
$validation->setErrorTitle('输出错误');
$validation->setError('值不在列表中');
$validation->setPromptTitle('请选择');
$validation->setPrompt('请从列表中选择一个值');
$validation->setFormula1($format);
}
private function setInputRule(Worksheet $sheet, $cellPoint, $type)
{
$validation = $sheet->getCell($cellPoint)->getDataValidation();
$validation->setType($type);
$validation->setErrorStyle(DataValidation::STYLE_INFORMATION);
$validation->setAllowBlank(false);
$validation->setShowInputMessage(true);
$validation->setShowErrorMessage(true);
$validation->setErrorTitle('输入格式有误');
}
}
|