C#将execl导入到sql server 数据库中

private void dao_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = @"execl文件|*.xls;*.xlsx";
            string name;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                name = ofd.FileName;
                input(name);

            }
        }
        private void input(string fileName)
        {
            List list = new List();
            string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" +
                 "Data Source=" + fileName + ";" +
                 "Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'";
    
            OleDbDataAdapter da = new OleDbDataAdapter("SELECT *  FROM [sheet1$]", strConn);/*execl中sheet1页中数据*/
            DataTable dt = new DataTable();
            DataSet ds = new DataSet();
            try
            {
                da.Fill(ds);
                dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (this.dClass.GetDataTable(string.Concat(new string[]{"select * from Luku where cPanhao =  '",
                        dt.Rows[i][0].ToString(),
                        "'"})).Rows.Count > 0)
                    {
                        //MessageBox.Show("入库已存在托盘号:"+dt.Rows[i][0].ToString());
                    }
                    else
                    {
                        list.Add(string.Concat(new object[]
						{
							"insert into Luku(cPanhao, cName, date, cGongl, iBiao)values('",
							dt.Rows[i][0].ToString(),
							"','",
							dt.Rows[i][1].ToString(),
							"','",
							dt.Rows[i][2].ToString(),
							"','",
							dt.Rows[i][3].ToString(),					
							"','",
							0,
							"')"
						}));
                    }
                }                                         
                if (this.dClass.ExecuteNonQueryByTransaction(list) && list.Count > 0)
                {

                }
            }

            catch (Exception err)
            {
                MessageBox.Show("操作失败!" + err.ToString());
            }
        }

淘宝小店:给个关注

你可能感兴趣的:(C#,sql,server,execl)