54 lines
1.7 KiB
C#
54 lines
1.7 KiB
C#
using CsvHelper;
|
|
using CsvHelper.Configuration;
|
|
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
|
using System.Formats.Asn1;
|
|
using System.Globalization;
|
|
using System.Text;
|
|
using WebAPI.Models;
|
|
|
|
namespace WebAPI.dataParsers
|
|
{
|
|
public class csvParser
|
|
{
|
|
public List<Record> parse(IFormFile file)
|
|
{
|
|
var info = CultureInfo.CurrentCulture;
|
|
|
|
List<Record> records = new List<Record>();
|
|
|
|
var stream = new StreamReader(file.OpenReadStream());
|
|
string content = stream.ReadToEnd();
|
|
|
|
List<string> lines = content.Split("\n").ToList();
|
|
List<List<string>> data = new List<List<string>>();
|
|
foreach (string line in lines)
|
|
{
|
|
data.Add(line.Split(";").ToList());
|
|
}
|
|
|
|
for (int i = 1; i < data[0].Count; i++)
|
|
{
|
|
for (int j = 1; j < data.Count; j++) {
|
|
if (data[j][0].Length > 0)
|
|
{
|
|
double value = double.Parse(data[j][i], CultureInfo.GetCultureInfo("pl-PL"));
|
|
if (value > 0)
|
|
{
|
|
Record record = new Record();
|
|
record.Id = Guid.NewGuid();
|
|
record.Code = data[0][i];
|
|
record.Desc1 = data[j][0];
|
|
record.Value1 = value;
|
|
record.CreatedAt = DateTime.UtcNow;
|
|
record.ModifiedAt= DateTime.UtcNow;
|
|
records.Add(record);
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
return records;
|
|
}
|
|
}
|
|
}
|