Home >Backend Development >PHP Problem >The principle of php exporting excel table

The principle of php exporting excel table

王林
王林Original
2019-09-06 14:11:104147browse

The principle of php exporting excel table

Before understanding how PHP exports excel tables, let’s first understand the essence of excel.

excel is divided into two major versions: excel2007 (suffix. Compress the document. excel2003: essentially a binary file.

After understanding excel, let’s introduce the principle of exporting excel from PHP:

For excel2007, since its essence is a collection document of xml, the export process is The process of parsing xml; for excel2003, since its essence is a binary file, the process of exporting excel will first open the binary file, then read the internal information, and convert the internal information into identifiable content.

Two export methods:

Method 1: Use PHPExcel class library

//引入PHPExcel库文件(路径根据自己情况)
include './phpexcel/Classes/PHPExcel.php';
$excel = new PHPExcel(); //创建对象
$letter = array('A','B','C','D','E','F','F','G'); //Excel表格式,这里简略写了8列
$tableheader = array('学号','姓名','性别','年龄','班级');//表头数组
//填充表头信息
for($i = 0;$i < count($tableheader);$i++) {
$excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
}
$data = array(
array(&#39;1&#39;,&#39;小王&#39;,&#39;男&#39;,&#39;20&#39;,&#39;100&#39;),
array(&#39;2&#39;,&#39;小李&#39;,&#39;男&#39;,&#39;20&#39;,&#39;101&#39;),
array(&#39;3&#39;,&#39;小张&#39;,&#39;女&#39;,&#39;20&#39;,&#39;102&#39;),
array(&#39;4&#39;,&#39;小赵&#39;,&#39;女&#39;,&#39;20&#39;,&#39;103&#39;)
);
//填充表格信息
for ($i = 2;$i <= count($data) + 1;$i++) {
$j = 0;
foreach ($data[$i - 2] as $key=>$value) {
$excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value");
$j++;
}
}
$write = new PHPExcel_Writer_Excel5($excel);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");;
header(&#39;Content-Disposition:attachment;filename="testdata.xls"&#39;);
header("Content-Transfer-Encoding:binary");
$write->save(&#39;php://output&#39;);

Method 2: Simple PHP export excel, does not apply to any external class library file

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=reply.csv");
header("Pragma: no-cache");
header("Expires: 0");
$output = fopen("php://output", "w");
$header = array(&#39;学号&#39;,&#39;姓名&#39;,&#39;性别&#39;,&#39;年龄&#39;,&#39;班级&#39;);
$converter = function($value) {
return iconv(&#39;utf-8&#39;, &#39;gbk&#39;, $value);
};
$header = array_map($converter, $header);
$list = array(
array(&#39;1&#39;,&#39;小王&#39;,&#39;男&#39;,&#39;20&#39;,&#39;100&#39;),
array(&#39;2&#39;,&#39;小李&#39;,&#39;男&#39;,&#39;20&#39;,&#39;101&#39;),
array(&#39;3&#39;,&#39;小张&#39;,&#39;女&#39;,&#39;20&#39;,&#39;102&#39;),
array(&#39;4&#39;,&#39;小赵&#39;,&#39;女&#39;,&#39;20&#39;,&#39;103&#39;)
);
fputcsv($output, $header);
foreach($list as $k => $v)
{
$csvrow = array_map($converter, array(
$v[0],
$v[1],
$v[2],
$v[3],
$v[4],

));
fputcsv($output, $csvrow);
}
fclose($output);

The above content is for reference only!

If you want to know more related content, please visit the PHP Chinese website: PHP Video Tutorial

The above is the detailed content of The principle of php exporting excel table. 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