public void ConvertBase(string inPath, string outIngredientsPath, string outCategoriesPath) { FileInfo existingFile = new FileInfo(inPath); ListeCategories categories = new ListeCategories(); ListeIngredients ingredients = new ListeIngredients(); int indexCategorie = 1, indexIngredient = 1; Categorie currentCat = null; using (StreamWriter databaseFileXML = new StreamWriter(outIngredientsPath)) { using (ExcelPackage package = new ExcelPackage(existingFile)) { for (int worksheetIndex = 1; worksheetIndex <= 8; worksheetIndex++) { ExcelWorksheet worksheet = package.Workbook.Worksheets[worksheetIndex]; Scan(worksheet, 1, databaseFileXML, ref currentCat, ingredients, categories, ref indexCategorie, ref indexIngredient); Scan(worksheet, 3, databaseFileXML, ref currentCat, ingredients, categories, ref indexCategorie, ref indexIngredient); } } } XMLPersistIngredients IngredientsXMLPersister = new XMLPersistIngredients(); IngredientsXMLPersister.Save(ingredients, outIngredientsPath); XMLPersistCategories categoriesXMLPersister = new XMLPersistCategories(); categoriesXMLPersister.Save(categories, outCategoriesPath); }
public void ChargerBase(string inIngredientsPath) { ListeIngredients ingredients = new ListeIngredients(); XMLPersistIngredients IngredientsXMLPersister = new XMLPersistIngredients(); IngredientsBase = IngredientsXMLPersister.Load(ingredients, inIngredientsPath).Ingredients.OrderBy(x => x.Nom).ToList(); IngredientsBaseFiltre = IngredientsBase; }
public void Save(ListeIngredients ingredients, string fileName) { XmlSerializer mySerializer = new XmlSerializer(typeof(ListeIngredients)); // To write to a file, create a StreamWriter object. StreamWriter myWriter = new StreamWriter(fileName); mySerializer.Serialize(myWriter, ingredients); myWriter.Close(); }
public ListeIngredients Load(ListeIngredients ingredients, string fileName) { // Constructs an instance of the XmlSerializer with the type // of object that is being deserialized. XmlSerializer mySerializer = new XmlSerializer(typeof(ListeIngredients)); // To read the file, creates a FileStream. FileStream myFileStream = new FileStream(fileName, FileMode.Open); // Calls the Deserialize method and casts to the object type. ingredients = (ListeIngredients)mySerializer.Deserialize(myFileStream); myFileStream.Close(); return(ingredients); }
private void Scan(ExcelWorksheet worksheet, int col, StreamWriter databaseFileXML, ref Categorie currentCat, ListeIngredients ingredients, ListeCategories categories, ref int indexCategorie, ref int indexIngredient) { object line = null; int index = 1; IngredientBase ingredient = null; while ((line = worksheet.Cells[index, col].Value) != null) { string col1 = worksheet.Cells[index, col].Value.ToString(); object col2Obj = null; if ((col2Obj = worksheet.Cells[index++, col + 1].Value) != null) { string col2 = col2Obj.ToString(); if (col2.Trim().CompareTo("CAT") != -1) { currentCat = new Categorie() { Id = indexCategorie++, Nom = col1.Trim() }; categories.Categories.Add(currentCat); //databaseFileXML.WriteLine(currentCat.Nom); } else { string nom = col1.ToString(); if (nom.Contains("Option Plus")) { // Option plus // Récupérer l'ingrédient précédent ! // Lui ajouter l'option plus if (ingredient != null) { ingredient.OptionPlus = int.Parse(col2.ToString().Trim()); ingredient.OptionPlusPossible = true; } } else { var nomSplit = col1.ToString().Split(new[] { ',' }, 2); ingredient = ingredients.Ingredients.FirstOrDefault(s => s.Nom == nomSplit[0].Trim()); if (ingredient == null) { ingredient = new IngredientBase() { Id = indexIngredient++, Nom = nomSplit[0].Trim(), Categorie = currentCat }; ingredients.Ingredients.Add(ingredient); } ingredient.QuantitesPoints.Add(new QuantitePoint() { Quantite = (nomSplit.GetLength(0) > 1 ? nomSplit[1].Trim() : ""), PointParUnite = int.Parse(col2.ToString().Trim()) }); //databaseFileXML.WriteLine("\t" + ingredient.Nom + " ," + ingredient.PointParUnite + (ingredient.OptionPlusPossible?" (Option plus:" + ingredient.OptionPlus + ")":"")); } } } } }