如何将javascript ajax数据导出到csv? [重复]

This question already has an answer here:

What I want to do: 1,use Javascript ajax POST array to PHP; 2,use POST array to search result from mysql with PHP; 3,return search results with csv format, so user can save the file. I have accomplished part 1 and part 2, but I can't get csv file returned I use ajax to POST array data to PHP like this:

$(document).ready(function(){
$('#export').click(function(){
    var list = JSON.stringify(count);
    $.ajax({
        type: "POST",
        url: "exportDataColoniesNumber.php",
        data: {'data': list},
        success: function(response){
            alert(response);
        }
    });
});
});

And process the data in exportDataColoniesNumber.php, and the page is to return a csv file:

<?php
$data = json_decode($_POST['data']);
$last_key = end(array_keys($data));
$sql = "SELECT * FROM coloniesNumber WHERE ";
foreach ($data as $key => $value) {
    $sql .= "num='" . $value . ($key == $last_key ? "';" : "' OR ");
}

$con = DatabaseConnection::get()->connect();
mysql_query('set names utf8');
mysql_select_db('fish') or die('Could not select database');
$result = mysql_query($sql);

$lines = '';
while($row = mysql_fetch_array($result)) {
    $line = '';
    for($i = 0; $i < $fields; $i++) {
        $line .= $row[$i] . ",";
    }
    $lines .= trim($line) . "
";
}
header("Content-type:application/vnd.ms-excel");
header("Content-disposition:csv".date("Y-m-d").".csv");
header("Content-disposition:filename=test.csv");
print "$lines";
?>

The alert(response) return csv data:

8,4008,11.267,fat,1,,
9,4009,12.022,thin,1,,
10,4010,11.356,thin,1,,
11,4011,10.873,thin,1,,
12,4012,11.017,thin,1,,
13,4013,11.301,thin,1,,

How can I get csv file returned ?

</div>
douchen1924
douchen1924 我也无法通过ajax导出数据。我该怎么做?
接近 3 年之前 回复
doukun0888
doukun0888 有机会code.google.com/p/jquery-csv提供帮助吗?
6 年多之前 回复
dqp99585
dqp99585 这已经得到了解答。看看stackoverflow.com/questions/14682556/...
6 年多之前 回复
doq70020
doq70020 最简单的方法是不使用ajax并只使用参数重定向到url。
6 年多之前 回复
doulangyu9636
doulangyu9636 抛弃ajax,然后提交隐藏的iframe。
6 年多之前 回复

1个回答



我个人创建了一个导出到csv的PHP处理程序,确保你有这些标题</ p>

< pre> header(“Content-Disposition:attachment; filename = report.csv”);
header(“Content-Type:text / csv”);
</ code> </ pre>

然后只是回显你的数据,csv格式化或使用许多免费的csv类之一(只需调用一个以Array作为参数的函数)</ p>

然后 使你的表单(假设你有一个)发布到一个1x1像素的隐藏iframe,并在屏幕外设置css </ p>

  iframe {position:absolute; 左:-9999px;  } 
</ code> </ pre>

您应该全部设置,然后弹出导出 - 但是,请确保文件不大或脚本需要很长时间才能完成 - 在此之前,下载不会显示。</ p>
</ div>

展开原文

原文

Personally i've created a PHP handler that exports to csv, make sure you have these headers

header("Content-Disposition: attachment; filename=report.csv");
header("Content-Type: text/csv");

Then just echo your data, csv-formatted or use one of the many free csv classes that does that for you (just call a function with an Array as a parameter)

Then make your form (asuming you have one) post to a hidden iframe of 1x1 pixels and set offscreen with css

iframe { position: absolute; left: -9999px; }

You should be all set, and the export will pop up - however, make sure the file is not big or your script takes a lot of time to complete - until then, the download won't show up.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问