Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用phpexcel将MySql导出到excel_Php_Mysql_Phpexcel - Fatal编程技术网

使用phpexcel将MySql导出到excel

使用phpexcel将MySql导出到excel,php,mysql,phpexcel,Php,Mysql,Phpexcel,大家好,我已经创建了一个将MySQL数据导出到excel的程序,但是在我试图打开excel文件时出现了错误 错误:您试图打开的文件的格式与文件扩展名指定的格式不同。请在打开该文件之前,确认该文件未损坏并且来自受信任的源。是否立即打开该文件 当我点击yes时,我会在excel文件中看到一些有趣的字符。 这是我的密码: include 'setup.php'; _connectsurvey(); //Database require_once 'PHPExcel.php'; $objPH

大家好,我已经创建了一个将MySQL数据导出到excel的程序,但是在我试图打开excel文件时出现了错误

错误:您试图打开的文件的格式与文件扩展名指定的格式不同。请在打开该文件之前,确认该文件未损坏并且来自受信任的源。是否立即打开该文件

当我点击yes时,我会在excel文件中看到一些有趣的字符。 这是我的密码:
 include 'setup.php'; 
 _connectsurvey(); //Database
 require_once 'PHPExcel.php';

 $objPHPExcel = new PHPExcel();

 $query = "SELECT * FROM questionanswer";

 $result = mysql_query($query) or die(mysql_error());

 $objPHPExcel = new PHPExcel();

 $rowCount = 1;

 while($row = mysql_fetch_array($result)){
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $row['gender']);

  $objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $row['city']);
  $rowCount++;
  pr($objPHPExcel);
}

 header('Content-Type: application/vnd.openxmlformats-   officedocument.spreadsheetml.sheet');
 header('Content-Disposition: attachment;filename="survey.xls"');
 header('Cache-Control: max-age=0');

 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
 $objWriter->save('php://output');

在底部附近,命名文件时使用文件扩展名.xlsx,这是office 2007中基于openxml的文档的正确文件扩展名,应该可以解决您的问题

header('Content-Disposition: attachment;filename="survey.xlsx"');
如果您确实需要使用xls,请将输出类型更改为Excel2003,但还需要更改内容类型标题以匹配该类型