php如何导出excel的方法
时间:2023-06-25 19:29:07
在Excel中录入数据的时候,有很多情况都是数据源在PHP上,这个时候只要用到导出功能就可以省下录入的时间了。接下来是小编为大家带来的php如何导出excel的方法,供大家参考。
php如何导出excel的方法:
导出Excel步骤1:百度:phpexcel,结果如图所示,点击第一个结果;
导出Excel步骤2:进入官网后,找到右边的download按钮,下载,下载完成的是一个压缩文件,解压放到你的项目目录里,根据个人情况而定;
导出Excel步骤3:因为这里给大家做演示,所以建了一个测试文件,有点基础的都能明白是怎么回事,下面进入代码;
导出Excel步骤4://引入PHPExcel库文件(路径根据自己情况)
include './phpexcel/Classes/PHPExcel.php';
//创建对象
$excel = new PHPExcel();
//Excel表格式,这里简略写了8列
$letter = array('A','B','C','D','E','F','F','G');
//表头数组
$tableheader = array('学号','姓名','性别','年龄','班级');
//填充表头信息
for($i = 0;$i < count($tableheader);$i++) {
$excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
}
导出Excel步骤5://表格数组
$data = array(
array('1','小王','男','20','100'),
array('2','小李','男','20','101'),
array('3','小张','女','20','102'),
array('4','小赵','女','20','103')
);
导出Excel步骤6://填充表格信息
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++;
}
}
导出Excel步骤7://创建Excel输入对象
$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');
导出Excel步骤8:打开页面,刷新的时候会弹出对话框,让你选择文件保存路径和文件名称,我直接放在了桌面上,如图所示;
导出Excel步骤9:打开表格后,数据和格式跟代码中的一致,说明PHP导出的Excel是正确的。如果出现错误,检查一下你的表格数组和数据数组吧。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
psd文件怎么转换成jpg
![](https://img.aspxhome.com/file/2023/9/a341794_0s.jpg)
电脑可以连接5G WiFi吗?电脑连接5G WiFi的方法图文教学分享
![](https://img.aspxhome.com/file/2023/6/a320959_0s.png)
Win7共享按钮灰色怎么办?Win7系统共享文件的方法
![](https://img.aspxhome.com/file/2023/28/a243477_0s.jpg)
服务器上创建本地用户账户lichimhui的操作步骤
![](https://img.aspxhome.com/file/2023/29/a255959_0s.jpg)
Win10电脑如何关闭系统自动更新功能?
![](https://img.aspxhome.com/file/2023/1/a279584_0s.jpg)
Word页码变成PAGE等英文乱码该怎么解决?
![](https://img.aspxhome.com/file/2023/1/24741_0s.jpg)
Win10更新驱动后黑屏怎么办?Win10更新驱动黑屏后解决教程
![](https://img.aspxhome.com/file/2023/3/52283_0s.png)
Win10桌面图标排列混乱怎么解决?
![](https://img.aspxhome.com/file/2023/26/a226301_0s.jpg)
Word表格中自动求和 word求平均值详解
![](https://img.aspxhome.com/file/2023/2/33802_0s.jpg)
Win10利用oh-my-posh美化PowerShell的方法
![](https://img.aspxhome.com/file/2023/26/a225220_0s.jpg)
Win10 20H2开机微软标不停转圈怎么处理?
![](https://img.aspxhome.com/file/2023/26/a223659_0s.jpg)
Win10电脑怎么查看配置?Win10电脑查看配置的方法
![](https://img.aspxhome.com/file/2023/26/a220451_0s.png)
如何从 FaceTime 视频切换到 FaceTime 音频?
![](https://img.aspxhome.com/file/2023/3/a195623_0s.png)
word如何批量导出图片?
![](https://img.aspxhome.com/file/2023/1/34811_0s.gif)
怎么检查Windows11中的新更新?
![](https://img.aspxhome.com/file/2023/27/a237222_0s.jpg)
电脑重装XP系统后一直出现黑屏的解决办法
![](https://img.aspxhome.com/file/2023/2/a285086_0s.jpg)
Win7默认共享怎么关闭?Win7关闭默认共享的方法
![](https://img.aspxhome.com/file/2023/1/a280683_0s.jpg)
Mac磁盘工具---Disk Xray三大功能使用指南
![](https://img.aspxhome.com/file/2023/2/a209712_0s.jpeg)
Excel中用自定义序列排序实现教程
![](https://img.aspxhome.com/file/2023/0/a156630_0s.jpg)