I´m trying to create an import of CSV data into my Blazor project in .NET7 but I´m facing issues with how to handle the data on one specific field in the CSV and can´t seem to get around the issue.
The CSV data I´m trying to import typically looks like this: First there are columnHeaders for "Name", "Address", "City" etc. and finally "Notes". Then comes the data values using ; as the delimiter. I´m able to read in all fields except "Notes" because it has a different format
NameValue;AddressValue;CityValue;"{
"Information": "",
"InformationHistoryFixed": "",
"customerNotes": [],
"bookingNotes": []
}"
The only thing in the "Notes" header I manage to use from this is the very first "{ then comes a row break that cuts me off from the rest of the data.
Right now I´m using an InputFile through a file uploader/picker I then run a method to process the data which is where the headers are assigned and the rest of the data is sent to a DataTable to read values in each cell.
private async Task ProcessImportData()
{
if (uploadedFileStream != null)
{
using (MemoryStream memoryStream = new MemoryStream())
{
await uploadedFileStream.CopyToAsync(memoryStream);
memoryStream.Position = 0;
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
using (Stream inputStream = memoryStream)
{
IWorkbook workbook = application.Workbooks.Open(inputStream);
IWorksheet worksheet = workbook.Worksheets[0];
columnHeaders = new List<string>();
for (int i = 1; i <= worksheet.UsedRange.LastColumn; i++)
{
if (!string.IsNullOrEmpty(worksheet[1, i]?.Text))
{
string[] headers = worksheet[1, i].Text.Split(';');
foreach (string header in headers)
{
columnHeaders.Add(header.Trim());
}
}
}
DataTable dataTable = new DataTable();
foreach (var columnHeader in columnHeaders)
{
dataTable.Columns.Add(columnHeader);
}
for (int i = 2; i <= worksheet.UsedRange.LastRow; i++)
{
DataRow dataRow = dataTable.NewRow();
for (int j = 1; j <= worksheet.UsedRange.LastColumn; j++)
{
dataRow[j - 1] = worksheet[i, j].Text;
}
dataTable.Rows.Add(dataRow);
}
string jsonString = GetJsonStringFromWorksheet(worksheet);
ProcessDataTable(dataTable);
}
}
}
}
}
It then goes to ProcessDataTable where it should create a model and fill in Name, Address, Notes etc:
private void ProcessDataTable(DataTable dataTable)
{
processedData = new List<ImportCustomerModel>();
// Loop through each row in the DataTable, skipping the first row with column headers
for (int i = 0; i < dataTable.Rows.Count; i++)
{
DataRow row = dataTable.Rows[i];
ImportCustomerModel model = new ImportCustomerModel();
string line = row[0].ToString();
if (columns.Length >= 21)
{
model.Name= columns[0]?.Trim();
model.Address= columns[1]?.Trim();
model.City= columns[2]?.Trim();
....
model.Notes= columns[20]?.Trim();
processedData.Add(model);
}
}
}