doujiao2443 2012-03-29 10:02
浏览 56
已采纳

使用PHP脚本将MySQL表数据导出为CSV

I am working on export excel data to using php

but it has problem on downloaded as csv with comma separated values
but i need XLS file with table field values

I using the below code

    $values = mysql_query("SELECT * FROM $table");

while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i-1;$j++) {

$csv_output .= $rowr[$j]."; ";
}
   $csv_output .= "
";
}
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
  • 写回答

1条回答 默认 最新

  • douji1999 2012-03-29 10:19
    关注
    $rown=0;
    while( $row = mysql_fetch_assoc($values)){
      if($rown++==0)
         $csv_output.=implode(";",array_keys($row)."
    ";
      $csv_output.=implode(";",array_values($row)."
    ";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?