C# program that interfaces with MS Excel: Open an existing Excel file

1. Open a new Visual C# .NET windows application. Name the project OpenExcel.
2. In Solution Explorer, right-click the References node and select Add Reference. On the COM tab, select “Microsoft Excel 10.0 Object Library”, and then click OK.
3. Design a form. Add a Button.
4. Set Name property of Button to “btnOpenExcel” and text property to “Open Excel File”.
5. From Toolbox expand Dialogs group double click on the “OpenFileDialog”.
6. In the code behind file import the library Microsoft Excel 10.0 Object Library using: using Excel;
7. Double click on the Button and pest the following code into the “btnOpenExcel_Click” event.

//Add a reference to excel.application

Excel.Application exc;



//Creating new excel.application

exc = new Excel.Application();

//Prepare open file dialog to only search for excel files

this.openFileDialog1.FileName = "*.xls";

if (this.openFileDialog1.ShowDialog() == DialogResult.OK)


//To get the workbooks collection

Workbooks workbooks = exc.Workbooks;

// Here is the call to Open a Workbook in Excel

// It uses most of the default values

workbooks.Open(openFileDialog1.FileName, 0, true, 5,

"", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0,

true, 0, 0);

// Get opened excel file name

label2.Text = openFileDialog1.FileName;

//To make application visible

exc.Visible = true;



catch (Exception ex)






exc = null;


Press F5 to build and run the project.

8. Click on the Button “Open Excel File” and select an excel file using “OpenFileDialog” window and Click on the Button “Open”. Your selected Excel file will be open.

1 comment

gopal singh 2 years ago

thnk you...

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.

    Click to Rate This Article