tksy tksy - 1 year ago 107
C# Question

How to open an Excel file in C#?

I am trying to convert some VBA code to C#. I am new to C#. Currently I am trying to open an Excel file from a folder and if it does not exist then create it. I am trying something like the following. How can I make it work?

Excel.Application objexcel;
Excel.Workbook wbexcel;
bool wbexists;
Excel.Worksheet objsht;
Excel.Range objrange;

objexcel = new Excel.Application();
if (Directory("C:\\csharp\\error report1.xls") = "")

wbexcel.Open("C:\\csharp\\error report1.xls");
objsht = ("sheet1");

Answer Source

You need to have installed Microsoft Visual Studio Tools for Office.

After that create common .NET project and add the reference to COM object Microsoft.Office.Interop.Excel.dll via 'Add Reference..' dialog.

Application excel = new Application();
Workbook wb = excel.Workbooks.Open(path);

Missing.Value is a special reflection struct for unnecessary parameters replacement

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download