我的头撞在墙上,试图弄清楚为什么我不能让
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类型更改为
原文链接:https://www.f2er.com/php/132938.htmlheader('Content-type: text/csv');