dtr32221 2014-12-11 12:54
浏览 137

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
        )

)
  • 写回答

1条回答 默认 最新

  • douzheren3349 2014-12-11 13:03
    关注

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题