Exemplo n.º 1
0
        /// <summary>
        /// Exporte les actions spécifiées.
        /// </summary>
        /// <param name="actions">Les actions.</param>
        /// <param name="cellReference">La référence de la cellule.</param>
        /// <param name="sheet">La feuille.</param>
        private void ExportActions(ICollection <KAction> actions, WorksheetPart sheet, ref CellReference cellReference)
        {
            var timeFormatService = IoC.Resolve <IServiceBus>().Get <ITimeTicksFormatService>();

            var timeScale = _data.Project.TimeScale;

            #region Format pour les actions

            // Actions
            var actionsFormats = new List <ColumnFormat>()
            {
                // Thumbnail
                new ColumnFormat()
                {
                    Header = string.Empty
                },

                // Label
                new ColumnFormat()
                {
                    Header = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_Task")
                },

                // Start
                new ColumnFormat()
                {
                    Header = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_Start")
                },

                // Duration
                new ColumnFormat()
                {
                    Header = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_Duration")
                },

                // Finish
                new ColumnFormat()
                {
                    Header = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_Finish")
                },

                // BuildStart
                new ColumnFormat()
                {
                    Header = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_BuildStart")
                },

                // BuildDuration
                new ColumnFormat()
                {
                    Header = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_BuildDuration")
                },

                // BuildFinish
                new ColumnFormat()
                {
                    Header = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_BuildFinish")
                },

                // WBS
                new ColumnFormat()
                {
                    Header = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_WBS")
                },

                // Category
                new ColumnFormat()
                {
                    Header = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_Category")
                },

                // Resource
                new ColumnFormat()
                {
                    Header = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_Resource")
                },

                // Video
                new ColumnFormat()
                {
                    Header = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_Video")
                },

                // Predecessors
                new ColumnFormat()
                {
                    Header = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_Predecessors")
                },

                // Original
                new ColumnFormat()
                {
                    Header = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_Original")
                },
            };

            // Ref1
            if (_referentialsUse[ProcessReferentialIdentifier.Ref1].IsEnabled)
            {
                actionsFormats.Add(new ColumnFormat()
                {
                    Header = IoC.Resolve <IReferentialsUseService>().GetLabel(ProcessReferentialIdentifier.Ref1)
                });
            }

            // Ref2
            if (_referentialsUse[ProcessReferentialIdentifier.Ref2].IsEnabled)
            {
                actionsFormats.Add(new ColumnFormat()
                {
                    Header = IoC.Resolve <IReferentialsUseService>().GetLabel(ProcessReferentialIdentifier.Ref2)
                });
            }

            // Ref3
            if (_referentialsUse[ProcessReferentialIdentifier.Ref3].IsEnabled)
            {
                actionsFormats.Add(new ColumnFormat()
                {
                    Header = IoC.Resolve <IReferentialsUseService>().GetLabel(ProcessReferentialIdentifier.Ref3)
                });
            }

            // Ref4
            if (_referentialsUse[ProcessReferentialIdentifier.Ref4].IsEnabled)
            {
                actionsFormats.Add(new ColumnFormat()
                {
                    Header = IoC.Resolve <IReferentialsUseService>().GetLabel(ProcessReferentialIdentifier.Ref4)
                });
            }

            // Ref5
            if (_referentialsUse[ProcessReferentialIdentifier.Ref5].IsEnabled)
            {
                actionsFormats.Add(new ColumnFormat()
                {
                    Header = IoC.Resolve <IReferentialsUseService>().GetLabel(ProcessReferentialIdentifier.Ref5)
                });
            }

            // Ref6
            if (_referentialsUse[ProcessReferentialIdentifier.Ref6].IsEnabled)
            {
                actionsFormats.Add(new ColumnFormat()
                {
                    Header = IoC.Resolve <IReferentialsUseService>().GetLabel(ProcessReferentialIdentifier.Ref6)
                });
            }

            // Ref7
            if (_referentialsUse[ProcessReferentialIdentifier.Ref7].IsEnabled)
            {
                actionsFormats.Add(new ColumnFormat()
                {
                    Header = IoC.Resolve <IReferentialsUseService>().GetLabel(ProcessReferentialIdentifier.Ref7)
                });
            }

            // IsRandom
            actionsFormats.Add(new ColumnFormat()
            {
                Header = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_IsRandom")
            });

            // Custom Text
            actionsFormats.Add(new ColumnFormat()
            {
                Header = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_CustomValue_Text1")
            });
            actionsFormats.Add(new ColumnFormat()
            {
                Header = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_CustomValue_Text2")
            });
            actionsFormats.Add(new ColumnFormat()
            {
                Header = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_CustomValue_Text3")
            });
            actionsFormats.Add(new ColumnFormat()
            {
                Header = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_CustomValue_Text4")
            });

            // Custom Numeric
            actionsFormats.Add(new ColumnFormat()
            {
                Header = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_CustomValue_Numeric1")
            });
            actionsFormats.Add(new ColumnFormat()
            {
                Header = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_CustomValue_Numeric2")
            });
            actionsFormats.Add(new ColumnFormat()
            {
                Header = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_CustomValue_Numeric3")
            });
            actionsFormats.Add(new ColumnFormat()
            {
                Header = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_CustomValue_Numeric4")
            });

            // DifferenceReason
            actionsFormats.Add(new ColumnFormat()
            {
                Header = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_DifferenceReason")
            });

            // Amélioration I/E/S
            actionsFormats.Add(new ColumnFormat()
            {
                Header = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_Reduced_IES")
            });

            // Solution
            actionsFormats.Add(new ColumnFormat()
            {
                Header = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_Reduced_Solution")
            });

            // Reduction ratio
            actionsFormats.Add(new ColumnFormat()
            {
                Header = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_Reduced_ReductionRatio")
            });

            #endregion

            CellContent[][] data = new CellContent[actions.Count][];

            uint tableRowIndex = cellReference.RowIndex;

            int i = 0;
            foreach (var action in actions)
            {
                CellContent[] row = new CellContent[actionsFormats.Count];

                // Mettre à jour IsGroup
                action.IsGroup = WBSHelper.HasChildren(action, actions);

                int j = 0;

                #region Data pour les actions

                // Thumbnail
                j++;

                // Label
                row[j++] = action.Label;

                if (!action.IsGroup)
                {
                    // Start
                    row[j++] = CellContent.TimeSpan(timeFormatService.RoundTime(action.Start, timeScale));

                    // Duration
                    row[j++] = CellContent.TimeSpan(timeFormatService.RoundTime(action.Duration, timeScale));

                    // Finish
                    row[j++] = CellContent.TimeSpan(timeFormatService.RoundTime(action.Finish, timeScale));
                }
                else
                {
                    j += 3; // Nombre de colonnes précédentes
                }
                // BuildStart
                row[j++] = CellContent.TimeSpan(timeFormatService.RoundTime(action.BuildStart, timeScale));

                // BuildDuration
                row[j++] = CellContent.TimeSpan(timeFormatService.RoundTime(action.BuildDuration, timeScale));

                // BuildFinish
                row[j++] = CellContent.TimeSpan(timeFormatService.RoundTime(action.BuildFinish, timeScale));

                // WBS
                row[j++] = action.WBS;


                if (!action.IsGroup)
                {
                    // Category
                    if (action.Category != null)
                    {
                        row[j++] = action.Category.Label;
                    }
                    else
                    {
                        row[j++] = null;
                    }

                    // Resource
                    if (action.Resource != null)
                    {
                        row[j++] = action.Resource.Label;
                    }
                    else
                    {
                        row[j++] = null;
                    }

                    // Video
                    if (action.Video != null)
                    {
                        row[j++] = action.Video.Filename;
                    }
                    else
                    {
                        row[j++] = null;
                    }

                    // Predecessors
                    row[j++] = FormatPredecessorsString(action);

                    // Original
                    if (action.Original != null)
                    {
                        row[j++] = action.Original.Label;
                    }
                    else
                    {
                        row[j++] = null;
                    }

                    // Ref1
                    if (_referentialsUse[ProcessReferentialIdentifier.Ref1].IsEnabled)
                    {
                        row[j++] = GetMultiReferentialLabels(action.Ref1, ProcessReferentialIdentifier.Ref1);
                    }

                    // Ref2
                    if (_referentialsUse[ProcessReferentialIdentifier.Ref2].IsEnabled)
                    {
                        row[j++] = GetMultiReferentialLabels(action.Ref2, ProcessReferentialIdentifier.Ref2);
                    }

                    // Ref3
                    if (_referentialsUse[ProcessReferentialIdentifier.Ref3].IsEnabled)
                    {
                        row[j++] = GetMultiReferentialLabels(action.Ref3, ProcessReferentialIdentifier.Ref3);
                    }

                    // Ref4
                    if (_referentialsUse[ProcessReferentialIdentifier.Ref4].IsEnabled)
                    {
                        row[j++] = GetMultiReferentialLabels(action.Ref4, ProcessReferentialIdentifier.Ref4);
                    }

                    // Ref5
                    if (_referentialsUse[ProcessReferentialIdentifier.Ref5].IsEnabled)
                    {
                        row[j++] = GetMultiReferentialLabels(action.Ref5, ProcessReferentialIdentifier.Ref5);
                    }

                    // Ref6
                    if (_referentialsUse[ProcessReferentialIdentifier.Ref6].IsEnabled)
                    {
                        row[j++] = GetMultiReferentialLabels(action.Ref6, ProcessReferentialIdentifier.Ref6);
                    }

                    // Ref7
                    if (_referentialsUse[ProcessReferentialIdentifier.Ref7].IsEnabled)
                    {
                        row[j++] = GetMultiReferentialLabels(action.Ref7, ProcessReferentialIdentifier.Ref7);
                    }

                    // IsRandom
                    row[j++] = action.IsRandom.ToString();

                    // Custom Text
                    row[j++] = action.CustomTextValue;
                    row[j++] = action.CustomTextValue2;
                    row[j++] = action.CustomTextValue3;
                    row[j++] = action.CustomTextValue4;

                    // Custom Numeric
                    row[j++] = action.CustomNumericValue;
                    row[j++] = action.CustomNumericValue2;
                    row[j++] = action.CustomNumericValue3;
                    row[j++] = action.CustomNumericValue4;

                    // DifferenceReason
                    row[j++] = action.DifferenceReason;

                    // Amélioration
                    if (action.Reduced != null)
                    {
                        // Amélioration I/E/S
                        string label;

                        if (ActionsTimingsMoveManagement.IsActionInternal(action))
                        {
                            label = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_Reduced_Internal");
                        }
                        else if (ActionsTimingsMoveManagement.IsActionExternal(action))
                        {
                            label = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_Reduced_External");
                        }
                        else if (ActionsTimingsMoveManagement.IsActionDeleted(action))
                        {
                            label = LocalizationManager.GetString("ViewModel_AnalyzeRestitution_Export_Action_Reduced_Deleted");
                        }
                        else
                        {
                            throw new ArgumentOutOfRangeException();
                        }

                        row[j++] = label;

                        row[j++] = action.Reduced.Solution;
                        row[j++] = CellContent.Percentage(action.Reduced.ReductionRatio);
                    }
                    else
                    {
                        row[j++] = null;
                        row[j++] = null;
                        row[j++] = null;
                    }
                }

                #endregion

                data[i] = row;
                i++;
            }

            _file.AddTable(sheet, actionsFormats.ToArray(), data, cellReference);


            // Ajouter une image

            cellReference.NewLine();

            i = 0;
            foreach (var action in actions)
            {
                if (action.Thumbnail != null)
                {
                    // Ajout du libellé du lien
                    var tableCellRef = new CellReference(1, tableRowIndex + (uint)i + 1);
                    _file.SetCellValue(sheet, tableCellRef, new CellContent(action.ActionId.ToString(), CellDataType.Hyperlink));

                    // Ajout du lien
                    string definedName = string.Format("Action.{0}", action.ActionId);
                    _file.CreateDefinedName(sheet, cellReference, definedName);

                    _file.AddHyperlinkToDefinedName(sheet, tableCellRef, definedName, "");

                    // Ajout du libellé
                    _file.SetCellValue(sheet, cellReference, string.Format("{0} {1}", action.WBS, action.Label ?? string.Empty));
                    cellReference.MoveRight();

                    // Ajout de l'image
                    using (var ms = new System.IO.MemoryStream(action.Thumbnail.Data))
                    {
                        var decoder   = System.Windows.Media.Imaging.BitmapDecoder.Create(ms, System.Windows.Media.Imaging.BitmapCreateOptions.PreservePixelFormat, System.Windows.Media.Imaging.BitmapCacheOption.OnLoad);
                        var size      = new System.Windows.Size(decoder.Frames[0].PixelWidth, decoder.Frames[0].PixelHeight);
                        var mimeTypes = decoder.CodecInfo.MimeTypes;

                        ImagePartType imageType;
                        if (mimeTypes.Contains("image/jpeg"))
                        {
                            imageType = ImagePartType.Jpeg;
                        }
                        else if (mimeTypes.Contains("image/bmp"))
                        {
                            imageType = ImagePartType.Bmp;
                        }
                        else if (mimeTypes.Contains("image/png"))
                        {
                            imageType = ImagePartType.Png;
                        }
                        else
                        {
                            continue;
                        }

                        string pictureName = string.Format("Thumbnail.{0}", action.ActionId);

                        uint rowsLength = _file.AddImage(sheet, action.Thumbnail.Data, imageType, action.ActionId.ToString(), pictureName, size, cellReference);

                        // On déplace la cellule active à après l'image
                        cellReference = new CellReference(1, cellReference.RowIndex + rowsLength);
                    }
                }
                i++;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Appelé lorsque l'utilisateur clique sur le bouton d'export excel
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">Les <see cref="RoutedEventArgs"/> contenant les données de l'évènement.</param>
        private async void OnExportExcelClick(object sender, RoutedEventArgs e)
        {
            var formats = new Dictionary <ColumnFormat, ExportFormatBehavior>();

            IEnumerable <DataGridColumn> columns;

            if (_target is DlhSoft.Windows.Controls.GanttChartDataGrid)
            {
                columns = ((DlhSoft.Windows.Controls.GanttChartDataGrid)_target).Columns;
            }
            else
            {
                columns = _target.Columns;
            }

            var sortedVisibileColumns = columns
                                        .Where(c => c.Visibility == Visibility.Visible)
                                        .OrderBy(c => c.DisplayIndex)
                                        .ToArray();

            // On crée les formats compatibles avec l'exporter.
            foreach (DataGridColumn column in sortedVisibileColumns)
            {
                ColumnFormat         excelFormat = new ColumnFormat();
                ExportFormatBehavior format      = Interaction.GetBehaviors(column).OfType <ExportFormatBehavior>().FirstOrDefault();

                if (format != null &&
                    format.Binding != null)
                {
                    excelFormat.Header   = format.Header ?? (column.Header != null ? column.Header.ToString() : string.Empty);
                    formats[excelFormat] = format;
                }
            }

            var data = new List <CellContent[]>();

            // On récupère les données grâce aux bindings
            foreach (object item in _target.Items)
            {
                CellContent[] row = new CellContent[formats.Count];

                int i = 0;
                foreach (var kvp in formats)
                {
                    var excelFormat = kvp.Key;
                    var behavior    = kvp.Value;
                    // Appliquer le binding
                    var element = new FrameworkElement
                    {
                        DataContext = item
                    };
                    BindingOperations.SetBinding(element, CellContentProperty, behavior.Binding);
                    object value = element.GetValue(CellContentProperty);
                    BindingOperations.ClearBinding(element, CellContentProperty);

                    row[i] = value?.ToString();

                    i++;
                }

                data.Add(row);
            }

            // Affiche la fenêtre de confirmation
            var result = IoC.Resolve <IDialogFactory>().GetDialogView <IExportDialog>().ShowExportToExcel(ExcelFormat.Xlsm);

            // On sauvegarde
            if (result.Accepts)
            {
                try
                {
                    string        fileName = ExcelExporter.GetFileNameWithExtension(result.Filename);
                    ExcelExporter file     = await ExcelExporter.Create(fileName);

                    WorksheetPart sheet = file.CreateSheet(LocalizationManager.GetString("DataGrid_ExcelExportSheetName"));
                    file.AddTable(sheet, formats.Keys.ToArray(), data.ToArray());
                    file.SaveAndClose();

                    if (result.OpenWhenCreated)
                    {
                        System.Diagnostics.Process.Start(fileName);
                    }
                }
                catch (ExcelExporter.FileAlreadyInUseExeption ex)
                {
                    TraceManager.TraceError(ex, ex.Message);
                    // Une notification a déjà été affichée dans ce cas précis
                }
                catch (Exception ex)
                {
                    TraceManager.TraceError(ex, ex.Message);
                    IoC.Resolve <IDialogFactory>().GetDialogView <IErrorDialog>().Show(ex.Message, LocalizationManager.GetString("Common_Error"), ex);
                }
            }
        }