Quantcast
Channel: PHPExcel Forum Rss Feed
Viewing all articles
Browse latest Browse all 2707

New Post: How to include field names with mysql data

$
0
0

Thanks Borft,

I modified your code a little and got it to work. The way you had it setup returns the title row as it should but it also moves down a row so the spreadsheet had the header row in a slant like so.

1
 2
3
4  etc.

 

Also it wasn't returning any other data along with the title row. But I figured it out, thanks again for the help. This is what worked:

 

if ($result = mysql_query($query) ordie(mysql_error())) {
/** Create a new PHPExcel object 1.0 */
   $objPHPExcel = new PHPExcel();
   $objPHPExcel->getActiveSheet()->setTitle('Data');
   }  

}
$rowNumber = 1; //start in cell 1while ($row = mysql_fetch_assoc($result)) {
	$col = 'A'; // start at column A
// returns title row
	   if ( $rowNumber == 1 ){
       	$headers = array_keys($row);
       		foreach($headers as $header) {
          	$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$header);
          	$col++;
       		}
	   	$rowNumber++;
	  }else{
	   $col = 'A';
	 	foreach($row as $cell) {          
		$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);          
		$col++;       
		} 
	  $rowNumber++;       
	  }
      
}

Viewing all articles
Browse latest Browse all 2707

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>