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 button1_Click(object sender, EventArgs e)
{
try
{
SaveFileDialog frm = new SaveFileDialog();
if (frm.ShowDialog() == DialogResult.OK)
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
Object missing = Missing.Value;
// il faut que le fichier excel existe sinon ça plante
// J'ouvre donc par défaut un fichier vide, créé dans le répertoire de l'appli.
Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Open("c:\\Test\\DoNotDelete.xls", missing, false,
missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing,
missing);
Microsoft.Office.Interop.Excel.Worksheet xlSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlBook.ActiveSheet;
if (xlApp != null)
{
xlSheet.Cells[1, 1] = "line 1 col 1";
xlSheet.Cells[1, 2] = "line 1 col 2";
// et on enregistre le fichier sous le nom saisi par l'utilisateur.
xlBook.SaveAs(frm.FileName, missing, missing, missing, missing, missing, XlSaveAsAccessMode.xlShared, missing, missing,
missing, missing, missing);
xlBook.Close(false, missing, missing);
xlApp.Quit();
}
}
}
catch (Exception e1)
{
MessageBox.Show(e1.Message);
}
} |
Partager