public void h_importar_usuario_coorporativo_do_excel() { string _conectionstring; _conectionstring = @"Provider=Microsoft.Jet.OLEDB.4.0;"; _conectionstring += String.Format("Data Source={0};", "D:\\Coorporativo.xls"); _conectionstring += "Extended Properties='Excel 8.0;HDR=NO;'"; OleDbConnection cn = new OleDbConnection(_conectionstring); OleDbCommand cmd = new OleDbCommand("Select * from [Usuarios$]", cn); cn.Open(); OleDbDataReader reader = cmd.ExecuteReader(); List<UsuarioCoorporativoExcel> usuariosExcel = new List<UsuarioCoorporativoExcel>(); int i = 0; while (reader.Read()) { if (i == 2) { if (reader[0] == DBNull.Value) break; UsuarioCoorporativoExcel usuarioExcel = new UsuarioCoorporativoExcel(); usuarioExcel.Usuario = (string)reader[0]; usuarioExcel.Login = (string)reader[1]; usuarioExcel.NomeSetor = (string)reader[3]; usuarioExcel.CodCentro = (string)reader[5]; usuarioExcel.Descricao = (string)reader[6]; usuariosExcel.Add(usuarioExcel); } i++; } var novo = usuariosExcel; Setores setores = new Setores(); CentrosDeCusto centrosDeCusto = new CentrosDeCusto(); TipoUsuarios tipoUsuarios = new TipoUsuarios(); var tipoUsuario = tipoUsuarios.Obter<TipoUsuario>(3); var repositorioUsuarios = new Usuarios(); foreach (var usuarioExcel in usuariosExcel) { Usuario usuario = repositorioUsuarios.ObterAcesso(usuarioExcel.Login, 123456.ToString()); if (usuario == null) { usuario = new Usuario(); usuario.TipoUsuario = tipoUsuario; usuario.Nome = usuarioExcel.Usuario; usuario.Login = usuarioExcel.Login; usuario.Senha = "123456"; } Setor setor = setores.ObterPor(usuarioExcel.NomeSetor); if (setor == null) { setor = new Setor(usuarioExcel.NomeSetor); } if (usuario.Departamentos == null || !usuario.Departamentos.Any(d => d.Nome == setor.Nome)) usuario.ParticiparDe(setor); var centroDecusto = centrosDeCusto.ObterPor(usuarioExcel.CodCentro); if (centroDecusto == null) { setores.Salvar(setor); centroDecusto = new CentroDeCusto(usuarioExcel.Descricao) { CodigoDoCentroDeCusto = usuarioExcel.CodCentro }; centrosDeCusto.Salvar(centroDecusto); } if (setor.CentrosDeCusto == null || setor.CentrosDeCusto.Count == 0 || setor.CentrosDeCusto != null && setor.CentrosDeCusto.Count > 0 && !setor.CentrosDeCusto.Any(c => c.CodigoDoCentroDeCusto == centroDecusto.CodigoDoCentroDeCusto)) setor.AdicionarCentroDeCusto(centroDecusto); //ServicoSalvarDepartamento servico = new ServicoSalvarDepartamento(); //servico.Salvar(setor); setores.Salvar(setor); repositorioUsuarios.Salvar(usuario); } }