客户有个需求就是在给定的excle文件中插入相关数据,一定要注意excle里的一些格式会导出phpexcle读取后无法保存,折腾了许久,把excel文件比较特殊的格式去掉就能正常读取,插入数据,保存了,下面给大家示例我的代码
require_once 'PHPExcel.php';
$objPHPExcel = new PHPExcel();
//打开xlsx格式的文件要使用
$excelType = PHPExcel_IOFactory::identify('attache/infotemp.xlsx');
$objReader=PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load('attache/infotemp.xlsx');
//这里具体需要什么数据自行根据需求了
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$i,"你想要的数据");
$objPHPExcel->getActiveSheet()->setTitle('Manifest');
$objPHPExcel->setActiveSheetIndex(0);
$filename=urlencode('Manifest').'_'.date('Y-m-dHis');
header('Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
header('Cache-Control: max-age=0');
$objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
$objWriter->save('php://output');exit;
//下面是打开xls文件和保存
$excelType = PHPExcel_IOFactory::identify('attache/infotemp.xls'); $objReader=PHPExcel_IOFactory::createReader('Excel5'); $objPHPExcel = $objReader->load('attache/infotemp.xls'); //这里具体需要什么数据自行根据需求了 $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$i,"你想要的数据");
$objPHPExcel->getActiveSheet()->setTitle('Manifest');
$objPHPExcel->setActiveSheetIndex(0);
$filename=urlencode('Manifest').'_'.date('Y-m-dHis'); header('Content-Type:application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="'.$filename.'.xls"'); header('Cache-Control: max-age=0'); $objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel5'); $objWriter->save('php://output');exit;