导出csv文件

public function putHeaderCsv($csvFileName, $dataArr ,$headerText = []){
        $csv_data = '';

        foreach ($headerText as $key => $value) {
            $csv_data .= $headerText[$key].',';
        }
        $csv_data .= PHP_EOL;


        foreach ($dataArr as $key => $value) {
            foreach ($value as $k => $v) {
                $csv_data .= $value[$k].',';
            }
            $csv_data .= PHP_EOL;
        }

        $csv_data = helper::convertEncoding($csv_data, 'utf-8', 'utf-8//TRANSLIT');
        $this->sendDownHeader($csvFileName, 'csv', $csv_data);
    }
public function sendDownHeader($fileName, $fileType, $content)
    {
        /* Clean the ob content to make sure no space or utf-8 bom output. */
        $obLevel = ob_get_level();
        for($i = 0; $i < $obLevel; $i++) ob_end_clean();

        /* Set the downloading cookie, thus the export form page can use it to judge whether to close the window or not. */
        setcookie('downloading', 1);

        /* Append the extension name auto. */
        $extension = '.' . $fileType;
        if(strpos($fileName, $extension) === false) $fileName .= $extension;

        /* urlencode the filename for ie. */
        if(strpos($this->server->http_user_agent, 'MSIE') !== false or strpos($this->server->http_user_agent, 'Trident') !== false) $fileName = urlencode($fileName);

        /* Judge the content type. */
        $mimes = $this->config->file->mimes;
        $contentType = isset($mimes[$fileType]) ? $mimes[$fileType] : $mimes['default'];

        header("Content-type: $contentType");
        header("Content-Disposition: attachment; filename=\"$fileName\"");
        header("Pragma: no-cache");
        header("Expires: 0");
        die($content);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值