찾다
php教程PHP源码php 导出数据到excel类

将数据导出到excel当中

<?php
/**
 * 导出到excel文件(一般导出中文的都会乱码,需要进行编码转换)
 * 使用方法如下
 * $excel = new Excel();
 * $excel->addHeader(array(&#39;列1&#39;,&#39;列2&#39;,&#39;列3&#39;,&#39;列4&#39;));
 * $excel->addBody(
            array(
                array(&#39;数据1&#39;,&#39;数据2&#39;,&#39;数据3&#39;,&#39;数据4&#39;),
                array(&#39;数据1&#39;,&#39;数据2&#39;,&#39;数据3&#39;,&#39;数据4&#39;),
                array(&#39;数据1&#39;,&#39;数据2&#39;,&#39;数据3&#39;,&#39;数据4&#39;),
                array(&#39;数据1&#39;,&#39;数据2&#39;,&#39;数据3&#39;,&#39;数据4&#39;)
            )
        );
 * $excel->downLoad();
 */
class Excel{
    private $head;
    private $body;
     
    /**
     * 
     * @param type $arr 一维数组
     */
    public function addHeader($arr){
        foreach($arr as $headVal){
            $headVal = $this->charset($headVal);
            $this->head .= "{$headVal}\t ";
        }
        $this->head .= "\n";
    }
     
    /**
     * 
     * @param type $arr 二维数组
     */
    public function addBody($arr){
        foreach($arr as $arrBody){
            foreach($arrBody as $bodyVal){
                $bodyVal = $this->charset($bodyVal);
                // 过滤特殊字符
                $bodyVal = str_replace(array(&#39;\\n&#39;,&#39;\\t&#39;,&#39;<br>&#39;,&#39;<br />&#39;, &#39;<br>&#39;,&#39;</br>&#39;), "", $bodyVal);
                $bodyVal = str_replace(array("rn", "r", "n"), "", $bodyVal);
                $bodyVal =preg_replace("{\t}","",$bodyVal);
                $bodyVal=preg_replace("{\r\n}","",$bodyVal);
                $bodyVal=preg_replace("{\r}","",$bodyVal);
                $bodyVal=preg_replace("{\n}","",$bodyVal);
                $bodyVal = str_replace(",", " ",$bodyVal);
                $this->body .= "{$bodyVal}\t ";
            }
            $this->body .= "\n";
        }
    }
     
    /**
     * 下载excel文件
     */
    public function downLoad($filename=&#39;&#39;){
        if(!$filename)
            $filename = date(&#39;YmdHis&#39;,time()).&#39;.xls&#39;;
        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:attachment;filename=$filename"); 
        header("Content-Type:charset=gb2312");
        if($this->head)
            echo $this->head;
        echo $this->body;
    }
     
    /**
     * 编码转换
     * @param type $string
     * @return string
     */
    public function charset($string){
        return iconv("utf-8", "gb2312", $string);
    }
}
?>


성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

SublimeText3 Linux 새 버전

SublimeText3 Linux 새 버전

SublimeText3 Linux 최신 버전

에디트플러스 중국어 크랙 버전

에디트플러스 중국어 크랙 버전

작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

Dreamweaver Mac版

Dreamweaver Mac版

시각적 웹 개발 도구