Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello All,
i want to validate excel column heading while uploading excel.if someone change column heading getting error me.thats why i want to validate heading while uploading excel.
if column heading not match then getting error msg me

plz share with me.

What I have tried:

C#
if (Path.GetExtension(FileUpload.FileName).ToLower() == ".xlsx")
                {
                    if (Path.GetExtension(FileUpload.FileName).ToLower() == ".xls")
                    {
                        ExcelPackage package = new ExcelPackage(FileUpload.FileContent);
                        dt = package.ToDataTable();
                        //dt = ExcelToDataTable(FilePath);

                    }
                    else
                    {
                        ExcelPackage package = new ExcelPackage(FileUpload.FileContent);
                        dt = package.ToDataTable();

                        //foreach (DataRow row in dt.Rows.Count)
                        //{
                            for(int i=0;i<=dt.Rows.Count-1;i++)
                        { 
                            DateTime a = Convert.ToDateTime(dt.Rows[i]["Start Date"]);
                            DateTime b = Convert.ToDateTime(dt.Rows[i]["End Date"]);
                            if (a >= b)
                            {
                                //c++;
                                lblMsg.ForeColor = System.Drawing.Color.Red;
                                lblMsg.Text = "Please Enter Start date is Less than End Date";
                            }

                            else
                            {
                                 c++;

                            }
                        }

                    }
                    if (c==dt.Rows.Count)
                        {
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                        Uploaddata(ExcelPackageExtensions.ValidateUpFromDataTable1(dt), FileName);
Posted
Updated 13-May-16 1:37am
v2

1 solution

Your code will never run because of these two lines
C#
if (Path.GetExtension(FileUpload.FileName).ToLower() == ".xlsx")
{
        if (Path.GetExtension(FileUpload.FileName).ToLower() == ".xls")

The file extension cannot be both ".xlsx" AND ".xls" it must be one or the other.
I suspect that you wanted to use
C#
if (Path.GetExtension(FileUpload.FileName).ToLower() == ".xlsx" ||
    Path.GetExtension(FileUpload.FileName).ToLower() == ".xls")
{
    ExcelPackage package = new ExcelPackage(FileUpload.FileContent);
    dt = package.ToDataTable();

    for(int i=0;i<=dt.Rows.Count-1;i++)


I haven't checked the rest of it, but you need to use the debugger to find out what is going on if it still doesn't work. This article will help you Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900