Hello everyone! I need to anticipate that I am very new to the Microsoft developing world, therefore I am not confident with Visual Studio.

I need to write an application for a windows server that works with excel. Looking on the web I found that the best method would be to use Microsoft.Office.Interop.Excel.
Therefore i put the using Microsoft.Office.Interop.Excel; line at the beginning of the code and, when comiling I told csc the reference using /r:Microsoft.Office.Interop.Excel.dll . The problem is that the compiler tells me that the metafile Microsoft.Office.Interop.Excel can't be found.

How can I proceed with this?
Thanks in advance!

Recommended Answers

All 5 Replies

Here's some code I wrote to get data from an excel file... To add the reference go to Project->Add Reference then find Microsoft.Office.Interop.Excel under the .Net tab. Note I think you need to have excel (or maybe just any office product) installed in order for this reference to be available.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;
using System.Data.OleDb;
using System.Threading;

namespace MobileExcelDB
{
    public class ExcelFile
    {
        public delegate void delProgUpdate(double dProgress);
        public delegate void delLoadComplete();

        public event delProgUpdate ProgressUpdate;
        public event delLoadComplete LoadComplete;

        string _Path;
        const int MAX_ROW = 100;
        const int MAX_COL = 100;
        public string[,] Data = new string[MAX_ROW,MAX_COL];
        public double Progress { get; private set; }

        public ExcelFile(string sPath)
        {
            _Path = sPath;
        }
        public void LoadFile()
        {
            Thread doExcelLoad = new Thread(new ParameterizedThreadStart(DoLoad));
            doExcelLoad.Start(_Path);
        }
        private void DoLoad(object sPath)
        {
            ApplicationClass excelApp = new ApplicationClass();
            excelApp.Visible = false;
            Workbook myWorkbook = excelApp.Workbooks.Open((string)sPath);
            Sheets sheets = myWorkbook.Worksheets;
            Worksheet myWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1);
            for (int i = 0; i < MAX_ROW ; i++)
            {
                for (int ii = 0; ii < MAX_COL; ii++)
                {
                    try
                    {
                        Data[i, ii] = ((Range)myWorksheet.Cells[i, ii]).get_Value(Type.Missing).ToString();
                    }
                    catch { Data[i, ii] = null; }
                }
                if (i % 10 == 0)
                {
                    UpdateProgress(i);
                    ProgressUpdate.Invoke(Progress);
                }
            }
            myWorkbook.Close();
            excelApp.Quit();
            LoadComplete.Invoke();
        }
        private void UpdateProgress(int iRowsLoaded)
        {
            Progress = ((double)iRowsLoaded / (double)MAX_ROW) * 100;
        }
    }

thank you very much for the swift reply.
the problem is that i'm not confident with visual studio, that's why i was adding the reference via command line as an argument to the compiler.
In visual studio 2008 i tried to create a new blank project and add a reference as suggested, but when i right click on the project name the "Add" submenu only includes "New project", "existing project", "new website", "existing website", "new item", "existing item", and new solution folder.
can i not bypass visual studio?

If you're using Visual Studio, I recommend you use its features rather than trying to make an end run around the system. This will help you avoid future headaches.

In VS2008, the "Add" submenu isn't where you add a reference. Right below it, you should see an "Add Reference..." menu option. You want that one. You can also right-click the "References" project node, and the "Add Reference..." menu option shows up there, too. And as skatamatic pointed out, the "Project->Add Reference" menu bar option also works. Any of these should allow you to add a reference to the Excel PIA.

If you're using Visual Studio, I recommend you use its features rather than trying to make an end run around the system. This will help you avoid future headaches.

In VS2008, the "Add" submenu isn't where you add a reference. Right below it, you should see an "Add Reference..." menu option.

thank you for the suggestion, but I do not find the menu option you are talking about. Also, being this just a very small and simple program, I thought that would be easy to develop it without using Visual Studio IDE: isn't this possible?

thank you for the suggestion, but I do not find the menu option you are talking about. Also, being this just a very small and simple program, I thought that would be easy to develop it without using Visual Studio IDE: isn't this possible?

There isn't really such thing as a small/simple program in c#. Just look at how much memory a "Hello World" app uses. This is because there's quite a bit of overhead with .Net apps, including loading the framework and its libraries, and adding external libraries (COM, COM+, etc...). The IDE helps clean the whole procedure up so you can focus on your program so it doesn't make much sense not to use some form of an IDE.

It doesn't make any sense why that menu would not be there; which visual studio version are you using? Perhaps something has become corrupted and you should reinstall it...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.