• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 904
  • Last Modified:

C# Open Excel file

I am trying to open an excel file in .NET.  I found the below code and I'm trying to use it but I'm receiving errors.  

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

Open in new window


I'm using VS 2015.

Errors I'm receiving are:

1.  Applicaion does not contain a constructor that takes 0 arguments.
2.  Application' does not contain a definition for 'Workbooks' and no extension method 'Workbooks' accepting a first argument of type 'Application' could be found (are you missing a using directive or an assembly reference?)      

I added Interop excel as a reference. v 15.
0
CipherIS
Asked:
CipherIS
2 Solutions
 
Ryan ChongCommented:
tried use :
System.Diagnostics.Process.Start(path);

Open in new window

?
1
 
CipherISAuthor Commented:
That works.  Just wondering why I can't use the code that I find in my searches.
0
 
Rgonzo1971Commented:
Hi,

Another way to open
        Excel.Application xlApp;
        Excel.Workbook xlWorkBook1;

        xlApp = new Excel.Application();
        xlWorkBook1 = xlApp.Workbooks.Open("c:\\YourFile.xlsx", Type.Missing, false, Type.Missing,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

Open in new window

REgards
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
CipherISAuthor Commented:
The problem is that even when I try
Excel.Application

Open in new window

I still receive an error.  I know I've done this previously in other apps that I've developed.
0
 
Éric MoreauSenior .Net ConsultantCommented:
which error?
0
 
CipherISAuthor Commented:
Well, I can't event type "Excel" alone.  When I attempt to intelisense gives me "ExcelDataOnlyFormatOptions".
0
 
Éric MoreauSenior .Net ConsultantCommented:
you are surely missing the using clauses:
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;

Open in new window

0
 
CipherISAuthor Commented:
I'll look at that.
0
 
CipherISAuthor Commented:
Found the issue.  Another namespace was inadvertently added which negated the Interop.excel namespace.  Thanks Eric.
0
 
CipherISAuthor Commented:
There was code in the namespace similar to

Application = .......;

which was not pointing to excel.  This was causing the problem.

I deleted it.  I should have copied and pasted it here to have as a reference.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now