簡體   English   中英

PHPExcel 不會導出到 CSV

[英]PHPExcel will not export to CSV

我正在用頭撞牆,試圖弄清楚為什么我無法將 PHPExcel 轉換為 output 和 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');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM