简体   繁体   English

如何计算excel列名?

[英]How Can I count excel column name?

I Want to import data from excel to sql,in this operation if a user can add one column extra,how can I check which column is newly added and at the same time in sql table also added the new column with the same column name like excel sheet... 我想将数据从excel导入到sql中,如果用户可以额外添加一列,如何检查新添加的列,同时在sql表中还添加了具有相同列名的新列excel表...

I can Done read the excel column count-like below my coding 我可以像下面的代码一样阅读Excel列的计数

 protected void btn_upload_Click(object sender, EventArgs e)
    {
        //Add Path to Local system dynamically
        string path = string.Empty;

        path = Server.MapPath("~//files//") + Fup_Excel.PostedFile.FileName;

        Fup_Excel.SaveAs(path);
        //-----------------Execl Connection & Count Part-------------------------------------
        //create Oledb connection for Excel Fetch
        OleDbConnection oconn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 8.0");


        OleDbCommand olcmd = new OleDbCommand("select * from [sheet1$]", oconn);//create command for select excel file

        OleDbDataAdapter adapter = new OleDbDataAdapter();

        adapter.SelectCommand = olcmd;//adap the command

        DataSet ds = new DataSet();//collection of data

        adapter.Fill(ds);//data's are filled to dataset

        string count = ds.Tables[0].Columns.Count.ToString();//taking the Excel sheet column count



    }

Here after I done take count from sql table also that code's are 在我完成从sql表中计数后的代码

 SqlConnection sconn = new SqlConnection(@"Data Source=C07-PC\SQLEXPRESS;Initial Catalog=excel;User ID=sa;Password=**********");
        string asdf = null;
        SqlCommand scmd = new SqlCommand();
        scmd.CommandText = "select count(*) from information_schema.columns where table_name='table_1'";
        scmd.Connection = sconn;
        sconn.Open();
        SqlDataReader sr = null;
        sr = scmd.ExecuteReader();
        while (sr.Read())
        {

              asdf =sr[0].ToString();

        }

and from here onwords i dont know how to check which column is newly added in excel?then that ssame name i want to insert column in sql table also.. 并且从这里onwords我不知道如何检查excel中新添加了哪一列?然后那个同样的名字我也想在sql表中插入列..

After retrieving all the columns from excel you need to check each columns in SQL Database whether that column is exists or not. 从excel检索所有列后,您需要检查SQL数据库中的每个列是否存在该列。 If not exists then add to sql database. 如果不存在,则添加到sql数据库。

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

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