Bonjour à toutes et à tous,

JE reprend actuellement une application utilisant une connexion vers une base SQL Server.

Sur une des pages, je présente une dizaine de gridview contenant pas mal de données (jusqu'à 45000 lignes, une dizaine de colonnes).

Je dois pouvoir exporter ces données vers Excel, en une seule fois, sans que l'utilisateur n'ai à fournir de nom de fichier, ni choisir l'endroit où les déposer (configuré dans le web.config)

J'utilise alors le code suivant:
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
 
        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);
            }
        }
mon problème est que, lorsque j'ai peu de données, tout ce passe bien, mais lorsque j'en ai beaucoup, ca met des heures....

qulqu'un peut-il m'aider?

Merci d'avance

doudou