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('MyExcel');$excel->wirteln(['第一列','第二列','第三列']);$arrs=array();foreach($i=0;$i<3;$i++){ foreach($j=0;$j<3;$j++){ $arrs[$i][$j]=$i.'*'.$j.'='.$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!