We help IT Professionals succeed at work.

C#: Reading Excel and resolving Dependency errors

trevor1940 asked
Last Modified: 2020-04-08
I've HAD to move some C# programs to a different system, to continue to work on, which clearly has different versions of VS & .net I knew  it had different versions of MS Office

So my question is twofold

First what is the best way to read an excel spreadsheet in C# that isn't dependent on the which version of office is installed?
The Code bellow is my Test script that just reads a excel file or did on the old system

Second how do I resolve the Dependency errors as seen in the pic this is A different script that dose something with the cell values


using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

using Excel = Microsoft.Office.Interop.Excel;

namespace CrapNetCore
    class Program
        static void Main(string[] args)
            Console.WriteLine("Hello World!");
            string WorkBook = @"C:\Temp\ExcaelFile.xls";

            if (File.Exists(WorkBook))
                Excel.Application xlApp = new Excel.Application();

                Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(WorkBook);
                Excel._Worksheet xlWorksheet = (Excel.Worksheet)xlWorkbook.Sheets[3];
                Excel.Range xlRange = xlWorksheet.UsedRange;

                int rowCount = 8;// xlRange.Rows.Count;
                int colCount = xlRange.Columns.Count;
                int PrefCount = 1;
                //iterate over the rows and columns and print to the console as it appears in the file
                //excel is not zero based!!
                for (int i = 3; i <= rowCount; i++)
                    string ColA = xlRange.Cells[i, 1].ToString();
                    Console.WriteLine("Value of CollA {0}", ColA);


                //rule of thumb for releasing com objects:
                //  never use two dots, all COM objects must be referenced and released individually
                //  ex: [somthing].[something].[something] is bad

                //release com objects to fully kill excel process from running in the background

                //close and release

                //quit and release


Open in new window

Watch Question

Senior Software Engineer
Top Expert 2009
This one is on us!
(Get your first solution completely free - no credit card required)



Turns out I had to update VS and reinstall nugate packages

Just worked out how to read the excel file  using the GemBox.Spreadsheet you suggested & hit row limit for  free version

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.