如何将列名设置为.csv文件列的名称?

时间:2016-11-08 08:30:36

标签: php csv

我有一个代码可以创建我的表的.csv导出。这是我的代码:

public function export(Request $request){

    header('Content-Type: application/excel');
    header('Content-Disposition: attachment; filename="export.csv"');

    $tb_name_alias = $request->tb_name;
    $convert_alias_to_table_name = array('person' => "App\\persons");
    $tb_name = $convert_alias_to_table_name[$tb_name_alias];
    $arr = $tb_name::all()->toArray();

    $newarr = array();
    $size_of_outer_array = sizeof($arr);

    For ( $i = 0; $i < $size_of_outer_array; $i++ ) {
        $newarr[] = implode(",",$arr[$i]);
    }

    $fp = fopen('php://output', 'w');
    foreach ( $newarr as $line ) {
        $val = explode(",", $line);
        fputcsv($fp, $val);
    }
    fclose($fp);
}

它也可以,但是当我再次导入它时,它看起来像这样:

enter image description here

如您所见,列名称并非真实......它们是col1col2等等。但是我需要设置一些名称作为列名。与idname等类似。!

我该怎么做?

1 个答案:

答案 0 :(得分:1)

试试这个

HWND hwndWBA;
pwba->get_HWND((LONG_PTR*)&hwndWBA);
InvalidateRect(hwndWBA, NULL, TRUE);
pwba->Quit();
pwba->Release();