Solved

C# Open Excel file

Posted on 2016-07-28
10
88 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
10 Comments
 
LVL 49

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 49

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
 
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 69

Expert Comment

by:Éric Moreau
ID: 41733277
which error?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 69

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# application error "Parameter Is Not Valid". 3 79
Server Error 11 47
Variable Event ? 3 23
.NET tools for adding thread safety to a web app? 3 14
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now