Comment puis-je get des valeurs à partir d'un file csv où certaines des cellules contiennent des virgules?

Dd illustréTFMM cit ChambeAldAachacheaMMMMMMMM DaTdMMMMMMMMMMMMMMMMMMMMMMMMMMMTTdY AlAAWAYAAAYTTTTTMMMMTTERMERMMDMMMMMT TratedAAAAAA TraddMemDDDMMMachachachachachachachachachachachachachachachach AllachachachachachachddddAA Marco citadinNelélélMéléldélemTachélmemMéléldélemtadiceAéleaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaacheaMacheaMelMMMMMMdAAAachéldAachelAtrasseAAAAAAHAAAAYAAAYAAAYAAAAAAAAAAA AYAAA“AAYYAAAA ACAA“ A` AùH`AAAAA“`TA ¿Muidence Marco

protected void SubmitButton_Click(object sender, EventArgs e) { if (UpdateFile.PostedFile != null) { var file = UpdateFile.PostedFile; // check if valid csv file message.InnerText = "Updating..."; Sitecore.Context.SetActiveSite("backedbybayer"); _database = Database.GetDatabase("master"); SitecoreContext context = new SitecoreContext(_database); Item homeNode = context.GetHomeItem<Item>(); var productsItems = homeNode.Axes.GetDescendants() .Where( child => child.TemplateID == new ID(TemplateFactory.FindTemplateId<IProductDetailPageItem>())); try { using (StreamReader sr = new StreamReader(file.InputStream)) { var firstLine = true; ssortingng currentLine; var productIdIndex = 0; var industryIdIndex = 0; var categoryIdIndex = 0; var pestIdIndex = 0; var titleIndex = 0; ssortingng title; ssortingng productId; ssortingng categoryIds; ssortingng industryIds; while ((currentLine = sr.ReadLine()) != null) { var data = currentLine.Split(',').ToList(); if (firstLine) { // find index of the important columns productIdIndex = data.IndexOf("ProductId"); industryIdIndex = data.IndexOf("PrimaryIndustryId"); categoryIdIndex = data.IndexOf("PrimaryCategoryId"); titleIndex = data.IndexOf("Title"); firstLine = false; continue; } title = data[titleIndex]; productId = data[productIdIndex]; categoryIds = data[categoryIdIndex]; industryIds = data[industryIdIndex]; var products = productsItems.Where(x => x.DisplayName == title); foreach (var product in products) { product.Editing.BeginEdit(); try { product.Fields["Product Id"].Value = productId; product.Fields["Product Industry Ids"].Value = industryIds; product.Fields["Category Ids"].Value = categoryIds; } finally { product.Editing.EndEdit(); } } } } // when done message.InnerText = "Complete"; } catch (Exception ex) { message.InnerText = "Error reading file"; } } } 

Le problème est que lorsqu'un champ de description comporte des virgules, comme «Le produit est un biofongicide efficace et préventif», il se divise également et jette l'index, de sorte que categoryIds = data[8] obtient la mauvaise valeur.

La feuille de calcul est une donnée fournie par notre client, donc je préfère ne pas obliger le client à modifier le file, sauf si nécessaire. Est-ce que je peux gérer cela dans mon code? Existe-t-il une façon différente de lire le file qui ne splita pas tout par virgule?

Je suggère d'utiliser Ado.Net, si datatables du champ sont entre guillemets et il l'parsingra comme un champ et ignorera toutes les virgules à l'intérieur de ce …

Exemple de code:

 static DataTable GetDataTableFromCsv(ssortingng path, bool isFirstRowHeader) { ssortingng header = isFirstRowHeader ? "Yes" : "No"; ssortingng pathOnly = Path.GetDirectoryName(path); ssortingng fileName = Path.GetFileName(path); ssortingng sql = @"SELECT * FROM [" + fileName + "]"; using(OleDbConnection connection = new OleDbConnection( @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly + ";Extended Properties=\"Text;HDR=" + header + "\"")) using(OleDbCommand command = new OleDbCommand(sql, connection)) using(OleDbDataAdapter adapter = new OleDbDataAdapter(command)) { DataTable dataTable = new DataTable(); dataTable.Locale = CultureInfo.CurrentCulture; adapter.Fill(dataTable); return dataTable; } }