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)."
    ";
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题