如何导出分隔的数据标签

时间:2019-05-23 11:53:00

标签: laravel

我正在从数据库中导出一些数据,我想将生成Tab的两列分开。

我在fputcsv之后已经尝试使用implode,但是它总是以逗号分隔。还尝试将我的分隔符(,)更改为其他字符,但是CSV总是打印逗号。

这是我正在使用的功能。

public function ResearchExport(Request $request)
{
    //Export ProductID and KitID of all the Kits created from the given date to the actual moment
    $TubeAddDate = $request->input('TubeAddDate');
    $select = DB::table('tubes')->select('TubeBarcodeID', 'ActivationCode')->where('LOG_important', '2')->whereBetween('TubeAddDate', array($TubeAddDate, NOW()))->get();
    $tot_record_found=0;
    if (count($select)>0) {
        $tot_record_found=1;
        $CsvData=array('TubeBarcodeID,ActivationCode');
        foreach ($select as $value) {
            $CsvData[]=$value->TubeBarcodeID.",".$value->ActivationCode.;
        }
        $filename=date('Y-m-d').".csv"; //stores the file with the date of today as name
        $file_path=storage_path().'/'.$filename;
        $file = fopen($file_path, "w+");
        foreach ($CsvData as $exp_data) {
            fputcsv($file, explode(",", $exp_data));
        }
        fclose($file);
        return response()->download($file_path, $filename);
    }
    return view('InsertForms/download', ['record_found' =>$tot_record_found]);
}

此刻,我将其作为输出

TubeBarcodeID,ActivationCode
FF01111112,IKG5G-B0FIZ
FF0111113,6XMP8-760Y3

但是我希望这样在CSV中分隔一些标签

TubeBarcodeID   ActivationCode
FF01111112      IKG5G-B0FIZ
FF0111113       6XMP8-760Y3

1 个答案:

答案 0 :(得分:0)

因此,实际上我要感谢指出我的错误的评论之一,从而解决了问题。 我只需要将.csv更改为.xls,然后用“ \ t”覆盖fputcsv的第三个参数即可。

    $filename=date('Y-m-d').".xls"; //stores the file with the date of today as name
    $file_path=storage_path().'/'.$filename;
    $file = fopen($file_path, "w+");
    foreach ($CsvData as $exp_data) {
        fputcsv($file, explode(",", $exp_data), "\t");
    }