Home  >  Article  >  Backend Development  >  PHP implements direct generation of Excel document code

PHP implements direct generation of Excel document code

小云云
小云云Original
2018-03-26 09:55:111497browse

This article mainly shares with you how to directly generate Excel documents in PHP, mainly in the form of code. I hope it can help you.

Excel class source code:

class Excel{
    function __construct($filename){
        header('Content-Type:application/vnd.ms-excel');
        header('Content-Disposition:attachment;filename='.iconv('utf-8', 'gb2312//IGNORE', $filename).'.xls');        /*gb2312后加“//IGNORE”以防止转码故障后停止运行。下同*/
    }    function writeln($row){
        foreach($row as $col){            echo iconv('utf-8', 'gb2312//IGNORE', $col."\t");//此处使用双引号
        }
    }    function write($rows){
        foreach($rows as $row){            $this->writeln($row);
        }
    }
}

Usage:

$excel=new Excel(&#39;MyExcel&#39;);$excel->wirteln([&#39;第一列&#39;,&#39;第二列&#39;,&#39;第三列&#39;]);$arrs=array();foreach($i=0;$i<3;$i++){    foreach($j=0;$j<3;$j++){        $arrs[$i][$j]=$i.&#39;*&#39;.$j.&#39;=&#39;.$i*$j;
    }
}$excel->write($arrs);

Related recommendations:

How to use php to generate EXCEL documents

php generates an EXCEL document example program_PHP tutorial

php generates an EXCEL document example program

The above is the detailed content of PHP implements direct generation of Excel document code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to use php generatorNext article:How to use php generator