在PHP中导出到Excel

时间:2017-12-03 06:52:17

标签: php html excel vba

我正在尝试使用php将一个调查报告导出到Excel工作表中,并且它在我的本地主机上工作正常,但是当我托管它时似乎只是显示excel表而不是下载。我认为它会覆盖我代码中的标题。

我的代码在这里。

#include <stdio.h>
#include <stdlib.h>

int main(void) {
  int a[6] = {1,2,3,4,5,6};
  int *p = &a[6];
  printf("%d\n", *p);
  printf("%d\n", *(p + 1));
}  

如何使用标题解决此问题。

1 个答案:

答案 0 :(得分:0)

Try following code
// Redirect output to a client’s web browser (Excel2007)

    header("Content-type: application/vnd-ms-excel");
    header("Content-Disposition: attachment; filename=".$filename);//filename with extension .xlsx
    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
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    $objWriter->save('php://output');