gpt4 book ai didi

PHPExcel 不会导出为 CSV

转载 作者:IT王子 更新时间:2023-10-28 23:49:12 27 4
gpt4 key购买 nike

我正在用头撞墙试图找出为什么我不能让 PHPExcel 输出 csv 文件。奇怪的是,这在 Internet Explorer 中有效——它输出为 csv,但在我尝试的所有其他浏览器中它都输出为 Excel 拒绝打开的 xls。

如有任何指点,我们将不胜感激。

error_reporting(E_ALL);
$username="blah";
$password="blahz";
$database="hollatme";
$sqlsrv="localhizzost";
date_default_timezone_set('US/Central');
$currenttime=date("m-d-Y");

require_once 'Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties();


$viewinv = mysql_connect($sqlsrv,$username,$password);
if (!$viewinv) { die('Could not connect to SQL server. Contact administrator.'); }
mysql_select_db($database, $viewinv) or die('Could not connect to database. Contact administrator.');
$query = "select somestuff from someplace;";
$result = mysql_query($query);

if ($result = mysql_query($query) or die(mysql_error())) {
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle('CYImport'.$currenttime.'');

$rowNumber = 1;
$headings = array('abunchof','differentheadings');
$objPHPExcel->getActiveSheet()->fromArray(array($headings),NULL,'A'.$rowNumber);
$rowNumber++;
while ($row = mysql_fetch_row($result)) {
$col = 'A';
foreach($row as $cell) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
$col++;
}
$rowNumber++;
}


$objWriter = new PHPExcel_Writer_CSV($objPHPExcel);
$objWriter->setDelimiter(',');
$objWriter->setEnclosure('');
$objWriter->setLineEnding("\r\n");
$objWriter->setSheetIndex(0);
$objWriter->save('blah '.$currenttime.'.csv');


header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="CY Import '.$currenttime.'"..csv"');
header('Cache-Control: max-age=0');

$objWriter->save('php://output');
exit();
}
echo 'Contact your Administrator. No data received from server.';

最佳答案

您可以尝试将 MIME 类型更改为

header('Content-type: text/csv');

关于PHPExcel 不会导出为 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6346314/

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