简体   繁体   English

从控制器中的excel导入数据时,如何检查excel文件中是否存在特定的列?

[英]While importing data from excel in the controller how to check if the specific column exists in the excel file or not?

I am adding data into my database table through a controller function that is adding the data successfully. 我正在通过成功添加数据的控制器功能将数据添加到数据库表中。 The problem is that i have to add data from two different excel files that contain same columns except one column so while adding data i want to put some check in the controller while reading from excel file that whether a specific column exists in excel file or not? 问题是我必须从两个不同的excel文件中添加数据,这些文件包含除一列之外的相同列,因此在添加数据时我想在控制器中进行一些检查,同时从excel文件中读取excel文件中是否存在特定的列? here is my controller method which is working fine for the rest of the columns: 这是我的控制器方法,适用于其余各列:

using (ApplicationDbContext db = new ApplicationDbContext())
{
    db.Database.ExecuteSqlCommand("TRUNCATE TABLE COCoreMembers");
    DataAccess.DataTable abc = DataAccess.DataTable.New.ReadCsv(Server.MapPath("~/App_Data/TempBrdcepPSC/PscData/co_members.csv"));
    COCoreMember xyz = new COCoreMember();

    foreach (Row Row in abc.Rows)
    {
        xyz._URI = Row["_URI"];
        xyz._PARENT_AURI = Row["_PARENT_AURI"];
        xyz.HHID = Row["HHID"];
        xyz.HHNAME = Row["HHNAME"];
        xyz.HH_SET_ID = Row["HH_SET_ID"];
        xyz.HH_SET_NAME = Row["HH_SET_NAME"];
        xyz.MEM_ID = Row["MEM_ID"];
        xyz.MEM_NAME = Row["MEM_NAME"];
        xyz.MEMBER_AGE = Row["MEMBER_AGE"];
        xyz.POSITION = Row["POSITION"];
        xyz.CELL = Row["CELL"];
        if (Row["CNIC"].ToString() == xyz.CNIC)
        {
            xyz.CNIC = Row["CNIC"];
        }
        else
        {
            xyz.CNIC = "no record";
        }

        //xyz.CNIC = "CNIC";

        db.COCoreMembers.Add(xyz);
        db.SaveChanges();
    }
}

I just want to check that if column "CNIC" exists in the excel file then input that into table other wise input "No Record" into its place in the table. 我只想检查excel文件中是否存在“ CNIC”列,然后将其输入到表中,否则将其输入到表中。

Try like this- 尝试这样-

using (ApplicationDbContext db = new ApplicationDbContext())
{
    db.Database.ExecuteSqlCommand("TRUNCATE TABLE COCoreMembers");
    DataAccess.DataTable abc = DataAccess.DataTable.New.ReadCsv(Server.MapPath("~/App_Data/TempBrdcepPSC/PscData/co_members.csv"));
    bool checkColumn = false;
    if(abc.Columns.Contains("CNIC"))
    {
        checkColumn = true;
    }
    else
    {
        abc.Columns.Add("CNIC");
    }

    COCoreMember xyz = new COCoreMember();

    foreach (Row Row in abc.Rows)
    {
        xyz._URI = Row["_URI"];
        xyz._PARENT_AURI = Row["_PARENT_AURI"];
        xyz.HHID = Row["HHID"];
        xyz.HHNAME = Row["HHNAME"];
        xyz.HH_SET_ID = Row["HH_SET_ID"];
        xyz.HH_SET_NAME = Row["HH_SET_NAME"];
        xyz.MEM_ID = Row["MEM_ID"];
        xyz.MEM_NAME = Row["MEM_NAME"];
        xyz.MEMBER_AGE = Row["MEMBER_AGE"];
        xyz.POSITION = Row["POSITION"];
        xyz.CELL = Row["CELL"];

        if(checkColumn)
        {
            xyz.CNIC = Row["CNIC"];
        }
        else
        {
            xyz.CNIC = "no record";
        }

        db.COCoreMembers.Add(xyz);
        db.SaveChanges();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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