我有一些用於將MYSQL表導出爲Excel格式的PHP腳本,但是在導出表後我遇到了一些問題。從db獲得的數據只顯示一行。將MYSQL表導出爲excel
這下面的腳本:
$dbc=mysql_connect(_SRV,_ACCID,_PWD) or die(_ERROR15.": ".mysql_error());
$db=mysql_select_db("qdbase",$dbc) or die(_ERROR17.": ".mysql_error());
$sQuery = "SELECT id, Line, Model,Lot_no,
COUNT(Serial_number) AS Qty,
SUM(S), SUM(A), SUM(B), SUM(C),
(SUM(S) + SUM(A) + SUM(B)*0.4 + SUM(C)*0.1)/COUNT(Serial_number) AS QP,
ROUND((SUM(S) + SUM(A) + SUM(B)*0.4 + SUM(C)*0.1)/COUNT(Serial_number)*1000,2) AS PPM
FROM `inspection_report`";
$rResult = mysql_query($sQuery) or die();
$count = mysql_num_fields($rResult);
// fetch table header
$header = '';
for ($i = 0; $i < $count; $i++){
$header .= mysql_field_name($rResult, $i)."\t";
}
// fetch data each row, store on tabular row data
while($row = mysql_fetch_row($rResult)){
$line = '';
foreach($row as $value){
if(!isset($value) || $value == ""){
$value = "\t";
}else{
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
$data = str_replace("\r", "", $data);
if ($data == "") {
$data = "\nno matching records found\n";
}
header("Content-type: application/vnd.ms-excel; name='excel'");
header("Content-Disposition: attachment; filename=exportfile.xls");
header("Pragma: no-cache");
header("Expires: 0");
// output data
echo $header."\n".$data;
?>
那旁邊,如何使一些邊境此表?
:回聲 「<表邊框= 1>」;哪個正確的地方,它不能讓我的腳本成爲錯誤? – klox 2010-11-27 08:09:17
耶...我想這樣做。 – klox 2010-11-27 09:38:47