[C#2.0]Problème export Excel
Bonjour,
J'ai un problème avec cette procédure d'exportation vers Excel :
Code:
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
| private void BtnExportExcel_Click(object sender, EventArgs e)
{
Excel.Application xlApp = new Excel.Application();
if (xlApp == null) return;
Excel.Workbook xlBook;
Excel.Worksheet xlSheet;
try
{
this.Cursor = Cursors.WaitCursor;
int NbLignes = DGVMachines.RowCount;
int NbColonnes = DGVMachines.ColumnCount;
int x, y; // Compteurs
xlBook = xlApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
xlSheet = (Excel.Worksheet)xlBook.Worksheets[1];
for (x = 0; x < NbColonnes; x++)
{
//Ligne de titre
xlSheet.Cells[1, x + 1] = DGVMachines.Columns[x].HeaderText;
for (y = 0; y < NbLignes; y++)
{
xlSheet.Cells[y + 2, x + 1] = DGVMachines.Rows[y].Cells[x].Value.ToString();
}
}
xlBook.SaveCopyAs(@"d:\testexport.xls");
xlBook.Saved = true;
}
catch(Exception X)
{
MessageBox.Show(X.Message, "ERREUR.");
}
finally
{
xlApp.Quit();
this.Cursor = Cursors.Default;
}
} |
L'exportation fonctionne et le fichier est bien créé mais à la fin de la procédure il reste un processus EXCEL.EXE qui tourne et je suis obligé de le terminer via le gestionnaire des tâches. Pourquoi ? Où est le problème ?
Merci.