Share via


Open an Excel Workbook in C# console application

Question

Wednesday, February 27, 2013 7:30 PM

I have been searching, googling, and so on for quite some time on how to do a simple task of opening a Excel Workbook so I can read from it in a C# console application.  I have seen lots of samples, but they are not working for me at all.  

This is my current code:

Excel.Application exApp = new Excel.Application();
Excel.Workbook exWbk = exApp.Workbooks.Open("Sample Data", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Excel.Worksheet exWks = (Excel.Worksheet)exWbk.Sheets["Sheet1"];

The Sample Data file is in the main project directory, and as part of my build, it gets copied to the output directory as well.  The error I get is that it can find the file.  

All the samples I have seen to date specify a full path to the file.  I dont want that.  I want it to read from the current directory to look for the file.  When I do this with tab delimited data, and the File.ReadAll type operation, this works fine.  

I have also seen samples where they use the @ before the file path.  I am not sure what this does, or if I need it.   I know this is a simple silly query, but I can not seem to get the it working.

TIA.

All replies (5)

Thursday, February 28, 2013 1:33 AM âś…Answered | 4 votes

While not your primary question, the @ is a quoted string literal.  The difference is that escape characters are not processed, which makes paths for instance much easier on the eyes.

var regularPath = "C:\mydirectory\myfile.txt"
var quotedPath = @"C:\mydirectory\myfile.txt"

http://msdn.microsoft.com/en-us/library/362314fe(v=VS.71).aspx

As for the excel question, from a quick look at a debug of this, it seems Office.Interop.Excel isn't seeing the same CurrentDirectory I am with Directory.CurrentDirectory.  no worries.  I placed the xlsx file in the bin\Debug directory and found the absolute path:

// Get fully qualified path for xlsx file
var spreadsheetLocation = Path.Combine(Directory.GetCurrentDirectory(), "Sample Data.xlsx");

var exApp = new Microsoft.Office.Interop.Excel.Application();
var exWbk = exApp.Workbooks.Open(spreadsheetLocation);
var exWks = (Microsoft.Office.Interop.Excel.Worksheet)exWbk.Sheets["Sheet1"];

Also, I noticed you added a bunch of Type.Missing arguments to Workbooks.Open. This is unnecessary, as they are optional parameters. You can use the method as I did above. The other parameters have default values and do not need to be specified.

 

Christopher Wilcox
If I have answered your question, Please click "Mark As Answer"


Thursday, February 28, 2013 6:46 AM

At least as far as C# is concerned, default parameters (and using those of existing things like office interop) is a rather new feature only implemented in C# 4.0

http://blogs.msdn.com/b/csharpfaq/archive/2004/03/07/why-doesn-t-c-support-default-parameters.aspx

Yes prior to this you had to define something for all default parameters, wich made using Office Interop a real pain in C#.

Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2


Thursday, February 28, 2013 2:20 PM

This video might help - it shows how to create simple Excel file programmatically


Thursday, February 28, 2013 2:20 PM

http://www.youtube.com/watch?v=HkCOCR2BOxQ


Thursday, February 28, 2013 3:53 PM

YES! That did it for me!  Thanks so much!