PhpExcel 自定义表头导出

话不多说,直接介绍上程序:

只符合使用PhpExcel 使用。

function export_excel($fileName = 'Excel', $headerArr = array(), $data = array(), $sheet = 'Sheet1')
{
    // 引入phpexcel核心类文件
    require_once '../includes/phpexcel/Classes/PHPExcel.php';
    // 实例化excel类
    $objPHPExcel = new PHPExcel();
    // 操作第一个工作表
    $objPHPExcel->setActiveSheetIndex(0);

    // 设置sheet名
    $title='客户信息表';
    $objPHPExcel->getActiveSheet()->setTitle("$title");
    // 表头
    $letter = 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',
'BA','BB','BC','BD','BE','BF','BG','BH','BI','BJ','BK','BL','BM','BN','BO','BP','BQ','BR','BS','BT','BU','BV','BW','BX','BY','BZ',
'CA','CB','CC','CD','CE','CF','CG','CH','CI','CJ','CK','CL','CM','CN','CO','CP','CQ','CR','CS','CT','CU','CV','CW','CX','CY','CZ'
);
    for($i = 0;$i < count($headerArr);$i++)
    {
        $key_value = $headerArr[$i]["value"];
        $objPHPExcel->getActiveSheet(0)->setCellValue("$letter[$i]1","$key_value");
    }

    $number = 2;
    for($i = 0;$i < count($data);$i++)
    {
        for($j=0;$jgetActiveSheet(0)->setCellValue("$letter[$j]$number","$value");
        }
        $number++;
    }
    $outputFileName = $title . '.xls';
    $xlsWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header('Content-Disposition:inline;filename="' . $outputFileName . '"');
    header("Content-Transfer-Encoding: binary");
    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: must-revalidate, post-check=0, pre-check=0");
    header("Pragma: no-cache");
    $xlsWriter->save("php://output");
    echo file_get_contents($outputFileName);exit;
}

若使用过程中出现其他错误,可调试测试。写的这个方式若有错误之处请见谅。

你可能感兴趣的:(php)