visual studio – C# xlsx exception in reading file

0
25

Im newbe in c# , I try to read xlsx file and get an error
Code:

Excel.Application xlApp ;
Excel.Workbook xlWorkBook ;
Excel.Worksheet xlWorkSheet ;
Excel.Range range ;

int rCnt ;
int rw = 0;
int cl = 0;

String excelPath = "";
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(@"C:dirmyxexel.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

range = xlWorkSheet.UsedRange;
rw = range.Rows.Count;
cl = range.Columns.Count;

// this list will hold all data for all users
// all data manipulations will be done on this list

Persons personslist = new Persons();// create an empty list
for (rCnt = 2; rCnt  <= rw; rCnt++)
{
    // each row represents a new Person
    Person p = new Person();
    p.firstname = (string)(range.Cells[rCnt, 1] as Excel.Range).Value2;
    p.lastname = (string)(range.Cells[rCnt, 2] as Excel.Range).Value2;
    p.job = (string)(range.Cells[rCnt, 3] as Excel.Range).Value2;
    p.destination = (string)(range.Cells[rCnt, 4] as Excel.Range).Value2;
    p.squad = (string)(range.Cells[rCnt, 5] as Excel.Range).Value2;
    personslist.Add(p);  // add this person to the list
}

xlWorkBook.Close(true, null, null);
xlApp.Quit();

Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(xlApp);
Console.WriteLine(" ");

this is the exception:

System.Runtime.InteropServices.COMException was unhandled
  HelpLink=xlmain11.chm
  HResult=-2146827284
  Message=Sorry, we couldn't find ‪‪‪C:dirmyxexel.xlsx. Is it possible it was moved, renamed or deleted?
  Source=Microsoft Excel
  ErrorCode=-2146827284
  StackTrace:
       ב-  Microsoft.Office.Interop.Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin, Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad)
       ב-  ConsoleApplication1.Program.Main(String[] args) ב- c:UsersuserDesktopהתקייה של דולבטכםexcel to listConsoleApplication1Program.cs:שורה 27
       ב-  System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       ב-  System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       ב-  Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       ב-  System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       ב-  System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       ב-  System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       ב-  System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       ב-  System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

how can I solve this?

LEAVE A REPLY

Please enter your comment!
Please enter your name here