excel导入数据库的简单方法

一个简单的方法将excel导入数据库

private void Plan_Import_Execl()
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = true;
            fileDialog.Title = "请选择文件";
            fileDialog.Filter = "xls files (*.xlsx)|*.xlsx";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                OperateExcel opExcel = new OperateExcel(fileDialog.FileName);
                DataTable dt = (DataTable)opExcel.ExcelToDatatalbe();
                for (int i = 1; i < dt.Rows.Count; i++)
                {
                    string sql = string.Format("select * from t_mes_hw_cust_code_info where cust_code='{0}'", dt.Rows[i][0].ToString().Trim());
                    DataTable dtt = DBManager.DBHelp.Instance().GetDataTable(sql);
                    if (dtt.Rows.Count > 0)
                    {
                        sql = string.Format("update  cust_code_info set COLOR_CODE='{2}',PROJECT_CODE='{1}',COLOR_NAME='{3}',EAN_UPC='{4}' where CUST_CODE='{0}' ",
                            dt.Rows[i][0].ToString().Trim(), dt.Rows[i][1].ToString().Trim(), dt.Rows[i][2].ToString().Trim(), dt.Rows[i][3].ToString().Trim(), dt.Rows[i][4].ToString().Trim());

                    }
                    else
                    {
                        sql = string.Format("insert into  cust_code_info (CUST_CODE,PROJECT_CODE,COLOR_CODE,COLOR_NAME,EAN_UPC) values ('{0}','{1}','{2}','{3}','{4}')",
                                                   dt.Rows[i][0].ToString().Trim(), dt.Rows[i][1].ToString().Trim(), dt.Rows[i][2].ToString().Trim(), dt.Rows[i][3].ToString().Trim(), dt.Rows[i][4].ToString().Trim());
                    }
                    int ii = DBManager.DBHelp.Instance().ExecuteSql(sql);

                }
            }
        }

posted @ 2019-04-27 17:29  hanje  阅读(379)  评论(0编辑  收藏  举报