php更新修改excel中的内容实例代码
程序员文章站
2023-12-02 16:28:58
复制代码 代码如下://模板存放目录$dir = $document_root.'/backoffice/admin/oemcheck/';$templatename =...
复制代码 代码如下:
//模板存放目录
$dir = $document_root.'/backoffice/admin/oemcheck/';
$templatename = '1.xlsx';
$outputfilename = '模板.xlsx';
$txt='test';
//实例化excel读取类
$phpreader = new phpexcel_reader_excel2007();
if(!$phpreader->canread($dir.$templatename)){
$phpreader = new phpexcel_reader_excel5();
if(!$phpreader->canread($dir.$templatename)){
echo '无法识别的excel文件!';
return false;
}
}
//读取excel
$phpexcel = $phpreader->load($dir.$templatename);
//读取工作表1
$currentsheet = $phpexcel->getsheet(0);
$currentsheet->setcellvalue('b13',iconv('gbk','utf-8',$txt));//表头赋值//
//实例化excel写入类
$phpwriter = new phpexcel_writer_excel2007($phpexcel);
ob_start();
header("content-type: application/force-download");
header("content-type: application/octet-stream");
header("content-type: application/download");
header('content-disposition:attachment;filename="' .$outputfilename. '"');//输出模板名称
header("content-transfer-encoding: binary");
header("last-modified:".gmdate("d, d m y h:i:s")." gmt");
header('pragma: public');
header('expires: 30');
header('cache-control: public');
$phpwriter->save('php://output');