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);
??????????????? }
??????????? }
??????? }
轉載于:https://www.cnblogs.com/hanje/p/10779453.html
總結
以上是生活随笔為你收集整理的excel导入数据库的简单方法的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 缓存雪崩、缓存击穿
 - 下一篇: JVM学习04:类的文件结构