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(" """) + "不存在。" + "\n";
                                            }
                                        }
                                        #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
posted @ 2015-04-28 18:19  Tz__C  阅读(305)  评论(0编辑  收藏  举报