我将我的 mysql 数据库导出到 excel,一切正常,但我希望价格字段在 excel 电子表格中显示为美元符号和逗号。
这是我的代码:
$pubtable = $_GET["publication"];
$addate = $_GET["adDateHidden"];
$export = mysql_query("SELECT * FROM $pubtable WHERE addate = '$addate' ORDER BY price DESC") or die ("Sql error : " . mysql_error());
$fields = mysql_num_fields($export);
for($i = 0; $i < $fields; $i++){
$header .= mysql_field_name($export , $i). "\t";
}
while($row = mysql_fetch_row($export)){
$line = '';
foreach($row as $value) {
if(!isset($value) || trim($value) == "") {
$value = "\t";
} else {
$value = str_replace('"' , '""' , $value);
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line). "\n";
}
$data = str_replace("\r" , "" , $data);
if(trim($data) == ""){
$data = "\n(0)Records Found!\n";
}
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=".$pubtable."_".$addate.".xls");
header("Pragma: no-cache");
header("Expires: 0");
header ('Content-Transfer-Encoding: binary');
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header ('Cache-Control: cache, must-revalidate');
print "$header\n$data";
我试着这样做
$export = mysql_query("SELECT CONCAT('$', FORMAT(price, 2)) as fieldalias * FROM $pubtable WHERE addate = '$addate' ORDER BY fieldalias DESC") or die ("Sql error : " . mysql_error());
这将正确格式化它,但它只输出价格字段而没有其他内容。