Link to home
Start Free TrialLog in
Avatar of Skale
Skale

asked on

Reading excel file even it's opened in ClosedXML C#

I've code like below and the problem is if the excel file is opened it gives an error and saying it's using with another process i don't know why it's happening.


        private static bool CheckTemplateFoundInDb(string dbPath, string substrName, string templateName)
        {
            System.IO.FileStream filestream = new System.IO.FileStream(dbPath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);

            var workbook = new ClosedXML.Excel.XLWorkbook(filestream);
            var worksheet = workbook.Worksheets.Worksheet(substrName);

            bool result = false;

            using (var excelWorkbook = new ClosedXML.Excel.XLWorkbook(dbPath))
            {

                var nonEmptyDataColumns = excelWorkbook.Worksheets.Worksheet(substrName).ColumnsUsed().Skip(5);

                foreach (var dataColumn in nonEmptyDataColumns)
                {

                    if (templateName == dataColumn.Cell(1).Value.ToString())
                    {
                        result = true;
                    }
                }
            }
            return result;
         }

Open in new window



Any help would be grateful.
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

I've code like below and the problem is if the excel file is opened it gives an error and saying it's using with another process i don't know why it's happening.
that's the common error when your source file is locked by a process

Reading excel file even it's opened in ClosedXML C#
try and see if you can replicate that file and let your process to read that replicated file? Of course, we are not sure your current process and doesn't know the reason why your source file is locked while you still trying to open it.
Usually, the office apps are opening the files for exclusive use. Why would you need to have the file open by another app?
IN your code you are opening the file twice (see lines 2 to 4 vs 10)
Pick just one way to open the excel file, for example:

private static bool CheckTemplateFoundInDb(string dbPath, string substrName, string templateName)
{
    using (var excelWorkbook = new ClosedXML.Excel.XLWorkbook(dbPath))
    {

        var nonEmptyDataColumns = excelWorkbook.Worksheets.Worksheet(substrName).ColumnsUsed().Skip(5);

        foreach (var dataColumn in nonEmptyDataColumns)
        {

            if (templateName == dataColumn.Cell(1).Value.ToString())
            {
                result = true;
            }
        }
    }
    return result;
}

Open in new window

Avatar of Skale
Skale

ASKER

@Miguel you're right! I changed it but result is same!

In fact sometimes forgot and files are opened so it causes error in fact i foudn solution on vb.net but it doesnt work now.

https://www.experts-exchange.com/questions/29145818/How-to-open-excel-files-in-read-only-with-ClosedXML-in-vb-net.html
ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial