Home > php教程 > PHP源码 > PHPExcel导出excel入门实例教程

PHPExcel导出excel入门实例教程

WBOY
Release: 2016-06-08 17:19:53
Original
1052 people have browsed it

PHPExcel是一款非常强大的操作excel的插件了,我们可以使用PHPExcel插件来读写excel了,下面就一起来看看吧.

<script>ec(2);</script>

PHPExcel是一个强大的excel导出/导入插件,它甚至还可以导出pdf等,是个非常好用的php插件,php学习必不可少。那么如何使用它呢,下面是PHPExcel入门范例:

php源码参考:

require_once("PHPExcel/PHPExcel.php");
$excel = new PHPExcel(); //实例化PHPExcel类

//设置作者(非必要)
$excel->getProperties()->setCreator("Reson");

//以下是要导出的数据
$datas = array(
array(
 0 => 'UID',
 1 => '用户名',
 2 => '性别'
),
array(
 0 => '1',
 1 => 'reson',
 2 => '男'
),
array(
 0 => '2',
 1 => 'anyse',
 2 => '女'
)
);

//开始导出。注意$i要从1开始,不能从0开始,原因如下:'A'.$i,当i=1即为A1(第一行,第一个格子)
$i = 1;
foreach($datas as $key => $val){
 $excel->getActiveSheet()->setCellValue('A'.$i, $val['0']);
 $excel->getActiveSheet()->setCellValue('B'.$i, $val['1']);
 $excel->getActiveSheet()->setCellValue('C'.$i, $val['2']);
 $i++;
}

$excel->getActiveSheet()->setTitle('php学习人员名单'); //含义见下图

$fileName = 'test';
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="'.$fileName.'.xls"');
header("Content-Transfer-Encoding:binary");

$objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel5');
$objWriter->save('php://output');
?>

 

phpexcel导出excel




出后就是这个样子了,是不是非常好用呢

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template