简体   繁体   中英

C# .NET Epplus - finding excel sheets with a specific name, best way to handle no such sheet found

I have a method that looks for worksheets with a specific name, and returns this worksheet object (Epplus):

private static ExcelWorksheet findExcelSheet(ExcelPackage dpcExcel, string v)
        {
            foreach (var sheet in dpcExcel.Workbook.Worksheets)
            {
                // if we found a matching sheet
                if ((sheet.Name).CompareTo(v)==0)
                {
                    return sheet;
                }
            }

            return null;
        }

The above method is called in other methods, as part of an all-or-nothing process. The spreadsheets that are being processed are assumed to have a specific set of worksheets, and that a worksheet with a specific name will be present.

My question is: if I am to use the above method, should I be using try-catch blocks where the method is being called? Or, is there a better way to implement the above in this context?

There are several cases in this method where an object or property can return null. In that case it will throw a null exception. The caller of this method could have a try and catch block to catch specific exceptions (ie ArgumentNullException e) or generic exceptions (Exception e). You can define your own custom Exceptions and let the caller catch them and do different behaviours depending on the specific Exception. Or you could have a try and catch in this method and return null if there is an exception. Here are three different ways to do this.

public class Program
{
    public static void Main(string[] args)
    {
        ExcelPackage dpcExcel = new ExcelPackage();
        var searchWord = "myExcel";

        //Method 1
        //Will contain an ExcelWorkSheet or null
        var sheet = FindExcelSheetV1(dpcExcel, searchWord);

        //Method 2
        //Will contain an ExcelWorkSheet or we need to handle the exception
        var sheet2 = new ExcelWorksheet();
        try
        {
            sheet2 = FindExcelSheetV2(dpcExcel, searchWord);
        }
        catch (ArgumentNullException e)
        {
            //Do something specific
            Console.WriteLine(e.Message);
            throw;
        }
        catch (Exception e)
        {
            //Do something generic
            Console.WriteLine(e.Message);
            throw;
        }

        //Method 3
        //Will contain an ExcelWorkSheet or null
        var sheet3 = dpcExcel.FindExcelSheetV3(searchWord);
    }

    //Will return null if not found, or if any object or property is null
    private static ExcelWorksheet FindExcelSheetV1(ExcelPackage dpcExcel, string v)
    {
        try
        {
            var sheet = dpcExcel.Workbook.Worksheets.FirstOrDefault(w => w.Name.Equals(v));
            return sheet;
        }
        catch
        {
            return null;
        }
    }

    //This will throw an ArgumentNullException if any object or property is null
    private static ExcelWorksheet FindExcelSheetV2(ExcelPackage dpcExcel, string v)
    {
        var sheet = dpcExcel.Workbook.Worksheets.FirstOrDefault(w => w.Name.Equals(v));
        return sheet;

    }

}

public static class ExcelPackageExtensions
{
    // Like FindExcelSheetV1, but in an extension method. 
    public static ExcelWorksheet FindExcelSheetV3(this ExcelPackage dpcExcel, string v)
    {
        try
        {
            var sheet = dpcExcel.Workbook.Worksheets.FirstOrDefault(w => w.Name.Equals(v));
            return sheet;
        }
        catch
        {
            return null;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM