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

PHP implements direct generation of Excel document code

小云云
Release: 2023-03-22 09:38:01
Original
1523 people have browsed it

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);
        }
    }
}
Copy after login

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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template