phpmailer xls已损坏

izj3ouym  于 2021-10-10  发布在  Java
关注(0)|答案(0)|浏览(204)

我有一个phpmailer脚本。
有很多脚本文件,但不能100%确定哪一个是问题所在。
当我尝试运行脚本文件时,我会收到一个损坏的文件发送到我的电子邮件。
这是我的php xls函数

{
       include_once(realpath(dirname(__FILE__) . "/../../phpexcel/") . "/PHPExcel.php");
       $this->_helper->viewRenderer->setNoRender();
       $fileName = $this->_request->fileName;
       if (!$fileName)
           return;
       $download   = $this->_request->download ? true : false;
       $htmlPath   = realpath(dirname(__FILE__) . "/../../public/reports/html/") . "/" . $fileName;
       $fileSource = file_get_contents($htmlPath);
       preg_match('!<title>([^<]+)!', $fileSource, $title);
       $title = trim($title[1]);
       if (stristr($fileSource, '<graph>')) {
           $fileSource = preg_replace('!<graph>[^<]+</graph>!is', '', $fileSource);
           file_put_contents($htmlPath, $fileSource);
       }
       file_put_contents($htmlPath,preg_replace('!<title>[^<]+</title>!is','',$fileSource));
       $reader  = new PHPExcel_Reader_HTML;
       $content = $reader->load($htmlPath);

       // Pass to writer and output as needed
       $objWriter = PHPExcel_IOFactory::createWriter($content, 'Excel2007');
       $download  = 1;
       // Redirect output to a client’s web browser (Excel5)
       //header('Content-Type: application/vnd.ms-excel');
       $fileName  = str_replace(' ', '-', ($title . "-" . date('Y-m-d')));
       if ($download) {
           // Redirect output to a client’s web browser (Excel2007)
           header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
           header('Cache-Control: max-age=0');
           // If you're serving to IE 9, then the following may be needed
           //  header('Cache-Control: max-age=1');

           // If you're serving to IE over SSL, then the following may be needed
           header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
           header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
           header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
           header('Pragma: public'); // HTTP/1.0
           header('Content-Disposition: attachment; filename=' . $fileName . ".xlsx");

       }
       $objWriter->save('php://output');

       return;

   }```

does anyone know what i am missing with this code?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题