﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

/*
 * https://docs.microsoft.com/fr-fr/office/open-xml/how-to-parse-and-read-a-large-spreadsheet
 */
namespace ConsoleApp1
{
    class Program
    {
        private static string _chemin = "";

        static void Main(string[] args)
        {
            /*
             * Variables
             */
            Program._chemin = @"C:\developpement\generateur-mondoshopping\ConsoleApp1\";  //  Local
            //Program._chemin = @"C:\generateur-mondoshopping\ConsoleApp1\";  //  Prod
            string chaine = "";
            string ligne = "";

            /*
             * Ouverture fichier écriture
             */
            StreamWriter sw = new StreamWriter(Program._chemin + @"OUT\flux-mondoshopping.csv");

            /*
             * Chaine entête
             */
            chaine = "id;title;description;link;image_link;availability;price;sale_price;Brand;gtin;MPN;condition;color;gender";
            sw.WriteLine(chaine);

            /*
             * Parcours du fichier source
             */
            using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(Program._chemin + @"IN\source.xlsx", false))
            {
                WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
                WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();

                OpenXmlReader reader = OpenXmlReader.Create(worksheetPart);
                string text;
                while (reader.Read())
                {
                    if (reader.ElementType == typeof(CellValue))
                    {
                        text = reader.GetText();
                        Console.Write(text + " ");
                    }
                }

            }

            /*
             * Fermeture fichier écriture
             */
            sw.Close();
        }
    }
}
