Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.96 KB | None | 0 0
  1. using ClosedXML.Excel;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9.  
  10. namespace GestionCoursWeb
  11. {
  12.     public partial class PageHome : System.Web.UI.Page
  13.     {
  14.         protected void Page_Load(object sender, EventArgs e)
  15.         {
  16.             Tab1.CssClass = "Clicked";
  17.             Tab2.CssClass = "Initial";
  18.             Tab3.CssClass = "Initial";
  19.             Tab4.CssClass = "Initial";
  20.             MainView.ActiveViewIndex = 0;
  21.         }
  22.  
  23.         protected void Tab1_Click(object sender, EventArgs e)
  24.         {
  25.             Tab1.CssClass = "Clicked";
  26.             Tab2.CssClass = "Initial";
  27.             Tab3.CssClass = "Initial";
  28.             Tab4.CssClass = "Initial";
  29.             MainView.ActiveViewIndex = 0;
  30.         }
  31.  
  32.         protected void Tab2_Click(object sender, EventArgs e)
  33.         {
  34.             Tab1.CssClass = "Initial";
  35.             Tab2.CssClass = "Clicked";
  36.             Tab3.CssClass = "Initial";
  37.             Tab4.CssClass = "Initial";
  38.             MainView.ActiveViewIndex = 1;
  39.  
  40.         }
  41.  
  42.         public List<Cours> recupererLesDonees()
  43.         {
  44.  
  45.             List<Cours> listeDesCours = new List<Cours>();
  46.  
  47.             GridView gridViewHome = GridViewHome;
  48.  
  49.             int lignes = gridViewHome.Rows.Count - 1;
  50.             int colonnes = gridViewHome.Columns.Count;
  51.  
  52.             // Pour chaque lignes
  53.             for (int i = 0; i < gridViewHome.Rows.Count - 1; i++)
  54.             {
  55.                 String nomDuCours = "";
  56.                 String nomDuBloc = "";
  57.  
  58.                 List<DateTime> datesDuCours = new List<DateTime>();
  59.  
  60.                 // Pour chaque colonnes
  61.                 for (int j = 0; j < gridViewHome.Rows[i].Cells.Count; j++)
  62.                 {
  63.                     if (gridViewHome.Rows[i].Cells[j].Text != null)
  64.                     {
  65.                         // Valeur de la case actuelle
  66.                         String currentValue = gridViewHome.Rows[i].Cells[j].Text.ToString();
  67.  
  68.                         DateTime dateTime;
  69.  
  70.                         // Si c'est la première case, on récupère le nom du bloc
  71.                         if (j == 0 && currentValue != nomDuBloc)
  72.                         {
  73.                             nomDuBloc = currentValue;
  74.                         }
  75.  
  76.  
  77.                         // A la deuxieme case eon recupere le nom du cours
  78.                         else if (j == 1) nomDuCours = currentValue;
  79.  
  80.                         // Sinon on récupère la date si c'est possible
  81.                         else if (DateTime.TryParse(currentValue, out dateTime)) datesDuCours.Add(Convert.ToDateTime(currentValue));
  82.  
  83.                     }
  84.  
  85.                 }
  86.  
  87.                 // On ajoute le cours à la liste des cours
  88.                 if (datesDuCours.Count > 0 && !String.IsNullOrEmpty(nomDuCours)) listeDesCours.Add(new Cours(nomDuCours, datesDuCours, new Bloc(nomDuBloc)));
  89.             }
  90.  
  91.             System.Diagnostics.Debug.WriteLine(listeDesCours.Count);
  92.             return listeDesCours;
  93.         }
  94.  
  95.         protected void Tab3_Click(object sender, EventArgs e)
  96.         {
  97.             Tab1.CssClass = "Initial";
  98.             Tab2.CssClass = "Initial";
  99.             Tab3.CssClass = "Clicked";
  100.             Tab4.CssClass = "Initial";
  101.             MainView.ActiveViewIndex = 2;
  102.  
  103.  
  104.         }
  105.  
  106.  
  107.  
  108.  
  109.         protected void Tab4_Click(object sender, EventArgs e)
  110.         {
  111.             Tab1.CssClass = "Initial";
  112.             Tab2.CssClass = "Initial";
  113.             Tab3.CssClass = "Initial";
  114.             Tab4.CssClass = "Clicked";
  115.             MainView.ActiveViewIndex = 3;
  116.  
  117.             // Liste des cours récupérés
  118.             List<Cours> listeDesCours = organiserUneListeDeCours(recupererLesDonees());
  119.  
  120.             System.Diagnostics.Debug.WriteLine(listeDesCours.Count);
  121.  
  122.             DataTable dt = new DataTable();
  123.             dt.Columns.Add("Bloc");
  124.             dt.Columns.Add("Module");
  125.             dt.Columns.Add("Cours");
  126.             dt.Columns.Add("Date");
  127.             String leDernierBloc = listeDesCours[0].bloc.nom;
  128.             int row = 0;
  129.             foreach (Cours cour in listeDesCours)
  130.             {
  131.                 if(leDernierBloc != cour.bloc.nom)
  132.                 {
  133.                     dt.Rows.Add();
  134.                     dt.Rows[row][0] = "#";
  135.                     row++;
  136.                     leDernierBloc = cour.bloc.nom;
  137.  
  138.                 }
  139.                
  140.  
  141.                
  142.                 dt.Rows.Add();
  143.                 dt.Rows[row][0] = cour.bloc.nbBloc;
  144.                 dt.Rows[row][1] = cour.bloc.nom;
  145.  
  146.                 dt.Rows[row][2] = cour.nom;
  147.                 dt.Rows[row][3] = cour.dateChoisie.ToString("dd/MM/yyyy");
  148.  
  149.                 row++;
  150.  
  151.              }
  152.            
  153.  
  154.             GridViewRes3.DataSource = dt;
  155.             GridViewRes3.DataBind();
  156.  
  157.  
  158.         }
  159.  
  160.  
  161.         public List<Cours> organiserUneListeDeCours(List<Cours> listeDesCours)
  162.         {
  163.  
  164.             // valeur qui va être retournée
  165.             List<Cours> sortie = new List<Cours>();
  166.  
  167.             // pour tous les cours
  168.             for (int cour = 0; cour < listeDesCours.Count;)
  169.             {
  170.                 // tableau qui contient les potentiel doublons
  171.                 List<Cours> tableauAvecLesDoublons = listeDesCours.Where(c => c.dateChoisie == listeDesCours[cour].dateChoisie).ToList();
  172.  
  173.                 // on enlève le cours d'origine qui est forcement dans le tableau car x = x
  174.                 tableauAvecLesDoublons.Remove(listeDesCours[cour]);
  175.  
  176.                 // si il reste des doublons
  177.                 if (tableauAvecLesDoublons.Count > 0)
  178.                 {
  179.                     // on récupère l'index de la date qui est en doublon
  180.                     int index = listeDesCours[cour].dates.IndexOf(listeDesCours[cour].dateChoisie);
  181.  
  182.                     // si il reste une date à attribuer
  183.                     if (listeDesCours[cour].dates.Count - 1 > index)
  184.                     {
  185.                         // on attribue une nouvelle date au cours
  186.                         listeDesCours[cour].dateChoisie = listeDesCours[cour].dates[++index];
  187.                     }
  188.                     else
  189.                     {
  190.                         // si c'était déjà la dernière date, on met la première
  191.                         listeDesCours[cour].dateChoisie = listeDesCours[cour].dates[0];
  192.                     }
  193.  
  194.                     // on recommence la detection des doublons au cas ou il s'en ai reformé
  195.                     cour = 0;
  196.                 }
  197.                 else
  198.                 {
  199.                     // si il y a aucun doublons, on continue la recherche
  200.                     cour++;
  201.                 }
  202.             }
  203.  
  204.             sortie = listeDesCours.OrderBy(x => x.dateChoisie).ToList();
  205.             String leDernierBloc = "fef";
  206.             int nombreDeBLoc = 0;
  207.             List<List<Cours>> listeDeListeDeCours = new List<List<Cours>>();
  208.             for(int cour = 0; cour < sortie.Count; cour++)
  209.             {
  210.                 if(leDernierBloc != sortie[cour].bloc.nom)
  211.                 {
  212.                     listeDeListeDeCours.Add(new List<Cours>());
  213.                     listeDeListeDeCours[listeDeListeDeCours.Count - 1].Add(sortie[cour]);
  214.                     leDernierBloc = sortie[cour].bloc.nom;
  215.                 }
  216.                 else
  217.                 {
  218.                     listeDeListeDeCours[listeDeListeDeCours.Count - 1].Add(sortie[cour]);
  219.  
  220.                 }
  221.             }
  222.  
  223.             foreach (Cours cr in listeDeListeDeCours[5])
  224.             {
  225.                 System.Diagnostics.Debug.WriteLine(cr);
  226.             }
  227.  
  228.             List<List<Cours>> listeFinale = new List<List<Cours>>();
  229.             for (int listeDeCours = 0; listeDeCours < listeDeListeDeCours.Count; listeDeCours++)
  230.             {
  231.                 List<Cours> listeActuelle = listeDeListeDeCours[listeDeCours];
  232.  
  233.                 if (listeDeCours == 0)
  234.                 {
  235.                     listeFinale.Add(listeActuelle);
  236.                     continue;
  237.                 }
  238.                 if (listeDeCours == listeDeListeDeCours.Count - 1)
  239.                 {
  240.                     listeFinale.Add(listeActuelle);
  241.  
  242.                     continue;
  243.                 }
  244.  
  245.                 List<Cours> listeDavant = listeDeListeDeCours[listeDeCours - 1];
  246.  
  247.                 List<Cours> listeDapres = listeDeListeDeCours[listeDeCours + 1];
  248.  
  249.                 if (listeDavant[0].bloc.nom == listeDapres[0].bloc.nom)
  250.                 {
  251.                     if (listeDapres.Count >= listeActuelle.Count)
  252.                     {
  253.                      
  254.                         listeFinale[listeFinale.Count - 1] = listeDavant.Concat(listeDapres).ToList();
  255.                     }
  256.                 }
  257.                 else if (listeDavant[0].bloc.nom != listeDapres[0].bloc.nom)
  258.                 {
  259.                     listeFinale.Add(listeActuelle);
  260.  
  261.  
  262.  
  263.                 }
  264.  
  265.  
  266.  
  267.             }
  268.  
  269.             List<Cours> caVaSortir = new List<Cours>();
  270.  
  271.             foreach(List<Cours> listeCours in listeFinale)
  272.             {
  273.                 caVaSortir = caVaSortir.Concat(listeCours).ToList();
  274.             }
  275.  
  276.             return caVaSortir;
  277.         }
  278.  
  279.  
  280.         protected void btnLoadData_Click(object sender, EventArgs e)
  281.         {
  282.             using (XLWorkbook workbook = new XLWorkbook(FileUpload1.PostedFile.InputStream))
  283.             {
  284.                 IXLWorksheet sheet = workbook.Worksheet(1);
  285.                 DataTable dt = new DataTable();
  286.                 bool firstRow = true;
  287.                 foreach (IXLRow row in sheet.Rows())
  288.                 {
  289.                     if (firstRow)
  290.                     {
  291.                         foreach (IXLCell cell in row.Cells())
  292.                         {
  293.                             dt.Columns.Add(cell.Value.ToString());
  294.                         }
  295.                         firstRow = false;
  296.                     }
  297.                     else
  298.                     {
  299.                         dt.Rows.Add();
  300.                         int i = 0;
  301.                         foreach (IXLCell cell in row.Cells())
  302.                         {
  303.                             dt.Rows[dt.Rows.Count - 1][i] = cell.Value.ToString();
  304.                             i++;
  305.                         }
  306.                     }
  307.                     GridViewHome.DataSource = dt;
  308.                     GridViewHome.DataBind();
  309.                 }
  310.             }
  311.         }
  312.  
  313.         protected void MainView_ActiveViewChanged(object sender, EventArgs e)
  314.         {
  315.  
  316.         }
  317.  
  318.         protected void GridViewRes2_SelectedIndexChanged(object sender, EventArgs e)
  319.         {
  320.  
  321.         }
  322.     }
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.     public class Cours
  330.     {
  331.         public String nom
  332.         {
  333.             get;
  334.             set;
  335.         }
  336.         public List<DateTime> dates
  337.         {
  338.             get;
  339.             set;
  340.         }
  341.  
  342.         public DateTime dateChoisie
  343.         {
  344.             get;
  345.             set;
  346.         }
  347.  
  348.         public Bloc bloc
  349.         {
  350.             get;
  351.             set;
  352.         }
  353.  
  354.  
  355.  
  356.         public Cours(String nom, List<DateTime> dates, Bloc bloc)
  357.         {
  358.             this.nom = nom;
  359.             this.dates = dates;
  360.             this.dateChoisie = dates[0];
  361.             this.bloc = bloc;
  362.  
  363.         }
  364.         public override string ToString()
  365.         {
  366.             String ret = "cours = " + this.nom + " ; dates = ";
  367.             foreach (DateTime date in this.dates)
  368.             {
  369.                 ret += " { " + date.ToString() + " } ";
  370.             }
  371.  
  372.             ret += " dateChoisie = " + this.dateChoisie;
  373.  
  374.             return ret;
  375.  
  376.         }
  377.  
  378.     }
  379.  
  380.     public class Bloc
  381.     {
  382.         public String nom
  383.         {
  384.             get;
  385.             set;
  386.         }
  387.  
  388.         public int nbBloc
  389.         {
  390.             get;
  391.             set;
  392.         }
  393.  
  394.         public Bloc(String nom)
  395.         {
  396.             this.nom = nom;
  397.             this.nbBloc = 1;
  398.  
  399.         }
  400.  
  401.  
  402.     }
  403. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement