Solved

C# Open Excel file

Posted on 2016-07-28
10
173 Views
Last Modified: 2016-08-01
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
Comment
Question by:CipherIS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 52

Assisted Solution

by:Ryan Chong
Ryan Chong earned 250 total points
ID: 41733194
tried use :
System.Diagnostics.Process.Start(path);

Open in new window

?
1
 
LVL 1

Author Comment

by:CipherIS
ID: 41733210
That works.  Just wondering why I can't use the code that I find in my searches.
0
 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 41733221
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:CipherIS
ID: 41733242
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
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41733277
which error?
0
 
LVL 1

Author Comment

by:CipherIS
ID: 41733516
Well, I can't event type "Excel" alone.  When I attempt to intelisense gives me "ExcelDataOnlyFormatOptions".
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 250 total points
ID: 41733598
you are surely missing the using clauses:
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;

Open in new window

0
 
LVL 1

Author Comment

by:CipherIS
ID: 41737776
I'll look at that.
0
 
LVL 1

Author Comment

by:CipherIS
ID: 41737782
Found the issue.  Another namespace was inadvertently added which negated the Interop.excel namespace.  Thanks Eric.
0
 
LVL 1

Author Comment

by:CipherIS
ID: 41737786
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question