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 :

Exporter datagridView en excel


Sujet :

C#

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Juin 2009
    Messages
    111
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Juin 2009
    Messages : 111
    Points : 65
    Points
    65
    Par défaut Exporter datagridView en excel
    Bonjour,
    j'aimerai exporter mon datagrid view en excel, j'ai trouver un lien vers ceci dans le forum mais je n'arrive pas a bien le comprendre. voici le 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
     
    private void Button1_Click(object sender, System.EventArgs e)
    		{
    			DataSet dsResult = new DataSet();
    			dsResult = (DataSet) Session["DataSetResult"];
    			DataGrid1.DataSource = dsResult;
    			DataGrid1.DataBind();
     
     
    				//Response.Clear(); 
    				Response.Buffer = true; 
    				Response.ContentType = "application/vnd.ms-excel"; 
    				Response.Charset = ""; 
    				this.EnableViewState = false; 
    				System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); 
    				System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); 
     
    				//optional function call to convert the controls to string literals 
    				ClearControls(DataGrid1); //name the grid which u want to convert to excel 
    				DataGrid1.RenderControl(oHtmlTextWriter); 
    				Response.Write(oStringWriter.ToString()); 
     
    				Response.End(); 
     
    			}
    J'aimerai savoir que représente le Response, et comment faire pour donner a l'utilisateur le choix du nom du fichier excel

    merci pour votre aide

  2. #2
    Membre du Club
    Profil pro
    Inscrit en
    Février 2004
    Messages
    59
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2004
    Messages : 59
    Points : 49
    Points
    49
    Par défaut
    Je pense pas que l'on puisse répondre à ta question dans le sens ou la variable Response n'est pas une variable locale. On a donc pas la déclaration dans la fonction (probablement une variable d'instance). Ceci-dit ça ressemle fortement à une variable de type Stream. Certainement l'un de ces descendants.

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Juin 2009
    Messages
    111
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Juin 2009
    Messages : 111
    Points : 65
    Points
    65
    Par défaut

    Qui aurait un lien simple pour que je puisse comprendre et reproduire l'exporte mon gridView en fichier excel ?

    Merci d'avance

  4. #4
    Membre expérimenté
    Homme Profil pro
    Inscrit en
    Juillet 2007
    Messages
    1 277
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Réunion

    Informations forums :
    Inscription : Juillet 2007
    Messages : 1 277
    Points : 1 521
    Points
    1 521
    Par défaut
    Response est un objet de type HttpResponse (le flux de réponse retourné au client).

    Ce que ce code fait, c'est qu'il efface le contenu actuel de la réponse, spécifie que c'est "soit-disant" un fichier Excel (parce que ce n'est pas vraiment le cas, c'est un flux HTML), désactive le ViewState, créé un stream, écrit le rendu HTML du DataGridView dans le stream qui est finalement écrit dans Response. Response est ensuite fermé pour être envoyé au client.

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Juin 2009
    Messages
    111
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Juin 2009
    Messages : 111
    Points : 65
    Points
    65
    Par défaut
    Je vois, mais c'est propre a ASP.Net, donc ca ne me sera pas utile je crois,
    Moi j'utilise application windows form en c#

  6. #6
    Membre expérimenté
    Homme Profil pro
    Inscrit en
    Juillet 2007
    Messages
    1 277
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Réunion

    Informations forums :
    Inscription : Juillet 2007
    Messages : 1 277
    Points : 1 521
    Points
    1 521
    Par défaut
    Exact.

  7. #7
    Membre du Club
    Profil pro
    Inscrit en
    Juin 2009
    Messages
    111
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Juin 2009
    Messages : 111
    Points : 65
    Points
    65
    Par défaut
    Une idée sur comment je dois faire pour c# ?

  8. #8
    Expert éminent Avatar de Graffito
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    5 993
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 5 993
    Points : 7 903
    Points
    7 903
    Par défaut
    Voir :
    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
        * #region ExporterDataGridVersExcel Surchargé Type #1
        * ///<summary>
        * ///Permet d'exporter un DataGrid vers excel
        * ///</summary>
        * /// <param name="dgView">Data Grid Source des données à Exporter vers Excel</param>
        * ///<param name="unFichier">Fichier Excel de destination des données</param>
        * ///<param name="strEnteteDeFichier">Libellé de l'en-tête du fichier à générer</param>
        * public void ExporterDataGridVersExcel(DataGridView dgView, String unFichier, string strEnteteDeFichier)
        * {
        * int i = 0;
        * int j = 0;
        * try
        * {
        * ExcelApplication excel = new ExcelApplication();
        * Workbook exbook = (Workbook)excel.Workbooks.Add(Missing.Value);
        * Worksheet exsheet = (Worksheet)excel.ActiveSheet;
        * //Double[] Totaux= new Double[4];
        *
        * //Mise en forme de l'en-tête de la feuille Excel
        * exsheet.Cells[1, 1] = strEnteteDeFichier;
        * Range r = exsheet.get_Range(Convert.ToChar(65 + i).ToString() + "1", Missing.Value);
        * r.Interior.ColorIndex = XlColorIndex.xlColorIndexAutomatic;
        * r.Font.Bold = true;
        * r.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic, Missing.Value);
        * r.EntireColumn.AutoFit();//Fin de la mise en forme de l'en-tête.
        *
        * foreach (DataGridViewColumn ch in dgView.Columns)
        * {
        * r = exsheet.get_Range(Convert.ToChar(65 + i).ToString() + "1", Missing.Value);
        * exsheet.Cells[2, i + 1] = ch.Name.Trim();
        * r.Interior.ColorIndex = XlColorIndex.xlColorIndexAutomatic;
        * r.Font.Bold = true;
        * r.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic, Missing.Value);
        * r.EntireColumn.AutoFit();
        * i++;
        * }
        * j = 3;
        *
        * foreach (DataGridViewRow uneLigne in dgView.Rows)
        * {
        * i = 1;
        * foreach (DataGridViewColumn uneColonne in dgView.Columns)
        * {
        * r = exsheet.get_Range(Convert.ToChar(65 + i - 1).ToString() + j.ToString(), Missing.Value);
        * exsheet.Cells[j, i] = "'" + uneLigne.Cells[uneColonne.Name].Value.ToString().Trim();
        * r.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic, Missing.Value);
        * r.EntireColumn.AutoFit();
        * i++;
        * }
        * exsheet.Columns.AutoFit();
        * j++;
        * }
        * exsheet.SaveAs(unFichier, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
        * excel.Quit();
        * }
        * catch (Exception ex)
        * {
        * throw (ex);
        * }
        * }//ExporterDataGridVersExcel
        * #endregion //ExporterDataGridVersExcel
    " Le croquemitaine ! Aaaaaah ! Où ça ? " ©Homer Simpson

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

Discussions similaires

  1. export datagridview vers excel
    Par dgkourou dans le forum C#
    Réponses: 5
    Dernier message: 24/06/2014, 11h56
  2. Export de données DataGridview Vers Excel
    Par Le gris dans le forum C#
    Réponses: 3
    Dernier message: 28/06/2012, 10h38
  3. Export Datagridview vers Excel
    Par Pierro7825 dans le forum C#
    Réponses: 4
    Dernier message: 01/08/2011, 12h21
  4. Problème d'export de datagridview vers excel
    Par holdboy dans le forum C#
    Réponses: 2
    Dernier message: 15/06/2009, 21h54
  5. Exporter une dataGridView vers Excel
    Par drayif dans le forum Windows Forms
    Réponses: 2
    Dernier message: 02/08/2007, 17h45

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