• EXCLE导入数据库


    #region 导入教师数据
            /// <summary>
            
    /// 导入教师数据
            
    /// </summary>
            
    /// <param name="context"></param>
            
    /// <returns></returns>
            private string ImportTeather(HttpContext context)
            {
                Hzjg.Common.Model.Mes oMes = new Hzjg.Common.Model.Mes();
                var entityModels = new List<EntityModel>();
                
                try
                {
                    if (context.Request.Files.Count > 0)
                    {
                        HttpPostedFile file = context.Request.Files["DataUpLoad"];
                        if (file != null && !string.IsNullOrEmpty(file.FileName))
                        {
                            string filePath = Hzjg.Common.Config.ConfigManage.fGetAppConfig("SaveFilePath") + "files\" +
                                   ConfigManage.fGetAppConfig("SysCode") + "\教师数据导入\" + Path.GetFileNameWithoutExtension(Path.GetFileName(file.FileName));
                            if (!Directory.Exists(filePath))
                                Directory.CreateDirectory(filePath);
                            file.SaveAs(filePath + "\" + Path.GetFileName(file.FileName));
                            //文件存在
                            if (File.Exists(filePath + "\" + Path.GetFileName(file.FileName)))
                            {
                                #region 读取EXCEL数据,存入DataTable
                                string dataSoure = filePath + "\" + Path.GetFileName(file.FileName);
                                //07以上版本
                                string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dataSoure + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";
                                var myConn = new OleDbConnection(strCon);
                                const string strCom = " SELECT * FROM [sheet1$]";
                                myConn.Open();
                                var myCommand = new OleDbDataAdapter(strCom, myConn);
                                var dt = new DataTable();
                                myCommand.Fill(dt);
                                myConn.Close();
                                #endregion

                                #region 保存到数据库
                                if (dt != null && dt.Rows.Count > 0)
                                {
                                    int j = 0;
                                    string mes = "";
                                    foreach (DataRow row in dt.Rows)
                                    {
                                        if (j > 0 && !string.IsNullOrEmpty(row[1].ToString()))
                                        {
                                            #region 导入数据
                                            string XM = row[0].ToString();
                                            string SJH = row[1].ToString();
                                            string GZSJDH = row[2].ToString();
                                            string BGDH = row[3].ToString();
                                            string NXDH = row[4].ToString();
                                            if (!string.IsNullOrEmpty(XM))
                                            {
                                                mJcjg0101 = cJcjg0101.GetList(" where t.XM = '" + XM.Replace(" """) + "'""").FirstOrDefault();
                                                if (mJcjg0101 != null)
                                                {
                                                    //更新
                                                    mJcjg0101.XM = XM.Replace(" """);
                                                    mJcjg0101.SJH = SJH;
                                                    mJcjg0101.GZSJDH = GZSJDH;
                                                    mJcjg0101.BGDH = BGDH;
                                                    mJcjg0101.NXDH = NXDH;
                                                    mJcjg0101.ISSWITCH = "0";
                                                    oMes = cJcjg0101.Update(mJcjg0101);
                                                }
                                                else
                                                {
                                                    oMes.State = MesState.Failure;
                                                    mes += "人员:" + XM.Replace(" """) + "不存在。" + " ";
                                                }
                                            }
                                            #endregion
                                        } j++;
                                    }
                                    oMes.Message = mes;
                                }
                                #endregion
                            }
                        }
                    }
                    else
                    {
                        oMes = new Mes() { Message = "未获取到要上传的教师数据,请检查!", State = MesState.Failure };
                        return Hzjg.GlobalExpand.ToJson(oMes);
                    }
                }
                catch (Exception ex)
                {
                    Hzjg.Common.Utility.Log.fWriterLog("教师导入操作异常", ex);
                }
                return Hzjg.GlobalExpand.ToJson(oMes);
            }
            #endregion
  • 相关阅读:
    scrapy-redis使用以及剖析
    完全理解 Python 迭代对象、迭代器、生成器
    Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy
    HTTP状态码
    Py西游攻关之RabbitMQ、Memcache、Redis
    Django contenttypes 应用
    cookie和session 以及Django中应用
    RESTful规范
    rest_framework框架的认识
    vue 总结
  • 原文地址:https://www.cnblogs.com/zecVip/p/4463744.html
Copyright © 2020-2023  润新知