Bonjour

Je recherche à remplir une feuille d'un classeur suivant un fichier template en xlt.

Voici mon 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
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
 
public bool ImportDataGridToExcel_WithTemplate(string filepath, string templatepath, int startrow, int startcolumn, DataView dv)
        {
            bool resultflag = false;
            int indexofsheetname = 0;
            //Gestion du fichier à écrire
            if (File.Exists(filepath))
            {
                File.Delete(filepath);
            }
            try
            {
               msExcel = new Microsoft.Office.Interop.Excel.Application();
               if (File.Exists(templatepath))
               {
                   //Ouverture du template 
                   workbook = msExcel.Workbooks.Open(templatepath,0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "",
                    true, false, 0, true, false, false);
               }
                //Sélection de la feuille
                sheets = workbook.Sheets;
                for (int i = 1; i <= sheets.Count; i++)
                {
                    //sélection du worksheet courant
                    worksheet = (Worksheet)sheets.get_Item(i);
                    //Test si la feuille est la bonne
                    if (worksheet.Name.ToString().Equals("LISTE"))
                    {
                        //Oui, sauvegarde de l'index
                        indexofsheetname = i;
                    }
                }
                worksheet = (Worksheet)sheets.get_Item(indexofsheetname);
                Row = startrow;
                Col = startcolumn;
 
                //Remplir la feuille
                for (int i = 0; i < dv.Table.Rows.Count; i++)
                {
 
                    for (int j = 0; j < dv.Table.Columns.Count; j++)
                    {
                        fillExcelCell(worksheet, Row, Col++, dv[i][j].ToString());
                    }
                    Col = 1;
                    Row++;
                }
                //Fermeture d'Excel
                try
                {
                    workbook.Close(true, filepath, Type.Missing);
                    msExcel.UserControl = false;
                    msExcel.Quit();
                    msExcel = null;
                    killExcel();
 
                }
                catch (COMException)
                {
                    Console.WriteLine("Erreur dans la fermeture automatique d'Excel");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Erreur :" + ex.Message);
            }
           return resultflag;;
        }
Code pour remplir une cellule :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
 
        private void fillExcelCell(Microsoft.Office.Interop.Excel.Worksheet worksheet, int row, int col, Object Value)
        {
            Range rng = (Range)worksheet.Cells[row, col];
            rng.Select();
            rng.Value2 = Value.ToString();
            rng.Columns.EntireColumn.AutoFit();
            rng.Borders.Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;
            rng.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
            rng.Borders.ColorIndex = Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic;
        }
je n'arrive pas à remplir ma fiche. Lorsque j'arrive à la sélection de la cellule
j'ai le message d'erreur suivant :
La méthode Select de la classe Range à échoué.
Je n'arrive pas à solutionner le problème. Il faut savoir que la fonction fillExcelCell fonctionne correctement quand le fichier Excel est nouveau.
Auriez vous une piste à explorer?
Cordialement