-
This is happening to me,, example I have a csv file with.
This CSV was exported from a software, and this is the text inside the CSV "name";"age"
"John":"4"
"jason":"5" but then when I tried to import it using this. $claimBulkImport = new ClaimsBulkImport($request->auth_group_id);
Excel::import($claimBulkImport, $request->file('file')); and I tried to log it using logger, this is an example of what I got. local.DEBUG: [
{"nameage: John;4"},
{"nameage": Jason;5}
] I really need help.. thanks a lot. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
You probably are using the wrong delimiter in your config file. |
Beta Was this translation helpful? Give feedback.
-
So What I did was created this function, to determine what delimiter is used in the CSV file. /**
* @param string $filePath
* @param int $checkLines
* @return string
*/
public function getCsvDelimiter(string $filePath, int $checkLines = 3): string
{
$delimiters = [",", ";", "\t"];
$default = ",";
$fileObject = new \SplFileObject($filePath);
$results = [];
$counter = 0;
while ($fileObject->valid() && $counter <= $checkLines) {
$line = $fileObject->fgets();
foreach ($delimiters as $delimiter) {
$fields = explode($delimiter, $line);
$totalFields = count($fields);
if ($totalFields > 1) {
if (!empty($results[$delimiter])) {
$results[$delimiter] += $totalFields;
} else {
$results[$delimiter] = $totalFields;
}
}
}
$counter++;
}
if (!empty($results)) {
$results = array_keys($results, max($results));
return $results[0];
}
return $default;
} add a protected variable to my class, and put my delimiter and add it in the CSV setting. like so. <?php
namespace App\Imports\V1;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithCustomCsvSettings;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class MyImportClass implements ToCollection, WithHeadingRow, WithCustomCsvSettings
{
protected $csvDelimiter;
public function __construct($delimiter = ',')
{
$this->csvDelimiter = $delimiter;
}
/**
* @param Collection $collection
*/
public function collection(Collection $rows)
{
//codes here
}
public function getCsvSettings(): array
{
return [
'delimiter' => $this->csvDelimiter
];
}
} |
Beta Was this translation helpful? Give feedback.
So What I did was created this function, to determine what delimiter is used in the CSV file.