萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> php更新修改excel中的內容例子

php更新修改excel中的內容例子

  //模板存放目錄
$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');

copyright © 萬盛學電腦網 all rights reserved