lim jia liang
lim jia liang

Reputation: 3

c# To verify excel file is damaged or not , if damaged then pop out a message box

I am a new to c# and i am doing a window form program that to check the excel file is exist or not. If exists then need to verify the excel file is damaged or not , but i have no idea how to write the code . the part of searching excel file i had done . #

        int point = 0;
            if (File.Exists(path1[0]))
            {
                MessageBox.Show("Sales " + Year + "-" + Month + "-" + Day + " Tranfser Error");
            point = 1;
            }
            else
            {
                for (int x = 1; x < path1.Length; x++)
                {
                    if (File.Exists(path1[x]))
                    {
                        MessageBox.Show("Sales " + Year + "-" + Month + "-" + Day + " Transfer Error");
                    point = 1;
                    }
                }

            }
            if (File.Exists(path2[0]))
            {
                MessageBox.Show("Sales " + Year + "-" + Month + "-" + Day + " Transfer Successful");
            point = 1;
            }
            else
            {
                for (int x = 1; x < path2.Length; x++)
                {
                    if (File.Exists(path2[x]))
                    {
                        MessageBox.Show("Sales " + Year + "-" + Month + "-" + Day + " Transfer Successful");
                    point = 1;
                    }
                }
            }

            if (File.Exists(path3))
            {
                MessageBox.Show("Sales " + Year + "-" + Month + "-" + Day + " Havent Transfer");
            point = 1;
            }
        if (point == 0)
        {
            MessageBox.Show("No File of the date "+ Year +"-"+ Month +"-"+ Day);
        }

Upvotes: 0

Views: 1697

Answers (1)

CreativeManix
CreativeManix

Reputation: 2170

Looks like you need to check multiple paths for existence and file format? if so please construct your code better way. However the below function shall give you expected result. This uses EPPlus library, install it through nuget.

enum ExcelFileTestResult
{
    FileNotFound,
    ValidFormat, //File found, valid excel
    InvalidFormat //File found but not valid excel
}

public static ExcelFileTestResult CheckExcelFile(string path)
{
    ExcelFileTestResult result = ExcelFileTestResult.FileNotFound;
    if (File.Exists(path))
    {
        FileInfo fi = new FileInfo(path);
        try
        {
            // Trying to read file using EPPlus
            // if the file is not valid format, it will throw error
            using (ExcelPackage p = new ExcelPackage(fi))
            {
                result = ExcelFileTestResult.ValidFormat;
            }
        }
        catch (InvalidDataException ex)
        {
            result = ExcelFileTestResult.InvalidFormat;
        }
    }
    return result;
}

Note: EPPlus works only for xlsx, not xls. https://github.com/JanKallman/EPPlus

Upvotes: 2

Related Questions