<?php
/**
PHPExcel类下载:
链接:https://share.weiyun.com/SLMllhrv 密码:5h7wsh
*/
//引入Excel类,请根据自身项目路径去引入
require_once "vendor/PHPExcel.php";
require_once "vendor/PHPExcel/IOFactory.php";
$file = 'template.xlsx'; //模板文件
$fileType = pathinfo($file, PATHINFO_EXTENSION);
//根据文件类型,加载不同类库
switch ($fileType) {
case 'xls':
$type = 'Excel5';
break;
case 'xlsx':
$type = 'Excel2007';
break;
}
$objReader = \PHPExcel_IOFactory::createReader($type);
$objPHPExcel =$objReader->load($file, $encode = 'utf-8'); // 加载文件
//将活动工作表索引设置为第一个工作表:
$objPHPExcel->setActiveSheetIndex(0);
//获取活动工作表
$worksheet = $objPHPExcel->getActiveSheet();
//填充数据
$worksheet->getCell('B3')->setValue('测试');
//下载修改后的excel文件
header('pragma:public');
header('Content-type:application/vnd.ms-excel;charset=utf-8;name="test.xlsx"');
header("Content-Disposition:attachment;filename=test.xlsx");//"xls"参考下一条备注
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, $type);
//"Excel2007"生成2007版本的xlsx,"Excel5"生成2003版本的xls 调用工厂类
return $objWriter->save('php://output');
编辑前:
编辑后:
文章来源:https://blog.csdn.net/weixin_42177572/article/details/122634995