Avatar of trevor1940
trevor1940 asked on

C#: Reading Excel and resolving Dependency errors

Hi
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

Dependancy.JPG

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);

                }
                Console.ReadLine();
                //cleanup
                GC.Collect();
                GC.WaitForPendingFinalizers();

                //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
                Marshal.ReleaseComObject(xlRange);
                Marshal.ReleaseComObject(xlWorksheet);

                //close and release
                xlWorkbook.Close();
                Marshal.ReleaseComObject(xlWorkbook);

                //quit and release
                xlApp.Quit();
                Marshal.ReleaseComObject(xlApp);
            }

        }
    }
}

Open in new window

.NET ProgrammingC#

Avatar of undefined
Last Comment
trevor1940

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Miguel Oz

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
trevor1940

Thanx

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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy