gpt4 book ai didi

php - 使用phpexcel将MySql导出到excel

转载 作者:行者123 更新时间:2023-11-29 23:01:51 25 4
gpt4 key购买 nike

大家好,我创建了一个程序将 MySQL 数据导出到 Excel,但在某些时候我尝试打开 Excel 文件时遇到错误。

错误:您尝试打开的文件格式与文件扩展名指定的格式不同。在打开文件之前,请验证该文件没有损坏并且来自受信任的来源。您要打开该文件吗?现在归档吗?

当我单击"is"时,我会在该 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,但随后您还需要更改内容类型标题以匹配它。

关于php - 使用phpexcel将MySql导出到excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28449860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com