troubleshooting Question

C#: Reading Excel and resolving Dependency errors

Avatar of trevor1940
trevor1940 asked on
.NET ProgrammingC#
2 Comments1 Solution49 ViewsLast Modified:
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);
            }

        }
    }
}
ASKER CERTIFIED SOLUTION
Miguel Oz
Senior Software Engineer
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros