Monday, August 11, 2014

Open Excel Document

After I explained in the previous post basics about the integration of Excel in C# and the creation and saving of a new document, I want to show in this post how to open an Excel document with C#.
For this we use the function Microsoft.Office.Interop.Excel.Application.Workbooks.Open().
For it many overloadings exist, which are described here. Here we pass just the filename, the corresponding file is then opened via the Excel control and can be used. The complete code is:

            Microsoft.Office.Interop.Excel.Application ExcelApp;
            Workbook ExcelWorkbook;

            ExcelApp = new Microsoft.Office.Interop.Excel.Application();
            ExcelWorkbook = ExcelApp.Workbooks.Open("MyFirstExcelSheet.xls");
            ExcelWorkbook.Close();
            ExcelApp.Quit();

No comments:

Post a Comment