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

时间:2014-12-11 12:54:24

标签: php ajax csv fputcsv

我有一些数据,我想创建CSV文件,并希望在我的浏览器中下载它。我正在使用Ajax调用创建它。

这是我的Ajax代码:

<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>

以下是此次调用的页码:

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);

 }

以下是我想在CSV文件中输入的$ data中的数据:

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
        )

)

我也尝试过使用数组数据,但还没有运气:

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
        )

)

1 个答案:

答案 0 :(得分:0)

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

尝试像这样修复它:

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