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

Windows Forms Discussion :

Exporter une dataGridView vers Excel


Sujet :

Windows Forms

  1. #1
    Membre du Club
    Inscrit en
    Juillet 2006
    Messages
    48
    Détails du profil
    Informations forums :
    Inscription : Juillet 2006
    Messages : 48
    Points : 41
    Points
    41
    Par défaut Exporter une dataGridView vers Excel
    Bonjour,
    je developpes une windows application en C# avec visual studio 2005, et j'utilise une DataGridView.
    je veux savoir s'il y a une fonction standard qui nous permet d'exporter le dataGridView vers un fichier Excel?
    Merci d'avance

  2. #2
    Membre habitué
    Profil pro
    Inscrit en
    Novembre 2006
    Messages
    264
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 264
    Points : 144
    Points
    144
    Par défaut
    Un copier / coller ne te suffirait pas ???

  3. #3
    Nouveau membre du Club
    Inscrit en
    Avril 2007
    Messages
    31
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 31
    Points : 25
    Points
    25
    Par défaut
    Bonjour,
    J'ai eu le même probleme que toi il y a 4 jours.

    Je n'ai pas trouver de fonction standart néanmoins, on peut faire l'exportation d'une datagrid vers excel.
    plusieurs solutions :

    La plus simple a vérifier
    tu ecrit dans un fichier texte avec des \n et \t pour changer de case dont tu force l'extention en .xls
    Marche en C/C++ avec printf

    La complexe mais plus "pro" :
    Tu utilise des modules de office et excel
    Sa n'a pas marcher pour moi
    Regarde sur google ou code projet

    Soit une solution alternative :
    tu transforme en xml et puis et le met dans excel
    C'est ce qui ma convenu

    Voici mon code qui en bien traffiquer car il ecrit tout en string dans excel
    car j'arrive pas encore a distinguer le type du contenu de mes cellules

    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
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
     public static void exportToExcel2(DataGridView source, string fileName)
            {
     
                System.IO.StreamWriter excelDoc;
     
                excelDoc = new System.IO.StreamWriter(fileName);
                const string startExcelXML = "<xml version>\r\n<Workbook " +
                      "xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\r\n" +
                      " xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\n " +
                      "xmlns:x=\"urn:schemas-    microsoft-com:office:" +
                      "excel\"\r\n xmlns:ss=\"urn:schemas-microsoft-com:" +
                      "office:spreadsheet\">\r\n <Styles>\r\n " +
                      "<Style ss:ID=\"Default\" ss:Name=\"Normal\">\r\n " +
                      "<Alignment ss:Vertical=\"Bottom\"/>\r\n <Borders/>" +
                      "\r\n <Font/>\r\n <Interior/>\r\n <NumberFormat/>" +
                      "\r\n <Protection/>\r\n </Style>\r\n " +
                      "<Style ss:ID=\"BoldColumn\">\r\n <Font " +
                      "x:Family=\"Swiss\" ss:Bold=\"1\"/>\r\n </Style>\r\n " +
                      "<Style     ss:ID=\"StringLiteral\">\r\n <NumberFormat" +
                      " ss:Format=\"@\"/>\r\n </Style>\r\n <Style " +
                      "ss:ID=\"Decimal\">\r\n <NumberFormat " +
                      "ss:Format=\"0.0000\"/>\r\n </Style>\r\n " +
                      "<Style ss:ID=\"Integer\">\r\n <NumberFormat " +
                      "ss:Format=\"0\"/>\r\n </Style>\r\n <Style " +
                      "ss:ID=\"DateLiteral\">\r\n <NumberFormat " +
                      "ss:Format=\"mm/dd/yyyy;@\"/>\r\n </Style>\r\n " +
                      "</Styles>\r\n ";
                const string endExcelXML = "</Workbook>";
     
                int rowCount = 0;
                int sheetCount = 1;
     
                excelDoc.Write(startExcelXML);
                excelDoc.Write("<Worksheet ss:Name=\"Sheet" + sheetCount + "\">");
                excelDoc.Write("<Table>");
                excelDoc.Write("<Row>");
                for (int x = 0; x < source.ColumnCount; x++)
                {
                    excelDoc.Write("<Cell ss:StyleID=\"BoldColumn\"><Data ss:Type=\"String\">");
                    excelDoc.Write(source.Columns[x].Name);
                    excelDoc.Write("</Data></Cell>");
                }
                excelDoc.Write("</Row>");
     
                DataGridViewCheckBoxCell maCell = new DataGridViewCheckBoxCell();
     
                for (int x = 0; x < source.RowCount; x++)//int x = 0; x < source.RowCount;x++
                {
                    rowCount++;
                    //if the number of rows is > 64000 create a new page to continue output
                    if (rowCount == 64000)
                    {
                        rowCount = 0;
                        sheetCount++;
                        excelDoc.Write("</Table>");
                        excelDoc.Write(" </Worksheet>");
                        excelDoc.Write("<Worksheet ss:Name=\"Sheet" + sheetCount + "\">");
                        excelDoc.Write("<Table>");
                    }
                    excelDoc.Write("<Row>"); //ID=" + rowCount + "
                    for (int y = 0; y < source.ColumnCount; y++)
                    {
                        System.Type rowType;
                        Object tito = new object();
                        rowType = source[y, x].ValueType;
                        tito = (System.Type)source[y, x].ValueType;
                        string titi = Convert.ToString(source[y, x].Value);
                        //maCell = (DataGridViewCheckBoxCell)this.dataGridViewItems.Rows[e.RowIndex].Cells[e.ColumnIndex];
                        string temp = "System.String";
                        switch (temp)//rowType.ToString()
                        {
                            case "System.String":
                                string XMLstring = Convert.ToString(source[y, x].Value);
                                XMLstring = XMLstring.Trim();
                                XMLstring = XMLstring.Replace("&", "&");
                                XMLstring = XMLstring.Replace(">", ">");
                                XMLstring = XMLstring.Replace("<", "<");
                                excelDoc.Write("<Cell ss:StyleID=\"StringLiteral\">" +
                                               "<Data ss:Type=\"String\">");
                                excelDoc.Write(XMLstring);
                                excelDoc.Write("</Data></Cell>");
                                break;
     
                            case "System.Boolean":
                                excelDoc.Write("<Cell ss:StyleID=\"StringLiteral\">" +
                                            "<Data ss:Type=\"String\">");
                                excelDoc.Write(source[y, x].ToString());
                                excelDoc.Write("</Data></Cell>");
                                break;
                            case "System.Int16":
                            case "System.Int32":
                            case "System.Int64":
                            case "System.Byte":
                                excelDoc.Write("<Cell ss:StyleID=\"Integer\">" +
                                        "<Data ss:Type=\"Number\">");
                                excelDoc.Write(source[y, x].ToString());
                                excelDoc.Write("</Data></Cell>");
                                break;
                            case "System.Decimal":
                            case "System.Double":
                                excelDoc.Write("<Cell ss:StyleID=\"Decimal\">" +
                                      "<Data ss:Type=\"Number\">");
                                excelDoc.Write(source[y, x].ToString());
                                excelDoc.Write("</Data></Cell>");
                                break;
                            case "System.DBNull":
                                excelDoc.Write("<Cell ss:StyleID=\"StringLiteral\">" +
                                      "<Data ss:Type=\"String\">");
                                excelDoc.Write("");
                                excelDoc.Write("</Data></Cell>");
                                break;
                            //default:
                            //throw (new Exception(rowType.ToString() + " not handled."));
                        }
                    }
                    excelDoc.Write("</Row>");
     
                }
                excelDoc.Write("<Row>");
                DateTime Date2 = new DateTime();
                Date2 = DateTime.Now;
                string dateS = Date2.ToString("MM_dd_yyyy_hh_mm");
                excelDoc.Write("<Cell ss:StyleID=\"StringLiteral\">" +
                               "<Data ss:Type=\"String\">");
                excelDoc.Write(dateS);
                excelDoc.Write("</Data></Cell>");
                excelDoc.Write("</Row>");
     
                excelDoc.Write("</Table>");
                excelDoc.Write(" </Worksheet>");
                excelDoc.Write(endExcelXML);
                excelDoc.Close();
            }
    Bon courage

Discussions similaires

  1. [Débutant] Exporter une datagridview vers un fichier Excel
    Par WIEM7 dans le forum Windows Forms
    Réponses: 2
    Dernier message: 26/04/2012, 15h46
  2. Réponses: 0
    Dernier message: 16/04/2009, 22h37
  3. [A-00] exporter une liste vers excel
    Par Slici dans le forum VBA Access
    Réponses: 5
    Dernier message: 06/03/2009, 14h49
  4. exporter une query vers excel
    Par mouaa dans le forum VBA Access
    Réponses: 7
    Dernier message: 27/12/2007, 10h33
  5. exporter une requete vers excel
    Par tzinzin dans le forum VBA Access
    Réponses: 3
    Dernier message: 02/11/2007, 16h08

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