将文件下载到服务器phpexcel + codeigniter

时间:2015-01-05 03:15:55

标签: php codeigniter phpexcel

我无法将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来保存文件,但是我无法让它工作,同样的想法很好。

问候。

2 个答案:

答案 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()获取当前工作目录。

非常感谢。