IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Silverlight Discussion :

Export Excel MVVM


Sujet :

Silverlight

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre habitué
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Avril 2009
    Messages
    9
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2009
    Messages : 9
    Par défaut Export Excel MVVM
    bonjour
    je veux faire l'export en excel à travers l'action RelayCommand (MVVM)
    il sort un erreur dans ShowDialog
    Dialog must be user-initiated
    merci pour votre aide
    Code:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    public RelayCommand ExportToExcel
    {
    get
    {
    exportExcelCommand = new RelayCommand(()=>
    {
     SaveFileDialog sDialog = new SaveFileDialog();
                sDialog.Filter = "Excel Files(*.xls)|*.xls";
     
                if (sDialog.ShowDialog() == true)
                {
     
                    // create an instance of excel workbook
                    Workbook workbook = new Workbook();
                    // create a worksheet object
                    Worksheet worksheet = new Worksheet("Friends");
     
                    Int16 ColumnCount = 0;
                    Int16 RowCount = 0;
     
                    //Writing Column Names 
                    foreach (DataGridColumn dgcol in theGrid.Columns)
                    {
                        worksheet.Cells[0, ColumnCount] = new Cell(dgcol.Header.ToString());
                        ColumnCount++;
                    }
     
                    //Extracting values from grid and writing to excell sheet
                    //
                    foreach (object data in theGrid.ItemsSource)
                    {
                        ColumnCount = 0;
                        RowCount++;
                        foreach (DataGridColumn col in theGrid.Columns)
                        {
     
                            string strValue = "";
                            Binding objBinding = null;
                            if (col is DataGridBoundColumn)
                                objBinding = (col as DataGridBoundColumn).Binding;
                            if (col is DataGridTemplateColumn)
                            {
                                //This is a template column... let us see the underlying dependency object
                                DependencyObject objDO = (col as DataGridTemplateColumn).CellTemplate.LoadContent();
                                FrameworkElement oFE = (FrameworkElement)objDO;
                                FieldInfo oFI = oFE.GetType().GetField("TextProperty");
                                if (oFI != null)
                                {
                                    if (oFI.GetValue(null) != null)
                                    {
                                        if (oFE.GetBindingExpression((DependencyProperty)oFI.GetValue(null)) != null)
                                            objBinding = oFE.GetBindingExpression((DependencyProperty)oFI.GetValue(null)).ParentBinding;
                                    }
                                }
                            }
                            if (objBinding != null)
                            {
                                if (objBinding.Path.Path != "")
                                {
                                    PropertyInfo pi = data.GetType().GetProperty(objBinding.Path.Path);
                                    if (pi != null) strValue = Convert.ToString(pi.GetValue(data, null));
                                }
                                if (objBinding.Converter != null)
                                {
                                    if (strValue != "")
                                        strValue = objBinding.Converter.Convert(strValue, typeof(string), objBinding.ConverterParameter, objBinding.ConverterCulture).ToString();
                                    //else
                                    //    strValue = objBinding.Converter.Convert(data, typeof(string), objBinding.ConverterParameter, objBinding.ConverterCulture).ToString();
                                }
                            }
                            // writing extracted value in excell cell
                            worksheet.Cells[RowCount, ColumnCount] = new Cell(strValue);
                            ColumnCount++;
                        }
     
     
                    }
     
                    //add worksheet to workbook
                    workbook.Worksheets.Add(worksheet);
                    // get the selected file's stream
                    Stream sFile = sDialog.OpenFile();
                    workbook.Save(sFile);
    }
    }
    }

  2. #2
    Rédacteur/Modérateur


    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    19 875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2004
    Messages : 19 875
    Par défaut
    C'est du Silverlight ?

    Comment ta commande est-elle invoquée ? Via un bouton ?

  3. #3
    Membre habitué
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Avril 2009
    Messages
    9
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2009
    Messages : 9
    Par défaut
    oui c'est du silverlight l'action se fait lors de click sur le button on utilisant le Command

  4. #4
    Expert confirmé
    Avatar de Skyounet
    Homme Profil pro
    Software Engineer
    Inscrit en
    Mars 2005
    Messages
    6 380
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 6 380
    Par défaut
    Oui il ne faut pas utiliser de Command pour ca, il faut que tu t'abonnes au Click et que la tu appelles ta Command a la main.

    Pourquoi ? Parce que.

  5. #5
    Membre habitué
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Avril 2009
    Messages
    9
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2009
    Messages : 9
    Par défaut
    comment j' appelles la Command a la main? moi j"utilise l'architecture MVVM donc je veux que tout le code dans le ViewModel

  6. #6
    Rédacteur
    Avatar de Nathanael Marchand
    Homme Profil pro
    Expert .Net So@t
    Inscrit en
    Octobre 2008
    Messages
    3 615
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Expert .Net So@t
    Secteur : Conseil

    Informations forums :
    Inscription : Octobre 2008
    Messages : 3 615
    Par défaut
    Citation Envoyé par faouzialoui Voir le message
    comment j' appelles la Command a la main? moi j"utilise l'architecture MVVM donc je veux que tout le code dans le ViewModel
    Le principe est le découplage. Ton viewmodel implémente une interface, dans cette interface il y'a une méthode d'execution de la commande. Il faut se brancher sur le OnClick et zou!

Discussions similaires

  1. [CR] export Excel (plusieurs feuilles)
    Par kaiserben dans le forum SAP Crystal Reports
    Réponses: 4
    Dernier message: 21/11/2005, 15h03
  2. [PEAR][Spreadsheet] PHP/EXPORT EXCEL : nombre de formats de cellule différents !
    Par joe_le_mort dans le forum Bibliothèques et frameworks
    Réponses: 6
    Dernier message: 27/10/2005, 17h21
  3. Tableau dynamique, export excel
    Par ptitepunk dans le forum Access
    Réponses: 1
    Dernier message: 14/10/2005, 12h21
  4. Réponses: 5
    Dernier message: 29/09/2005, 13h55
  5. Export Excel
    Par jfn dans le forum Access
    Réponses: 5
    Dernier message: 15/11/2004, 01h55

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo