I am accessing an excel file C# and reading data from it. Here is the code that have written so far.
string fileName = @"C:\repos\somepath\Workbook.xlsx";
string sheetName = "Sheet1";
FileStream stream = File.Open(fileName, FileMode.Open, FileAccess.Read);
IExcelDataReader excelReader =
ExcelReaderFactory.CreateOpenXmlReader(stream);
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();
DataTableCollection table = result.Tables;
DataTable resultTable = table[sheetName];
return resultTable;
My issue is as soon as I run my code, if the file is open, it throws an exception. After that I need to close the excel manually and then run again my code.
Is there a way which I can handle this and tell the code itself to save the changes and close it [may be the process] and then once the file is close proceed to the next step?
Any suggestions would be of great help.