欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php读取excel文件示例分享(更新修改excel)

程序员文章站 2023-08-15 20:04:51
复制代码 代码如下://模板存放目录$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');