Home>Article>Backend Development> The principle of php exporting excel table

The principle of php exporting excel table

王林
王林 Original
2019-09-06 14:11:10 4026browse

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('1','小王','男','20','100'), array('2','小李','男','20','101'), array('3','小张','女','20','102'), array('4','小赵','女','20','103') ); //填充表格信息 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('Content-Disposition:attachment;filename="testdata.xls"'); header("Content-Transfer-Encoding:binary"); $write->save('php://output');

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('学号','姓名','性别','年龄','班级'); $converter = function($value) { return iconv('utf-8', 'gbk', $value); }; $header = array_map($converter, $header); $list = array( array('1','小王','男','20','100'), array('2','小李','男','20','101'), array('3','小张','女','20','102'), array('4','小赵','女','20','103') ); 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