'strange issue while reading excel sheet with epplus C#

My project has 2 console applications.

First application is writing data to the "first sheet" of an existing excel file (xlsx). The same excel file has a "second sheet" which is pretty huge and is using several formulas which are based on the data of the first sheet.

I am using the following code for writing the data to the first sheet:

public void WriteExcel(string InvoiceNo, string InvoiceDate, ...., int rowNumber)
    {
        using (ExcelPackage excelPackage = new ExcelPackage(this.excelFilePath))
        {
            ExcelWorksheet firstWorksheet = excelPackage.Workbook.Worksheets.First();

            firstWorksheet.Cells[rowNumber, 1].Value = (rowNumber - 2).ToString();
            firstWorksheet.Cells[rowNumber, 2].Value = InvoiceNo;
            firstWorksheet.Cells[rowNumber, 3].Value = InvoiceDate;
            .
            .
            excelPackage.Save();

        }
    }

The above code is working completely fine.

Second application is fetching data from the second sheet of this excel file and here I am facing an strange issue. The application, in the first place, is not picking up the data from the second sheet. But if I am opening the same excel file and closing it after saving it (by pressing Ctrl + S), then the application is fetching data properly.

I am using the following code to fetch the data:

string FilePath = @"file path";
foreach (string file in Directory.EnumerateFiles(FilePath, "*.xlsx"))
            {
                FileInfo fileInfo = new FileInfo(file);
                ExcelPackage package = new ExcelPackage(fileInfo);
                ExcelWorksheet excelSheet = package.Workbook.Worksheets[2];
                int colCount = excelSheet.Dimension.End.Column;
                int rowCount = excelSheet.Dimension.End.Row;
                

                for (int col = 7; col < colCount; col++)
                {
                    if (excelSheet.Cells[27, col].Value != "" && excelSheet.Cells[27, col].Value != null && excelSheet.Cells[27, col].Value.ToString() != "NAN")
                    {
                        Console.WriteLine("Data found!.. " + excelSheet.Cells[27, col].Value);
                        Console.ReadLine();
                        
                    }
                }
            }

Please help me on this.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source