Exemplo n.º 1
0
        public void ecrireMetaChartData(WorksheetPart worksheetPart, Dictionary <string, Object> d)
        {
            SheetData sd = worksheetPart.Worksheet.GetFirstChild <SheetData>();
            int       i  = 1;

            foreach (string key in d.Keys.Where(s => s.Contains("meta")))
            {
                //Enregistre la ligne de début du tableau de ce metachart
                ((MetaChartData)d[key]).indice = i;

                //Ligne d'axisData
                Row           rr  = new Row();
                int           k   = 1;
                MetaChartData tmp = (MetaChartData)d[key];
                foreach (string serie in tmp.series)
                {
                    Cell ceee = XcelWin.createTextCell(headerColumns[k], i, ((ChartData)d[serie]).nomModele, 0);
                    rr.Append(ceee);
                    k++;
                }
                sd.Append(rr);
                i++;

                //le nombre de ligne de data
                //Les formules de data et titre de series
                foreach (int data in ((ChartData)d[tmp.series.First()]).data)
                {
                    Row r = new Row();

                    string titre   = ((ChartData)d[tmp.series.First()]).colTitres;
                    string feuille = ((ChartData)d[tmp.series.First()]).titre;//nomModele

                    Cell cee = GenerateCell(headerColumns[0] + i, "'" + feuille + "'!$" + titre + "$" + data);
                    r.Append(cee);
                    //le nombre de serie
                    int j = 1;
                    foreach (string serie in tmp.series)
                    {
                        //string formule = "" + ((ChartData)d[serie]).nomModele + "!$" + ((ChartData)d[serie]).colValeurs + "$" + data;
                        string formule = "'" + ((ChartData)d[serie]).titre + "'!$" + ((ChartData)d[serie]).colValeurs + "$" + data;
                        Cell   ce      = GenerateCell(headerColumns[j] + i, formule);
                        r.Append(ce);
                        j++;
                    }
                    i++;
                    sd.Append(r);
                }
            }
        }
Exemplo n.º 2
0
        public void execution(DataSet dt, string date, string[] sheetNames, bool monoFeuille = false)
        {
            if (monoFeuille)
            {
                //Copie du template et ouverture du fichier
                System.IO.File.Copy(template, copie, true);
                SpreadsheetDocument myWorkbook = SpreadsheetDocument.Open(copie, true);

                //Access the main Workbook part, which contains all references.
                WorkbookPart  workbookPart = myWorkbook.WorkbookPart;
                WorksheetPart wp           = XcelWin.getWorksheetPartByName(myWorkbook, "Feuil1");
                creerFeuille2(wp, dt);

                myWorkbook.WorkbookPart.Workbook.CalculationProperties.ForceFullCalculation  = true;
                myWorkbook.WorkbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = true;

                //Sauvegarde du workbook et fermeture de l'objet fichier
                workbookPart.Workbook.Save();
                myWorkbook.Close();
            }
            else
            {
                //Copie du template et ouverture du fichier
                System.IO.File.Copy(template, copie, true);
                SpreadsheetDocument myWorkbook = SpreadsheetDocument.Open(copie, true);

                //Access the main Workbook part, which contains all references.
                WorkbookPart workbookPart = myWorkbook.WorkbookPart;

                if (stylePath != null)
                {//Importation des styles contenus dans le fichier de style
                    //Copie du StyleSheet existant pour ne pas perdre les elements du template
                    var        wsp = workbookPart.WorkbookStylesPart;
                    Stylesheet ss  = wsp.Stylesheet;
                    // Comme on a fait une copie, on doit supprimer le WorkbookStylePart oiginal
                    workbookPart.DeletePart(wsp);
                    // Et on y ajoute le notre
                    WorkbookStylesPart wbsp = workbookPart.AddNewPart <WorkbookStylesPart>();
                    //Importation des styles contenus dans le fichier de style
                    Interface itf = new Interface();
                    itf.lectureFichier(ss, stylePath);
                    wbsp.Stylesheet = ss;
                    wbsp.Stylesheet.Save();
                    lineStyles = itf.lineStyles;
                }

                //WorksheetPart que l'on va copier
                WorksheetPart worksheetPart = XcelWin.getWorksheetPartByName(myWorkbook, "Feuil1");

                //Obtention des conf de graph : modeles, titres, colonnes ...
                Dictionary <string, Object> d = new Dictionary <string, Object>();
                if (graphPath != null)
                {
                    d = Interface.lectureConfGraph(graphPath);
                }
                WorksheetPart graphs = XcelWin.getWorksheetPartByName(myWorkbook, "Graphs");

                //Dimension du plus grand des graphs (pour les placer sans superposition par la suite)
                //maxJ pour savoir ou placer les metagraphs par la suite
                int maxLi = -1, maxCol = -1;
                int maxJ = 0;
                try
                { XcelWin.maxDimChart(graphs, out maxLi, out maxCol); }
                catch (NullReferenceException)
                { Console.WriteLine("Aucun graph présent dans la feuille 'graph' \nIgnore toutes les operations relatives aux graphs"); }


                //Creation de chacunes des feuilles
                for (int i = 0; i < dt.Tables.Count; i++)
                {
                    //obtention du nom pour la feuille, si absent : Sheet 1, 2, 3...
                    string nom = "";
                    try { nom = sheetNames[i]; }
                    catch (Exception) { nom = "Sheet" + (i + 1); }

                    WorksheetPart wp = CopySheet(workbookPart, worksheetPart, nom);

                    int tab = -1;
                    foreach (string clef in d.Keys.Where(s => s.Contains("tableau")))
                    {
                        if (((TableauData)d[clef]).indice == i)
                        {
                            tab = i;
                        }
                    }
                    if (tab != -1)
                    {
                        creerTableau(wp, dt.Tables[i], i + 1);
                    }
                    else
                    {
                        creerFeuille(wp, dt.Tables[i]);
                    }


                    //Obtention des lignes de données des graphs
                    d = Interface.razConfGraph(d);
                    if (indiceGraph != -1)
                    {
                        obtInfosGraphs(dt.Tables[i], d, nom);
                    }
                    int j = 0;

                    //Si on a trouvé des graphs lors de l'obtention de leurs tailles
                    if (maxLi != -1)
                    {
                        //Creation des graphs qui ont les clefs de type graph
                        foreach (string clef in d.Keys.Where(s => s.Contains("graph") && ((ChartData)d[s]).data.Count != 0))
                        {
                            ChartData tmp = (ChartData)d[clef];
                            ChartPart y   = XcelWin.cloneChart(graphs, tmp.nomModele, i, j, maxLi, maxCol);
                            if (tmp.colValeurs == "last")
                            {
                                XcelWin.fixChartData(y, nom, tmp.data, headerColumns[dt.Tables[i].Columns.Count - nbColonneConfig - 1], getHeaderCol(tmp.colTitres, dt.Tables[i]));
                            }
                            else
                            {
                                try { XcelWin.fixChartData(y, nom, tmp.data, getHeaderCol(tmp.colValeurs, dt.Tables[i]), getHeaderCol(tmp.colTitres, dt.Tables[i])); }
                                catch (Exception) { Console.WriteLine("Echec de mise à jour des données du graph, verifier le fichier de configuration " + clef); }
                            }
                            XcelWin.fixChartTitle(y, tmp.titre + " " + nom);
                            j++;
                        }
                        if (j > maxJ)
                        {
                            maxJ = j;
                        }
                    }
                }
                //Si on a trouvé des graphs lors de l'obtention de leurs tailles
                if (maxLi != -1)
                {
                    //Creation des metagraphs
                    WorksheetPart wp2 = XcelWin.addWorksheetPart(myWorkbook, "data");
                    ecrireMetaChartData(wp2, d);
                    int k = 0;
                    foreach (string key in d.Keys.Where(s => s.Contains("meta")))
                    {
                        MetaChartData tmp = (MetaChartData)d[key];
                        try
                        {
                            ChartPart     cp = XcelWin.cloneChart(graphs, tmp.nomModele, k, maxJ, maxLi, maxCol);
                            List <string> formules;
                            if (tmp.transpose)
                            {
                                formules = creaFormuleTranspose(((ChartData)d[tmp.series.First()]).data.Count, tmp.series.Count, tmp.indice);
                            }
                            else
                            {
                                formules = creaFormule(((ChartData)d[tmp.series.First()]).data.Count, tmp.series.Count, tmp.indice);
                            }
                            XcelWin.majMetaChart(cp, formules);
                            XcelWin.fixChartTitle(cp, tmp.titre);
                            k++;
                        }
                        catch (Exception) { Console.WriteLine("Impossible de copier un graph (verifiez que le graph est présent dans le template avec le bon titre) : " + tmp.nomModele); }
                    }

                    //Cache la feuille de donnée des metagraph
                    Sheet sData = XcelWin.getSheetByName(workbookPart, "data");
                    sData.State       = new SheetStateValues();
                    sData.State.Value = SheetStateValues.Hidden;

                    //suppression des modèles de graphs
                    List <string> listeModele = new List <string>();
                    foreach (string key in d.Keys.Where(s => s.Contains("graph") || s.Contains("meta")))
                    {
                        string nomModele = "";
                        try { nomModele = ((ChartData)d[key]).nomModele; }
                        catch (Exception) { nomModele = ((MetaChartData)d[key]).nomModele; }
                        if (!listeModele.Contains(nomModele))
                        {
                            listeModele.Add(nomModele);
                            try { XcelWin.supprChart(graphs, nomModele); }
                            catch (Exception) { Console.WriteLine("Impossible de supprimer le modèle car introuvable : " + nomModele); }
                        }
                    }
                }

                try
                {//Ajout de la date sur la page de garde
                    Cell cell = XcelWin.createTextCell("H", 27, date, 0);
                    Row  r    = new Row();
                    r.RowIndex = 27;
                    r.AppendChild(cell);
                    worksheetPart = XcelWin.getWorksheetPartByName(myWorkbook, "REPORTING");;
                    SheetData sheetData = worksheetPart.Worksheet.GetFirstChild <SheetData>();
                    sheetData.AppendChild(r);
                }
                catch (Exception)
                {
                    Console.WriteLine("Erreur lors de l'ajout de la date sur la page de garde 'REPORTING'");
                }

                //Suppression du worksheetPart qui a servis de modele
                workbookPart.DeletePart(XcelWin.getWorksheetPartByName(myWorkbook, "Feuil1"));
                XcelWin.getSheetByName(workbookPart, "Feuil1").Remove();

                //Sauvegarde du workbook et fermeture de l'objet fichier
                workbookPart.Workbook.Save();
                myWorkbook.Close();
            }
        }