1

I have been trying to export the content of mysql database to excel using the script below to no avail. I must be doing somethign wrong:

$getExcel = "SELECT name, age, course, city FROM person";
$res = mysql_query($getExcel);


/** Error reporting */
error_reporting(E_ALL);

date_default_timezone_set('Europe/London');

/** PHPExcel */
require_once 'Classes/PHPExcel.php';


// Create new PHPExcel object
echo date('H:i:s') . " Create new PHPExcel object\n";
$objPHPExcel = new PHPExcel();

if(!$res){
    die("Error");
}
$col = 0; 
$row = 0; 
while($row = mysql_fetch_assoc($res)) { 
    foreach($row as $key=>$value) { 
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value); 
        $col++; 
    } 
    $row++; 
} 

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);

// Save Excel 2007 file
echo date('H:i:s') . " Write to Excel2007 format\n";
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('record.xlsx');

What I'm I doing wrong please?

11
  • Aside from the fact that $row should start at 1 rather than 0, what's the actual problem? Is the file being generated? Are you getting any error messages? Commented Mar 15, 2012 at 16:11
  • Keep it simple, just create an html file in your code with a table in it, and save it as .xls, then open it with excel. it's a dirty dirty hack but it works if all you need to do is export some data to excel. It won't an entire whole system... Commented Mar 15, 2012 at 16:11
  • 1
    @Yaniro - it's a dirty trick that brings up a warning message in most new versions of MS EXcel - not the greatest user experience in the world... I'd consider it pretty unprofessional if I was the user opening that file Commented Mar 15, 2012 at 16:16
  • @Mark Baker, The file is not being generated and i am not getting any errors either. I can connect to the db and the query does return data - just doesn't generate the excel file. I have tried changing $row to 1 but no joy. I also tried changing $col to 1... No success. Commented Mar 15, 2012 at 16:19
  • The file should be generated in the directory on the server where the script is running... double check user permissions for that folder... or try saving to php://output with appropriate headers (and without any echoes in the code) to download it directly from your browser Commented Mar 15, 2012 at 16:21

1 Answer 1

4

Your issue is, likely, that you're using the variable $row in two different contexts ... try this:

$row = 1; 
while($mrow = mysql_fetch_assoc($res)) { 
    $col = 0; 
    foreach($mrow as $key=>$value) { 
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value); 
        $col++; 
    } 
    $row++; 
}
Sign up to request clarification or add additional context in comments.

3 Comments

Alright, this generates the excel but the columns are not aligned. E.g. if the first record starts at A1, the second start at C2, then G3 instead of all records to start at column A. Any ideas guys?
$col = 0; should be inside the while loop, immediately before the foreach() loop
You twuo guys are amazing. I'm eternally grateful!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.