I have a TXT file generated by SISOBI , which contains a list of people who died.
This txt is generated, and I need to read the file and compare the CPF
, which is the only key present in the file, with my database and check if it has any employee who has the same% / p>
I upload the file, separate people by the amount of characters (210), and remove the CPF
through CPF
.
My problem is how to compare SubString
with the data returned from my table.
My CPF
that performs these actions looks like this:
public ActionResult Index(HttpPostedFileBase file)
{
//verifica se o arquivo está nulo
if (file == null)
{
TempData["MensagemError"] = "Erro ao realizar o upload do arquivo!";
return View("Index");
}
//Salvar o arquivo txt
string path = Path.Combine(Server.MapPath("~/App_Data/Uploads/" + Path.GetFileName(file.FileName)));
file.SaveAs(path);
//Realiza a leitura do arquivo txt
var fileContents = System.IO.File.ReadAllText(path);
//Separa o texto em blocos de 210 caracteres, conforme o Layout
var partes = SplitBlocks(fileContents, 212);
foreach (var parte in partes)
{
var Data = parte.Substring(155, 8);
var Cpf = parte.Substring(163, 11);
}
//Separa os dados pelo substring e salva em suas variáveis
var DtObito = fileContents.Substring(155, 8);
var CPF = fileContents.Substring(163, 11);
//converte data para o formato dd/MM/yyyy
var dtMorte = DtObito.Substring(6, 2) + "/" + DtObito.Substring(4, 2) + "/" + DtObito.Substring(0, 4);
//Converte o CPF para int
var cpfcerto = Convert.ToInt64(CPF);
//Consulta dos usuários com a variável cpf
var usuarios = usuarioRepository.Lista.Where(u => u.NrCpf == cpfcerto).ToList();
if (usuarios.Count > 0)
{
TempData["UsuarioEncontrado"] = "Existe funcionário.";
return View(usuarios);
}
TempData["Usuario"] = "Nenhum funcionário encontrado.";
return View();
}
In this way you are only using Controller
to check if you are actually reading the file.
I thought of putting the query inside Substring
, but the query is very time consuming, because the For
file has more than 100 thousand people and an average of 22mb in size.
Each person with their respective data is 210 characters in length. To perform this separation I use this method.
public static List<String> SplitBlocks(string texto, int tamanho)
{
var partes = new List<String>();
var posicao = 0;
var total = texto.Length;
while (total >= posicao + tamanho)
{
partes.Add(texto.Substring(posicao, tamanho));
posicao += tamanho;
}
return partes;
}
Tabela Funcionários para comparação.
[Key]
[Column("CdPessoa")]
public double iUsuarioID { get; set; }
public string Cod_Lotacao { get; set; }
public string Descricao { get; set; }
public string NmFuncionario { get; set; }
public string nmMunicipio { get; set; }
public string NaTipoLogradouro { get; set; }
public string nmLogradouro { get; set; }
public int CdCep { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime DtNascimento { get; set; }
public double NrCpf { get; set; }
I wonder if there is a way to do this. Using txt
to save the ViewModel
data and compare the two.
Save the data of txt
in the Database, and after comparing the two.