PHP Laravel 读取 csv

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52948387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:07:49  来源:igfitidea点击:

PHP Laravel read csv

phplaravelcsvmaatwebsite-excel

提问by Naveed Sheriffdeen

I have a CSV file where the data is in landscape orientation.ex:

我有一个 CSV 文件,其中数据为横向。ex:

name, test name
age, 20
gender,Male

where the first column is the headers and the second the data, i tried using laravel maatwebsite/Exceland the response after reading the file, the first row is taken as the headers. (nameand test name).

其中第一列是标题,第二列是数据,我尝试使用laravel maatwebsite/Excel和读取文件后的响应,第一行作为标题。(nametest name)。

is there any method to read this type of CSV files in laravel using maatwebsite/Excel

有什么方法可以使用 maatwebsite/Excel 在 Laravel 中读取这种类型的 CSV 文件

回答by Jignesh Joisar

maatwebsite/Excelbest package for laravel (csv create and read )

maatwebsite/Excellaravel 的最佳包(csv 创建和读取)

your method is proper so no doubt any use maatwebsite/Excelfor reading

你的方法是正确的所以毫无疑问任何use maatwebsite/Excel阅读

回答by online Thomas

You don't need an entire library. PHP has a built-in function http://php.net/manual/en/function.str-getcsv.php

您不需要整个图书馆。PHP 有一个内置函数http://php.net/manual/en/function.str-getcsv.php

回答by Sagar Patel

HI Naveed Sheriffdeen,

嗨纳维德警长,

You Can This function

你可以这个功能

public function readCSV($csvFile, $array)
{
    $file_handle = fopen($csvFile, 'r');
    while (!feof($file_handle)) {
        $line_of_text[] = fgetcsv($file_handle, 0, $array['delimiter']);
    }
    fclose($file_handle);
    return $line_of_text;
}
$csvFileName = "test.csv";
$csvFile = public_path('csv/' . $csvFileName);
$this->readCSV($csvFile,array('delimiter' => ','))