php Read excel File Example Share of Update Modify excel
- 2021-01-19 22:03:39
- OfStack
// Template storage directory
$dir = $DOCUMENT_ROOT.'/backoffice/admin/oemcheck/';
$templateName = '1.xlsx';
$outputFileName = ' The template .xlsx';
$txt='test';
// instantiation Excel Reading class
$PHPReader = new PHPExcel_Reader_Excel2007();
if(!$PHPReader->canRead($dir.$templateName)){
$PHPReader = new PHPExcel_Reader_Excel5();
if(!$PHPReader->canRead($dir.$templateName)){
echo ' unidentifiable Excel File! ';
return false;
}
}
// read Excel
$PHPExcel = $PHPReader->load($dir.$templateName);
// Read worksheet 1
$currentSheet = $PHPExcel->getSheet(0);
$currentSheet->setCellValue('B13',iconv('gbk','utf-8',$txt));// Header assignment //
// instantiation Excel Writing class
$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. '"');// Output template name
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');