CSV不是在浏览器中创建和下载 - PHP

I have array of data whom I want to create CSV file and want to download it in my browser. I am creating it using Ajax call.

Here is my Ajax code:

<script type="text/javascript" language="javascript">
$(document).ready(function(){
    $("#exportBtn").click(function(){
        $.ajax({
        url: "<?php echo WEB_URL; ?>process/adminProcess.php?page=exportFile",
        async: true,
        type: "POST",
        data: $('#reportForm').serialize(),
        beforeSend: function(){
            $("#file").html("Generating Your File");
                    }
            })
        });
    });
</script>

Here is my page code on which this call goes:

if(isset($_GET['page']) && $_GET['page']=="exportFile"){

    $data = $objadminViewFunctions->exportFile();

    header("Content-type: text/csv");
    header("Content-Disposition: attachment; filename=data.csv");
    header("Cache-Control: no-cache, no-store, must-revalidate");
    header("Pragma: no-cache"); 
    header("Expires: 0"); 

    $outstream = fopen('php://output', 'w');

    fputcsv($output, array('Serial No.', 'Report Number', 'Pallet Id', 'Type', 'Consignee', 'Damage', 'Damage Description', 'Label', 'Vessel', 'Location', 'Suryeyor'));

    foreach ($data as $sss) {
        fputcsv($output, $sss);
    }
    fclose($outstream);

 }

Here is the data in $data which I want to enter in the CSV file:

Array
(
    [0] => stdClass Object
        (
            [de_ID] => 1
            [report_ID] => 1
            [pallet_ID] => 4788
            [type] => Apple
            [consignee] => Dandrea
            [damage] => Stow Damage
            [damage_desc] => TT Damage
            [label] => Valdovinos
            [variety] => Flame Seedless
            [category_code] => Empty and/or Missing Cartons
            [pieces] => 2
            [hold] => 2
            [deck] => C
            [dDate] => 2014-12-08
            [e_ID] => 1
            [report_number] => 123
            [vessel] => 321
            [location] => Phoenix 602
            [eDate] => 2014-12-01
            [suryeyor] => Mohsin
        )

    [1] => stdClass Object
        (
            [de_ID] => 2
            [report_ID] => 3
            [pallet_ID] => 8696
            [type] => Peach
            [consignee] => Del Monte
            [damage] => Breakout Damage
            [damage_desc] => TT Damage
            [label] => Agricom
            [variety] => Mango
            [category_code] => Damage to contents of cartons
            [pieces] => 4
            [hold] => 2
            [deck] => P
            [dDate] => 2014-12-08
            [e_ID] => 3
            [report_number] => 526
            [vessel] => 748
            [location] => Atlanta 404
            [eDate] => 2014-12-01
            [suryeyor] => Amir
        )

)

I have tried with array data as well but no luck yet:

Array
(
    [0] => Array
        (
            [0] => 1
            [de_ID] => 1
            [1] => 1
            [report_ID] => 1
            [2] => 4788
            [pallet_ID] => 4788
            [3] => Apple
            [type] => Apple
            [4] => Dandrea
            [consignee] => Dandrea
            [5] => Stow Damage
            [damage] => Stow Damage
            [6] => TT Damage
            [damage_desc] => TT Damage
            [7] => Valdovinos
            [label] => Valdovinos
            [8] => Flame Seedless
            [variety] => Flame Seedless
            [9] => Empty and/or Missing Cartons
            [category_code] => Empty and/or Missing Cartons
            [10] => 2
            [pieces] => 2
            [11] => 2
            [hold] => 2
            [12] => C
            [deck] => C
            [13] => 2014-12-08
            [dDate] => 2014-12-08
            [14] => 1
            [e_ID] => 1
            [15] => 123
            [report_number] => 123
            [16] => 321
            [vessel] => 321
            [17] => Phoenix 602
            [location] => Phoenix 602
            [18] => 2014-12-01
            [eDate] => 2014-12-01
            [19] => Mohsin
            [suryeyor] => Mohsin
        )

    [1] => Array
        (
            [0] => 2
            [de_ID] => 2
            [1] => 3
            [report_ID] => 3
            [2] => 8696
            [pallet_ID] => 8696
            [3] => Peach
            [type] => Peach
            [4] => Del Monte
            [consignee] => Del Monte
            [5] => Breakout Damage
            [damage] => Breakout Damage
            [6] => TT Damage
            [damage_desc] => TT Damage
            [7] => Agricom
            [label] => Agricom
            [8] => Mango
            [variety] => Mango
            [9] => Damage to contents of cartons
            [category_code] => Damage to contents of cartons
            [10] => 4
            [pieces] => 4
            [11] => 2
            [hold] => 2
            [12] => P
            [deck] => P
            [13] => 2014-12-08
            [dDate] => 2014-12-08
            [14] => 3
            [e_ID] => 3
            [15] => 526
            [report_number] => 526
            [16] => 748
            [vessel] => 748
            [17] => Atlanta 404
            [location] => Atlanta 404
            [18] => 2014-12-01
            [eDate] => 2014-12-01
            [19] => Amir
            [suryeyor] => Amir
        )

)
duanpen9294
duanpen9294 你可以在我的代码中调整它吗?我的意思是这段代码中没有PHP涉及。它不是关于创建CSV文件。
接近 6 年之前 回复
doudou20145
doudou20145 对不起,我的错。检查一下:stackoverflow.com/questions/4545311/...
接近 6 年之前 回复
doujian0265
doujian0265 我已经评论过,因为我不需要它。它需要它吗?对不起,我麻木了。
接近 6 年之前 回复
duanmi8349
duanmi8349 你在ajax的成功或成功部分在哪里?
接近 6 年之前 回复

1个回答



你的PHP错了。 函数 fputcsv()</ code>不知道如何处理对象,它希望第二个参数是一个数组。</ p>

尝试修复它:< / p>

  fputcsv($ output,(array)$ sss); 
</ code> </ pre>
</ div>

展开原文

原文

Your PHP is wrong. The function fputcsv() doesn't know how to handle objects, it expects the second parameter to be an array.

Try to fix it like this:

fputcsv($output, (array)$sss);

duanquan1243
duanquan1243 我已经检查了我放置我的数组数据并调用它正在生成我的csv但是空数据的url。 意味着没有数据。 因为我在$ data中放了一些数组数据。
接近 6 年之前 回复
doulan9419
doulan9419 这里有两个部分:PHP部分和Ajax部分,您需要单独检查它们。 首先,在浏览器中打开PHP的URL(http://.../process/adminProcess.php?page = exportFile)并检查是否按预期工作(即浏览器要求您保存文件,它会保存 具有正确名称和终止的文件,保存的文件包含您在$ data中的CSV数据。此部分完美无缺(并且仅在此之后),专注于Ajax部分。
接近 6 年之前 回复
doudou7361
doudou7361 我也尝试过使用数组但不起作用。
接近 6 年之前 回复
doujumiao5024
doujumiao5024 我无法告诉Ajax,但PHP代码显示标题行,然后只有“PHP警告:fputcsv()期望参数1是资源,null给定”行。 如果您的错误报告被抑制,则不会显示警告,但不会显示数据。
接近 6 年之前 回复
drdr123456
drdr123456 我怎么能解决它?
接近 6 年之前 回复
doutale7115
doutale7115 谢谢,但它不起作用。
接近 6 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐