2
class ExcelHandling
    {
        public static void NewExcelFile(){
            frmMain._frmMain.EXCEL_FILE = new Excel.Application();
            var excelApp = frmMain._frmMain.EXCEL_FILE as Excel.Application;
            excelApp.Workbooks.Add();
            excelApp.Visible = true;
        }

        public static void LoadExcelFile()
        {
            FileStream load = File.Open(@"F:\dsa.xlsx", FileMode.Open, FileAccess.Write);
            var excelApp = load as Excel.Application;
            frmMain._frmMain.EXCEL_FILE = excelApp;
            excelApp.Visible = true;
        }
    }

The above shown LoadExcelFile() method doesn't work.

What is the correct way of loading an existing excel file into a variable? I've tried a few things, but to no avail. The NewExcelFile() method works like a charm - it creates a new Excel file, stores it into the EXCEL_FILE global variable so I can manipulate it thereafter and shows it. I want the load function to do the same but with an existing excel file.

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
Schadenfreude
  • 1,522
  • 1
  • 20
  • 41
  • I would look into [Excel Data Reader](https://exceldatareader.codeplex.com/) - It's lightweight and very easy to use. Loads your data into a `DataSet`, then you can maniuplate it easily. – Evan L Feb 07 '14 at 16:58
  • Maybe check out this page: http://stackoverflow.com/questions/1118735/get-instance-of-excel-application-with-c-sharp-by-handle – Kapol Feb 07 '14 at 17:00
  • @Kapol I'm planning on making a load file dialog so getting an already running excel file won't do it for me. I just need to open the file, so It's visible to the user and load it into the variable so he can manipulate it through the windows form. – Schadenfreude Feb 07 '14 at 17:03

2 Answers2

1

Here is an example of opening an existing Excel spreadsheet using the Microsoft.Interop library:

var xlApp = new Microsoft.Office.Interop.Excel.Application();
var xlWorkBook = xlApp.Workbooks.Open("PathAndNameOfMyFile.xls", 0, true, 5, "", "", true,    Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

The overload parameters are fairly vague in documentation, but unfortunately required. Intellisense should give you some idea of the values and their meaning.

Your issue was that you were using a FileStream and then trying to let the library figure out what that stream contained. This will utilize the interop library to open the file and handle the data properly.

Evan L
  • 3,805
  • 1
  • 22
  • 31
  • Thanks, this is exactly what I was looking for, I guess I should check out the MS.Interop library a bit more thoroughly. – Schadenfreude Feb 08 '14 at 22:29
  • Ha no problem. Some of the stuff in there has really bad documentation so you have to look in intelli-sense half the time. – Evan L Feb 09 '14 at 17:00
0

I really like LinqToExcel. You can use NuGet Package Mgr in Visual Studio to add it https://www.nuget.org/packages/LinqToExcel . Documentation is @https://code.google.com/p/linqtoexcel/ (with a demo video)

justinmvieira
  • 572
  • 4
  • 13
  • I would prefer not to add an external library for just this one operation. If possible I'd like to convert the file stream into an excel application variable using the default c# libraries. – Schadenfreude Feb 07 '14 at 17:08
  • 1
    Oh. Have you tried excelApp.Workbooks.Open(@"F:\dsa.xlsx")? – justinmvieira Feb 07 '14 at 17:27
  • 1
    Here is the method documentation http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.workbooks.open.aspx – justinmvieira Feb 07 '14 at 17:28