I am new to PHPexcel, trying to fetch data from my database and print the id and title values in it's own columns but right now it's just printing it like this, I want each value to be in each cell:
This is how the excel looks:

Code:
<?php error_reporting(E_ALL); $username="root"; $password=""; $database="nih_bw"; $sqlsrv="localhost"; date_default_timezone_set('US/Central'); $currenttime=date("m-d-Y"); require_once '../Classes/PHPExcel.php'; $objPHPExcel = new PHPExcel(); $objPHPExcel->getProperties(); function num2alpha($n) { for($r = ""; $n >= 0; $n = intval($n / 26) - 1) $r = chr($n%26 + 0x41) . $r; return $r; } $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 id, title from measurements;"; $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('id','Title'); $objPHPExcel->getActiveSheet()->fromArray(array($headings),NULL,'A'.$rowNumber); $rowNumber++; while ($row = mysql_fetch_row($result)) { $col = '0'; foreach($row as $cell) { $objPHPExcel->getActiveSheet()->setCellValue(num2alpha($col).$rowNumber,$cell); $col++; } $rowNumber++; } $objWriter = new PHPExcel_Writer_CSV($objPHPExcel); $objWriter->setDelimiter("\t"); $objWriter->setEnclosure(''); $objWriter->setLineEnding("\r\n"); $objWriter->setSheetIndex(0); $objWriter->save('blah '.$currenttime.'.csv'); header('Content-type: text/csv'); 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.'; ?>
num2alpha(), consider using PHPExcel's built-inPHPExcel_Cell::stringFromColumnIndex()method''? and the enclosure to''..... this is the likely cause of your problem: if you have to change the delimiter, at least change it to a valid character like a"\t"that will still be recognised when the generated file is loaded into MS Excel