private bool InserirPF(ExcelPFModel row) { SqlParameter[] parms = new SqlParameter[] { new SqlParameter("@NOME", row.NOME), new SqlParameter("@PROFISSAO", row.PROFISSAO), new SqlParameter("@ORIGEM", row.ORIGEM), new SqlParameter("@INAUGURACAO", row.INAUGURACAO), new SqlParameter("@ESPECIAL", row.ESPECIAL), new SqlParameter("@DDD1", row.DDD1), new SqlParameter("@NUMTEL1", row.NUMTEL1), new SqlParameter("@TIPOTEL1", row.TIPOTEL1), new SqlParameter("@EMAIL1", row.EMAIL1), new SqlParameter("@LOGRADOURO1", row.LOGRADOURO1), new SqlParameter("@NUMEND1", row.NUMEND1), new SqlParameter("@BAIRRO1", row.BAIRRO1), new SqlParameter("@COMP1", row.COMP1), new SqlParameter("@CEP1", row.CEP1), new SqlParameter("@CIDADE1", row.CIDADE1), new SqlParameter("@SIGLAESTAD1", row.SIGLAESTAD1), new SqlParameter("@EMPRESA1", row.EMPRESA1), new SqlParameter("@CARGO1", row.CARGO1), new SqlParameter("@DPTO1", row.DPTO1), new SqlParameter("@DDD2", row.DDD2), new SqlParameter("@NUMTEL2", row.NUMTEL2), new SqlParameter("@TIPOTEL2", row.TIPOTEL2), new SqlParameter("@EMAIL2", row.EMAIL2), new SqlParameter("@LOGRADOURO2", row.LOGRADOURO2), new SqlParameter("@NUMEND2", row.NUMEND2), new SqlParameter("@BAIRRO2", row.BAIRRO2), new SqlParameter("@COMP2", row.COMP2), new SqlParameter("@CEP2", row.CEP2), new SqlParameter("@CIDADE2", row.CIDADE2), new SqlParameter("@SIGLAESTAD2", row.SIGLAESTAD2), new SqlParameter("@EMPRESA2", row.EMPRESA2), new SqlParameter("@CARGO2", row.CARGO2), new SqlParameter("@DPTO2", row.DPTO2), new SqlParameter("@DDD3", row.DDD3), new SqlParameter("@NUMTEL3", row.NUMTEL3), new SqlParameter("@TIPOTEL3", row.TIPOTEL3), new SqlParameter("@EMAIL3", row.EMAIL3), new SqlParameter("@LOGRADOURO3", row.LOGRADOURO3), new SqlParameter("@NUMEND3", row.NUMEND3), new SqlParameter("@BAIRRO3", row.BAIRRO3), new SqlParameter("@COMP3", row.COMP3), new SqlParameter("@CEP3", row.CEP3), new SqlParameter("@CIDADE3", row.CIDADE3), new SqlParameter("@SIGLAESTAD3", row.SIGLAESTAD3), new SqlParameter("@EMPRESA3", row.EMPRESA3), new SqlParameter("@CARGO3", row.CARGO3), new SqlParameter("@DPTO3", row.DPTO3), new SqlParameter("@OBSERVACAO", row.OBSERVACAO) }; DataTable dt = db.ExecuteReader<SqlDataReader>("proc_importaPF", parms); string erro = dt.Rows[0]["ERRO"].ToString(); if (erro != "") throw new Exception(erro); return true; }
private List<ExcelPFModel> CriaListaPFExcel(string _nome, bool? _inauguracao, bool? _especial, int? _idOrigem, int? _idProfissao) { List<PFModel> listaPF = new PF().Filtro(_nome, _inauguracao, _especial, _idOrigem, _idProfissao); List<ExcelPFModel> listaExcelPF = new List<ExcelPFModel>(); ExcelPFModel excelPF; foreach (var pf in listaPF) { excelPF = new ExcelPFModel(); excelPF.NOME = pf.Nome; excelPF.OBSERVACAO = pf.Observacao; excelPF.PROFISSAO = pf.Profissao.Nome; excelPF.ORIGEM = pf.Origem.Nome; excelPF.INAUGURACAO = pf.IsInauguracao == true ? "SIM" : "NÃO"; excelPF.ESPECIAL = pf.IsEspecial == true ? "SIM" : "NÃO"; if (pf.Telefones.Count > 0) { excelPF.DDD1 = pf.Telefones[0].Ddd; excelPF.NUMTEL1 = pf.Telefones[0].Numero; excelPF.TIPOTEL1 = pf.Telefones[0].TipoTelefone.Nome; if (pf.Telefones.Count > 1) { excelPF.DDD2 = pf.Telefones[1].Ddd; excelPF.NUMTEL2 = pf.Telefones[1].Numero; excelPF.TIPOTEL2 = pf.Telefones[1].TipoTelefone.Nome; if (pf.Telefones.Count > 2) { excelPF.DDD3 = pf.Telefones[2].Ddd; excelPF.NUMTEL3 = pf.Telefones[2].Numero; excelPF.TIPOTEL3 = pf.Telefones[2].TipoTelefone.Nome; } } } if (pf.Emails.Count > 0) { excelPF.EMAIL1 = pf.Emails[0].Nome; if (pf.Emails.Count > 1) { excelPF.EMAIL2 = pf.Emails[1].Nome; if (pf.Emails.Count > 2) { excelPF.EMAIL3 = pf.Emails[2].Nome; } } } if (pf.Enderecos.Count > 0) { excelPF.LOGRADOURO1 = pf.Enderecos[0].Logradouro; excelPF.NUMEND1 = pf.Enderecos[0].Numero.ToString(); excelPF.BAIRRO1 = pf.Enderecos[0].Bairro; excelPF.COMP1 = pf.Enderecos[0].Complemento; excelPF.CEP1 = pf.Enderecos[0].CEP; excelPF.CIDADE1 = pf.Enderecos[0].Cidade.Nome; excelPF.SIGLAESTAD1 = pf.Enderecos[0].Cidade.Estado.Sigla; if (pf.Enderecos.Count > 1) { excelPF.LOGRADOURO2 = pf.Enderecos[1].Logradouro; excelPF.NUMEND2 = pf.Enderecos[1].Numero.ToString(); excelPF.BAIRRO2 = pf.Enderecos[1].Bairro; excelPF.COMP2 = pf.Enderecos[1].Complemento; excelPF.CEP2 = pf.Enderecos[1].CEP; excelPF.CIDADE2 = pf.Enderecos[1].Cidade.Nome; excelPF.SIGLAESTAD2 = pf.Enderecos[1].Cidade.Estado.Sigla; if (pf.Enderecos.Count > 2) { excelPF.LOGRADOURO3 = pf.Enderecos[2].Logradouro; excelPF.NUMEND3 = pf.Enderecos[2].Numero.ToString(); excelPF.BAIRRO3 = pf.Enderecos[2].Bairro; excelPF.COMP3 = pf.Enderecos[2].Complemento; excelPF.CEP3 = pf.Enderecos[2].CEP; excelPF.CIDADE3 = pf.Enderecos[2].Cidade.Nome; excelPF.SIGLAESTAD3 = pf.Enderecos[2].Cidade.Estado.Sigla; } } } if (pf.Empresas.Count > 0) { excelPF.EMPRESA1 = pf.Empresas[0].PJ.Nome; excelPF.CARGO1 = pf.Empresas[0].Cargo.Nome; excelPF.DPTO1 = pf.Empresas[0].Departamento.Nome; if (pf.Empresas.Count > 1) { excelPF.EMPRESA2 = pf.Empresas[1].PJ.Nome; excelPF.CARGO2 = pf.Empresas[1].Cargo.Nome; excelPF.DPTO2 = pf.Empresas[1].Departamento.Nome; if (pf.Empresas.Count > 2) { excelPF.EMPRESA3 = pf.Empresas[2].PJ.Nome; excelPF.CARGO3 = pf.Empresas[2].Cargo.Nome; excelPF.DPTO3 = pf.Empresas[2].Departamento.Nome; } } } listaExcelPF.Add(excelPF); } return listaExcelPF; }
public byte[] ImportaPF(Stream file) { List<ExcelPFModel> lista = new List<ExcelPFModel>(); using (var package = new ExcelPackage(file)) { ExcelWorksheet worksheet = package.Workbook.Worksheets[1]; ExcelPFModel excelRow; for (int rowE = 2; worksheet.Cells[rowE, 1].Value != null; rowE++) { excelRow = new ExcelPFModel(); excelRow.NOME = trataValor(worksheet.Cells[rowE, 1]); excelRow.PROFISSAO = trataValor(worksheet.Cells[rowE, 2]); excelRow.ORIGEM = trataValor(worksheet.Cells[rowE, 3]); excelRow.INAUGURACAO = trataValor(worksheet.Cells[rowE, 4]); excelRow.ESPECIAL = trataValor(worksheet.Cells[rowE, 5]); excelRow.DDD1 = trataValor(worksheet.Cells[rowE, 6]); excelRow.NUMTEL1 = trataValor(worksheet.Cells[rowE, 7]); excelRow.TIPOTEL1 = trataValor(worksheet.Cells[rowE, 8]); excelRow.EMAIL1 = trataValor(worksheet.Cells[rowE, 9]); excelRow.LOGRADOURO1 = trataValor(worksheet.Cells[rowE, 10]); excelRow.NUMEND1 = trataValor(worksheet.Cells[rowE, 11]); excelRow.BAIRRO1 = trataValor(worksheet.Cells[rowE, 12]); excelRow.COMP1 = trataValor(worksheet.Cells[rowE, 13]); excelRow.CEP1 = trataValor(worksheet.Cells[rowE, 14]); excelRow.CIDADE1 = trataValor(worksheet.Cells[rowE, 15]); excelRow.SIGLAESTAD1 = trataValor(worksheet.Cells[rowE, 16]); excelRow.EMPRESA1 = trataValor(worksheet.Cells[rowE, 17]); excelRow.CARGO1 = trataValor(worksheet.Cells[rowE, 18]); excelRow.DPTO1 = trataValor(worksheet.Cells[rowE, 19]); excelRow.DDD2 = trataValor(worksheet.Cells[rowE, 20]); excelRow.NUMTEL2 = trataValor(worksheet.Cells[rowE, 21]); excelRow.TIPOTEL2 = trataValor(worksheet.Cells[rowE,22]); excelRow.EMAIL2 = trataValor(worksheet.Cells[rowE, 23]); excelRow.LOGRADOURO2 = trataValor(worksheet.Cells[rowE, 24]); excelRow.NUMEND2 = trataValor(worksheet.Cells[rowE, 25]); excelRow.BAIRRO2 = trataValor(worksheet.Cells[rowE, 26]); excelRow.COMP2 = trataValor(worksheet.Cells[rowE, 27]); excelRow.CEP2 = trataValor(worksheet.Cells[rowE, 28]); excelRow.CIDADE2 = trataValor(worksheet.Cells[rowE, 29]); excelRow.SIGLAESTAD2 = trataValor(worksheet.Cells[rowE, 30]); excelRow.EMPRESA2 = trataValor(worksheet.Cells[rowE, 31]); excelRow.CARGO2 = trataValor(worksheet.Cells[rowE, 32]); excelRow.DPTO2 = trataValor(worksheet.Cells[rowE, 33]); excelRow.DDD3 = trataValor(worksheet.Cells[rowE, 34]); excelRow.NUMTEL3 = trataValor(worksheet.Cells[rowE, 35]); excelRow.TIPOTEL3 = trataValor(worksheet.Cells[rowE, 36]); excelRow.EMAIL3 = trataValor(worksheet.Cells[rowE, 37]); excelRow.LOGRADOURO3 = trataValor(worksheet.Cells[rowE, 38]); excelRow.NUMEND3 = trataValor(worksheet.Cells[rowE, 39]); excelRow.BAIRRO3 = trataValor(worksheet.Cells[rowE, 40]); excelRow.COMP3 = trataValor(worksheet.Cells[rowE, 41]); excelRow.CEP3 = trataValor(worksheet.Cells[rowE, 42]); excelRow.CIDADE3 = trataValor(worksheet.Cells[rowE, 43]); excelRow.SIGLAESTAD3 = trataValor(worksheet.Cells[rowE, 44]); excelRow.EMPRESA3 = trataValor(worksheet.Cells[rowE, 45]); excelRow.CARGO3 = trataValor(worksheet.Cells[rowE, 46]); excelRow.DPTO3 = trataValor(worksheet.Cells[rowE, 47]); excelRow.OBSERVACAO = trataValor(worksheet.Cells[rowE, 48]); lista.Add(excelRow); } } List<ExcelPFModel> listaErro = new List<ExcelPFModel>(); foreach (var row in lista) { try { InserirPF(row); } catch (Exception ex) { row.ERRO = ex.Message; listaErro.Add(row); } } if (listaErro != null && listaErro.Count > 0) return FormatExcelPF(listaErro); else return null; }