| 12
 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
 
 |  
        public static void ExporterDataGridVersExcel(GridView dgView, String unFichier, string strEnteteDeFichier)
        {
            int i = 0;
            int j = 1;
            try
            {
                // initialisation du fichier excel
                ExcelApplication excel = new ExcelApplication();
                Workbook exbook = (Workbook)excel.Workbooks.Add(Missing.Value);
                Worksheet exsheet = (Worksheet)excel.ActiveSheet;
                //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.
                if (dgView.Rows.Count > 0)
                {
                    // initialisation colonnes
                    foreach (TableCell ch in dgView.Rows[0].Cells )
                    {
                        r = exsheet.get_Range(Convert.ToChar(65 + i).ToString() + "1", Missing.Value);
                        exsheet.Cells[j, i + 1] = ((System.Web.UI.WebControls.DataControlFieldCell)(ch)).ContainingField.HeaderText;
                        r.Interior.ColorIndex = XlColorIndex.xlColorIndexAutomatic;
                        r.Font.Bold = true;
                        r.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic, Missing.Value);
                        r.EntireColumn.AutoFit();
                        i++;
                    }
                    j++;
                    foreach (GridViewRow uneLigne in dgView.Rows)
                    {
                        //foreach (DataControlField uneColonne in dgView.Columns
                        for (int nocolonne = 0; nocolonne < i ; nocolonne++)
                        {
                            r = exsheet.get_Range(Convert.ToChar(65 + nocolonne).ToString() + j.ToString(), Missing.Value);
                            exsheet.Cells[j, nocolonne + 1] = "'" +
                                uneLigne.Cells[nocolonne].Text.ToString().Trim();
                            //r.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic, Missing.Value);
                            //r.EntireColumn.AutoFit();
                        }
                        //exsheet.Columns.AutoFit();
                        j++;
                    }
                }
                exsheet.Columns.AutoFit();
                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);
            }
        } | 
Partager