我有一個從MySQL和PHP中獲取數據,並將其導出到excel文件,其這樣的代碼:Excel導出不能從MySQL獲得太多的數據
<?php
$xls_filename = "export_".rand(100000,999999).".xls"; // Define Excel (.xls) file name
// Header info settings
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=$xls_filename");
header("Pragma: no-cache");
header("Expires: 0");
/***** Start of Formatting for Excel *****/
// Define separator (defines columns in excel & tabs in word)
$sep = "\t"; // tabbed character
$slct2 = $dbh->prepare("SELECT * FROM forms_answers WHERE ans_formid='{$identity}'");
$slct2->execute();
while($name = $slct2->fetch(PDO::FETCH_ASSOC)) {
$stmt2 = $dbh->prepare("SELECT * FROM forms_answers_details WHERE ans_id='{$name['ans_id']}'");
$stmt2->execute();
// Start of printing column names as names of MySQL fields
for($i=0; $i<$stmt2->columnCount(); $i++) {
echo $stmt2->getColumnMeta($i)."\t";
}
print("\n");
// End of printing column names
// Start while loop to get data
while($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
$schema_insert = "";
for($j=0; $j<$stmt2->columnCount(); $j++) {
if(!isset($row[$j])) {
$schema_insert .= "NULL".$sep;
}
elseif(isset($row[$j])) {
$schema_insert .= "$row[$j]".$sep;
}
else {
$schema_insert .= "".$sep;
}
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
$schema_insert .= "\t";
print(trim($schema_insert));
print "\n";
}
}
?>
但問題是,這個代碼只是出口數據可以說它幾乎可以導出一個8 * 20的excel表格,並且不能導出超出該限制的數據。相反,它會導出Array,NULL和頁面代碼。任何人都可以建議我該怎麼做才能解決這個問題?
你能跟我們分享一個例子嗎? –
您需要在緊急情況下導出數據,即在sql查詢中限制位置 –