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

C# Discussion :

Export de données DataGridview Vers Excel


Sujet :

C#

  1. #1
    Membre averti
    Homme Profil pro
    Ingénieur Développement Logiciel
    Inscrit en
    Septembre 2005
    Messages
    285
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Activité : Ingénieur Développement Logiciel
    Secteur : Service public

    Informations forums :
    Inscription : Septembre 2005
    Messages : 285
    Points : 421
    Points
    421
    Par défaut Export de données DataGridview Vers Excel
    salut à tous,
    en cherchant sur le forum je suis tombé sur un sujet en vbnet qui permet d'exporter les données d'un datagridview vers excel http://www.developpez.net/forums/d78...xcel-checkbox/

    J'ai essayé d'Adapter ce code en c#,
    Code c# : 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
     
    //Verifions si le datagrid est vid ou non
                if (dtGridInsertion.ColumnCount == 0){
                    MessageBox.Show("Aucune données à exporter !", "Erreur !", MessageBoxButtons.OK);
                }
                if (dtGridInsertion.RowCount == 0){
                    MessageBox.Show("Aucune données à exporter !", "Erreur !", MessageBoxButtons.OK);
                }
                //Créons un DataSet pour recuperer les données
                DataSet myDs,myDs2;
                //On ajoute des tables au ds 
                myDs.Tables.Add();myDs2.Tables.Add();
                //On ajoute des colonnes aux tables
                for (int i = 0; i <= dtGridInsertion.ColumnCount - 1; i++)
                {myDs.Tables[0].Columns.Add();}
                //On ajoute des lignes aux tables
                DataRow myDrw;
                for (int i = 0; i <= dtGridInsertion.RowCount - 1; i++)
                {
                    myDrw = myDs.Tables[0].NewRow();
                    for (int j = 0; j <= dtGridInsertion.ColumnCount - 1; j++)
                    {
                        myDrw[j] = dtGridInsertion.Rows[i].Cells[j].Value;
                    }
                    myDs.Tables[0].Rows.Add(myDrw);
                }
                Microsoft.Office.Interop.Excel.ApplicationClass myExcel;
                Microsoft.Office.Interop.Excel.Workbook wBook;
                Microsoft.Office.Interop.Excel.Worksheet wSheet;
                wBook = myExcel.Workbooks.Add();
                wSheet = (Microsoft.Office.Interop.Excel.Worksheet)wBook.ActiveSheet;
     
                System.Data.DataTable myDt = myDs.Tables[0];
                System.Data.DataColumn myDc;
                System.Data.DataRow myDr;
                int col = 0; int row = 0;
                foreach(myDc in myDt.Columns)
                {
     
                    col=col+1;
                    if (col==1) myDc.ColumnName="CODE";
                    if (col==2) myDc.ColumnName="DATE INSERTION";
                    if (col==3) myDc.ColumnName="ANNONCEUR";
                    if (col==4) myDc.ColumnName="MARQUE";
                    if (col==5) myDc.ColumnName="PRODUIT";
                    if (col==6) myDc.ColumnName="SUPPORT";
                    if (col==7) myDc.ColumnName="MESSAGE";
                    if (col==8) myDc.ColumnName="LANGUE";
                    if (col==9) myDc.ColumnName="ACTION MEDIA";
                    if (col==10) myDc.ColumnName="DUREE";
                    if (col==11) myDc.ColumnName="COUT";
                    myExcel.Cells[1, col] = myDc.ColumnName;
                }
                foreach (myDr Dr in myDt.Rows)
                {
                    row = row + 1;
                    col = 0;
                    foreach (myDc Dc in myDt.Columns)
                    {
                        col = col + 1;
                        myExcel.Cells[row + 1, col] = myDr[myDc.ColumnName];
                    }
                }
                wSheet.Columns.AutoFit();
                string myFileName="C:\\export.xls";
                Boolean myFileOpen = false;
                try
                {
                    System.IO.FileStream fileTemp= System.IO.File.OpenWrite(myFileName);
                    fileTemp.Close();
                }
                catch (Exception myEx)
                { myFileOpen = false; }
                if(System.IO.File.Exists(myFileName)) System.IO.File.Delete(myFileName);
                wBook.SaveAs(myFileName);
                myExcel.Workbooks.Open(myFileName);
                myExcel.Visible = true;
    Et là j'ai une erreur au niveau de foreach:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    foreach(myDc in myDt.Columns)
                {
    Error 2 Type and identifier are both required in a foreach statement ...
    quelqu'un a une idée?

    Merci d'avance!

  2. #2
    Membre averti
    Homme Profil pro
    Ingénieur Développement Logiciel
    Inscrit en
    Septembre 2005
    Messages
    285
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Activité : Ingénieur Développement Logiciel
    Secteur : Service public

    Informations forums :
    Inscription : Septembre 2005
    Messages : 285
    Points : 421
    Points
    421
    Par défaut
    Et la je rectifie les lignes
    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
     
    //Verifions si le datagrid est vid ou non
                if (dtGridInsertion.ColumnCount == 0){
                    MessageBox.Show("Aucune données à exporter !", "Erreur !", MessageBoxButtons.OK);
                }
                if (dtGridInsertion.RowCount == 0){
                    MessageBox.Show("Aucune données à exporter !", "Erreur !", MessageBoxButtons.OK);
                }
                //Créons un DataSet pour recuperer les données
                DataSet myDs,myDs2;
                //On ajoute des tables au ds 
                myDs.Tables.Add();myDs2.Tables.Add();
                //On ajoute des colonnes aux tables
                for (int i = 0; i <= dtGridInsertion.ColumnCount - 1; i++)
                {myDs.Tables[0].Columns.Add();}
                //On ajoute des lignes aux tables
                DataRow myDrw;
                for (int i = 0; i <= dtGridInsertion.RowCount - 1; i++)
                {
                    myDrw = myDs.Tables[0].NewRow();
                    for (int j = 0; j <= dtGridInsertion.ColumnCount - 1; j++)
                    {
                        myDrw[j] = dtGridInsertion.Rows[i].Cells[j].Value;
                    }
                    myDs.Tables[0].Rows.Add(myDrw);
                }
                Microsoft.Office.Interop.Excel.ApplicationClass myExcel;
                Microsoft.Office.Interop.Excel.Workbook wBook;
                Microsoft.Office.Interop.Excel.Worksheet wSheet;
                wBook = myExcel.Workbooks.Add();
                wSheet = (Microsoft.Office.Interop.Excel.Worksheet)wBook.ActiveSheet;
     
                System.Data.DataTable myDt = myDs.Tables[0];
                System.Data.DataColumn myDc;
                System.Data.DataRow myDr;
                int col = 0; int row = 0;
                foreach (DataColumn Dc in myDt.Columns)
                {//code,date_insertion,annonceur,marque,produit,support,message,langue,actionmedia,duree,cout
                    //string nomColonne = { "CODE", "DATE INSERTION", "ANNONCEUR", "MARQUE", "PRODUIT", "SUPPORT", "MESSAGE", "LANGUE", "ACTION MEDIA", "DUREE","COUT" };
                    //foreach (col c in nomColonne) { myExcel.Cells[1, col] = nomColonne[col - 1]; }
                    col=col+1;
                    if (col==1) myDc.ColumnName="CODE";
                    if (col==2) myDc.ColumnName="DATE INSERTION";
                    if (col==3) myDc.ColumnName="ANNONCEUR";
                    if (col==4) myDc.ColumnName="MARQUE";
                    if (col==5) myDc.ColumnName="PRODUIT";
                    if (col==6) myDc.ColumnName="SUPPORT";
                    if (col==7) myDc.ColumnName="MESSAGE";
                    if (col==8) myDc.ColumnName="LANGUE";
                    if (col==9) myDc.ColumnName="ACTION MEDIA";
                    if (col==10) myDc.ColumnName="DUREE";
                    if (col==11) myDc.ColumnName="COUT";
                    myExcel.Cells[1, col] = myDc.ColumnName;
                }
                foreach (DataRow Dr in myDt.Rows)
                {
                    row = row + 1;
                    col = 0;
                    foreach (DataColumn Dc in myDt.Columns)
                    {
                        col = col + 1;
                        myExcel.Cells[row + 1, col] = myDr[myDc.ColumnName];
                    }
                }
                wSheet.Columns.AutoFit();
                string myFileName="C:\\export.xls";
                Boolean myFileOpen = false;
                try
                {
                    System.IO.FileStream fileTemp= System.IO.File.OpenWrite(myFileName);
                    fileTemp.Close();
                }
                catch (Exception myEx)
                { myFileOpen = false; }
                if(System.IO.File.Exists(myFileName)) System.IO.File.Delete(myFileName);
                wBook.SaveAs(myFileName);
                myExcel.Workbooks.Open(myFileName);
                myExcel.Visible = true;
    et j'ai d'autres erreurs au niveau de:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    wBook = myExcel.Workbooks.Add();
    :
    Error 2 No overload for method 'Add' takes '0' argument
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    wBook.SaveAs(myFileName);
                myExcel.Workbooks.Open(myFileName);
    :
    Error 3 No overload for method 'SaveAs' takes '1' arguments;
    Error 4 No overload for method 'Open' takes '1' arguments
    Bon là je suis dans les vap!!!

  3. #3
    Membre averti
    Homme Profil pro
    Ingénieur Développement Logiciel
    Inscrit en
    Septembre 2005
    Messages
    285
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Activité : Ingénieur Développement Logiciel
    Secteur : Service public

    Informations forums :
    Inscription : Septembre 2005
    Messages : 285
    Points : 421
    Points
    421
    Par défaut
    Salut à tous,
    je viens de résoudre le problème. Voilà le code définitif:

    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
     
    //Verifions si le datagrid est vid ou non
                if (dtGridInsertion.ColumnCount == 0)
                {
                    MessageBox.Show("Aucune données à exporter !", "Erreur !", MessageBoxButtons.OK);
                }
                if (dtGridInsertion.RowCount == 0)
                {
                    MessageBox.Show("Aucune données à exporter !", "Erreur !", MessageBoxButtons.OK);
                }
                //Créons un DataSet pour recuperer les données
                DataSet myDs = new DataSet();
                DataSet myDs2 = new DataSet();
                //On ajoute des tables au ds 
                myDs.Tables.Add();
                myDs2.Tables.Add();
                //On ajoute des colonnes aux tables
                for (int i = 0; i <= dtGridInsertion.ColumnCount - 1; i++)
                { myDs.Tables[0].Columns.Add(); }
                //On ajoute des lignes aux tables
                DataRow myDrw;
                for (int i = 0; i <= dtGridInsertion.RowCount - 1; i++)
                {
                    myDrw = myDs.Tables[0].NewRow();
                    for (int j = 0; j <= dtGridInsertion.ColumnCount - 1; j++)
                    {
                        myDrw[j] = dtGridInsertion.Rows[i].Cells[j].Value;
                    }
                    myDs.Tables[0].Rows.Add(myDrw);
                }
                Microsoft.Office.Interop.Excel.ApplicationClass myExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
                Microsoft.Office.Interop.Excel.Workbook wBook;
                Microsoft.Office.Interop.Excel.Worksheet wSheet;
                object oMissing = System.Reflection.Missing.Value;
                wBook = myExcel.Workbooks.Add(oMissing);
                wSheet = (Microsoft.Office.Interop.Excel.Worksheet)wBook.ActiveSheet;
     
                System.Data.DataTable myDt = myDs.Tables[0];
                //System.Data.DataColumn myDc;
                //System.Data.DataRow myDr;
                int col = 0; int row = 0;
                foreach (DataColumn Dc in myDt.Columns)
                {//code,date_insertion,annonceur,marque,produit,support,message,langue,actionmedia,duree,cout                
                    col = col + 1;
                    if (col == 1) Dc.ColumnName = "CODE";
                    if (col == 2) Dc.ColumnName = "DATE INSERTION";
                    if (col == 3) Dc.ColumnName = "ANNONCEUR";
                    if (col == 4) Dc.ColumnName = "MARQUE";
                    if (col == 5) Dc.ColumnName = "PRODUIT";
                    if (col == 6) Dc.ColumnName = "SUPPORT";
                    if (col == 7) Dc.ColumnName = "MESSAGE";
                    if (col == 8) Dc.ColumnName = "LANGUE";
                    if (col == 9) Dc.ColumnName = "ACTION MEDIA";
                    if (col == 10) Dc.ColumnName = "DUREE";
                    if (col == 11) Dc.ColumnName = "COUT";
                    myExcel.Cells[1, col] = Dc.ColumnName;
                }
                foreach (DataRow Dr in myDt.Rows)
                {
                    row = row + 1;
                    col = 0;
                    foreach (DataColumn Dc in myDt.Columns)
                    {
                        col = col + 1;
                        myExcel.Cells[row + 1, col] = Dr[Dc.ColumnName];
                    }
                }
                wSheet.Columns.AutoFit();
                string myFileName = "C:\\export.xls";
                Boolean myFileOpen = false;
                try
                {
                    System.IO.FileStream fileTemp = System.IO.File.OpenWrite(myFileName);
                    myFileOpen = true;
                    fileTemp.Close();
                }
                catch (Exception myEx)
                { myFileOpen = false; MessageBox.Show("Erreur fichier deja ouvert!!! \n\t " + myEx.Message, "Erreur Export"); }
                if (System.IO.File.Exists(myFileName)) System.IO.File.Delete(myFileName);
                wBook.SaveAs(myFileName, oMissing, oMissing, oMissing, oMissing, oMissing, XlSaveAsAccessMode.xlExclusive, oMissing, oMissing, oMissing, oMissing, oMissing);
                myExcel.Workbooks.Open(myFileName, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
                myExcel.Visible = true;
    Et ça marche! Si quelqu'un peu optimiser ce code, il sera le bienvenue!

  4. #4
    Membre habitué
    Inscrit en
    Novembre 2006
    Messages
    262
    Détails du profil
    Informations forums :
    Inscription : Novembre 2006
    Messages : 262
    Points : 163
    Points
    163
    Par défaut
    bonjour ,
    merci Le gris pour ce bout code,très pratique.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Exportation de données access vers excel
    Par garsflo dans le forum VBA Access
    Réponses: 3
    Dernier message: 29/05/2007, 01h13
  2. Export des données MsAccess vers Excel
    Par vautour29 dans le forum Access
    Réponses: 3
    Dernier message: 26/01/2007, 20h14
  3. exportation de données access vers excel
    Par ptitemel dans le forum Microsoft Office
    Réponses: 3
    Dernier message: 12/07/2006, 14h24
  4. exporter des données access vers excel
    Par Sebastien_INR59 dans le forum Access
    Réponses: 8
    Dernier message: 20/06/2006, 23h29
  5. Export de données Oracle vers Excel via PHP
    Par Yanos dans le forum Oracle
    Réponses: 1
    Dernier message: 11/01/2006, 18h11

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