php 导出excel(通用方法)

使用第三方类库phpspreadsheet(phpExcel已停止更新,使用phpspreadsheet)
composer require phpoffice/phpspreadsheet

定义通用方法

public function exportExcel($expTitle,$expCellName,$expTableData){
    $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
    $fileName =$xlsTitle . date('_YmdHis').'.xlsx';//or $xlsTitle 文件名称可根据自己情况设定
    $cellNum = count($expCellName);
    $dataNum = count($expTableData);
    $cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');

    $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
    $worksheet = $spreadsheet->getActiveSheet();
    $worksheet->setTitle($expTitle);//设置工作表标题名称

    //表头   设置单元格内容
    $worksheet->setCellValueByColumnAndRow(1, 1, $expTitle.date('Y-m-d H:i:s'));
    $worksheet->mergeCells('A1:'.$cellName[$cellNum-1].'1');
    for ($i = 0; $i < $cellNum; $i++){
        $worksheet->setCellValueByColumnAndRow($i+1, 2, $expCellName[$i][1]);
    }
    for ($i = 0; $i < $dataNum; $i++) {
        for ($j = 0; $j < $cellNum; $j++){
            $worksheet->setCellValueByColumnAndRow($j + 1, $i + 3, $expTableData[$i][$expCellName[$j][0]]);
        }
    }

    // 设置单元格样式
    $styleArray = [
        'font' => ['bold' => true],
        'alignment' => ['horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,],
    ];
    $worksheet->getStyle('A1')->applyFromArray($styleArray)->getFont()->setSize(14);

    $worksheet->getStyle('A2:'.$cellName[$cellNum-1].'2')->applyFromArray($styleArray)->getFont()->setSize(12);

    $styleArrayBody = [
        'borders' => [
            'allBorders' => [
                'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                'color' => ['argb' => '666666'],
            ],
        ],
        'alignment' => [
            'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
        ],
    ];
    $total_rows = $dataNum + 2;
    $worksheet->getStyle('A1:'.$cellName[$cellNum-1].$total_rows)->applyFromArray($styleArrayBody);

    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="'.$fileName.'"');
    header('Cache-Control: max-age=0');

    $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
    $writer->save('php://output');
}

方法调用

public function AccountLogExport($list)
{
    $title = '账户明细';
    $cellname = [
        ['createtime','时间'],
        ['type','收入/支出'],
        ['money','金额'],
        ['remark','备注']
    ];
    $data = [];
    foreach($list as $k=>$vo){
        $data[] = [
            'createtime' => $vo['create_time'],
            'type' => $vo['type'] == 1 ? '收入' : '支出',
            'money' => $vo['money'],
            'remark' => $vo['remark']
        ];
    }
    $this->exportExcel($title,$cellname,$data);
}

你可能感兴趣的:(php 导出excel(通用方法))