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
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);
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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