Created
June 13, 2013 12:13
-
-
Save dcblogdev/5773211 to your computer and use it in GitHub Desktop.
Export from MySQL to Excel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function cleanData($str){ | |
$str = preg_replace("/\t/", "\\t", $str); | |
$str = preg_replace("/\n/", "\\n", $str); | |
} | |
$filename = "export_" . date('Y-m-d-h-i-s') . ".xls"; | |
header("Content-Disposition: attachment; filename=\"$filename\""); | |
header("Content-Type: application/vnd.ms-excel"); | |
$flag = false; | |
$result = mysql_query("SELECT * FROM table") or die('Query failed!'); | |
while(false !== ($row = mysql_fetch_assoc($result))) { | |
if(!$flag) { | |
# display field/column names as first row | |
echo implode("\t", array_keys($row)) . "\n"; | |
$flag = true; | |
} | |
array_walk($row, 'cleanData'); | |
echo implode("\t", array_values($row)) . "\n"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment