标签搜索

导出一万行数据(包括合并行,超过26个字段)到Excel花了三秒左右时间的代码文件2

basil
2020-04-21 / 257 阅读
<?php 
/**
 * @author cxwt
 * 导出Excel订单类(包含横向合并功能)
 */
class Export_excel
{
    private $file_name;//导出的文件名
    private $output_data;//导出的数据内容
    public function __construct()
    {
        
    }
    /**
     * 设置文件名
     * @author cxwt&basil
     * @DateTime 2019-05-10T11:19:33+0800
     * @param    [type]                   $file_name [description]
     */
    public function set_file_name($file_name)
    {
        $this->file_name = $file_name;
    }
    /**
     * 设置表头
     * @author cxwt&basil
     * @DateTime 2019-05-10T17:24:40+0800
     * @param    array                   $data 表头数据(一维数组)
     */
    public function set_header($data)
    {
        array_walk($data,function(&$v,$k)
        {
            $v = "<th style='text-align:center;background-color:gray;color:#fff;font-size:15px;vnd.ms-excel.numberformat:@'>".$v."</th>";
        });
        $this->output_data .= "<tr>".join($data)."</tr>";
    }
    /**
     * 设置表格数据
     * @author cxwt&basil
     * @DateTime 2019-05-10T17:27:33+0800
     * @param    string                  $data 需要导出的数据(html)
     */
    public function set_data($data)
    {    
            $this->output_data .= $data;

        
    }
    /**
     * 下载Excel文件
     * @author cxwt&basil
     * @DateTime 2019-05-13T18:38:35+0800
     * @return   [type]                   [description]
     */
    public function download()
    {
        // Redirect output to a client’s web browser (Excel5)
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename='.$this->file_name.'_'.date('Y-m-d').'.xls');
        header('Cache-Control: max-age=0');
        // If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');

        // If you're serving to IE over SSL, then the following may be needed
        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header ('Pragma: public'); // HTTP/1.0

        $result = "<table border='1'>".$this->output_data."</table>";
echo <<< OEF
<html>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <body>
    {$result}
    </body>
</html>
OEF;
    }
}
 ?>
0