floppyraid floppyraid - 5 months ago 35x
PHP Question

PHPExcel will not export to CSV

I am banging my head against a wall trying to figure out why I cannot get PHPExcel to output a csv file. Oddly enough, this works in Internet Explorer- it outputs as csv, but in every other browser I try it outputs it as an xls that Excel refuses to open.

Any pointers would be exceedingly appreciated.


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

$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();

$rowNumber = 1;
$headings = array('abunchof','differentheadings');
while ($row = mysql_fetch_row($result)) {
$col = 'A';
foreach($row as $cell) {

$objWriter = new PHPExcel_Writer_CSV($objPHPExcel);
$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');

echo 'Contact your Administrator. No data received from server.';


You could try to change the MIME type to

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