我无法将PHPExcel生成的文件保存到服务器。什么时候这样做
$this->load->library('Classes/PHPExcel');
$this->phpexcel->getActiveSheet()->setCellValue('A5','Value');
more excel code...
$writer = new PHPExcel_Writer_Excel5($this->phpexcel);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="newFile.xls"');
header('Cache-Control: max-age=0');
$writer->setPreCalculateFormulas(false);
$writer->save('php://output');
我可以下载该文件,但我尝试了许多不同的方法将文件保存在服务器文件夹中,例如
$filename = 'file.xls';
$writer = new PHPExcel_Writer_Excel5($this->phpexcel);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="newFile.xls"');
header('Cache-Control: max-age=0');
$writer->setPreCalculateFormulas(false);
$writer->save($filename);
或使用PHPExcel_IOFactory来保存文件,但是我无法让它工作,同样的想法很好。
问候。
答案 0 :(得分:0)
试试这个:
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$fname.'"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('./files/'.$fname);
答案 1 :(得分:0)
昨晚我解决了错误。
$writer = new PHPExcel_Writer_Excel5($this->phpexcel);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="newFile.xls"');
header('Cache-Control: max-age=0');
$writer->setPreCalculateFormulas(false);
$writer->save(getcwd().'/mailAttachment/newFile.xls');
我添加了这一行。
$writer->save(getcwd().'/mailAttachment/newFile.xls');
函数getcwd()获取当前工作目录。
非常感谢。