Generate txt file according to default layout in C #

3

I need to export data from a class according to a specific layout that consists of size and position.

For easy maintenance, I need a simple layout change.

Below is my class:

    public class NovosCotistasDTO
    {
        public string Agencia { get; set; }
        public string Conta { get; set; }
        public DateTime DataInclusao { get; set; }
        public string Tipo { get; set; }
        public string IdUsuario { get; set; }
        public string CNPJ { get; set; }
    }  

Below is the layout that should be exported:

Campo           |Tamanho    |Posição Inicial    |Posição Final
--------------------------------------------------------------
Agencia         |5          |1                  |5
Conta           |13         |6                  |19
DataInclusao    |10         |20                 |30
Tipo            |1          |31                 |32
IdUsuario       |7          |33                 |40
CNPJ            |14         |41                 |55
    
asked by anonymous 16.07.2015 / 16:56

2 answers

4

The FileHelpers library does exactly what you need.

With it you can define file layouts by size and position.

In your specific case, after importing FileHelpers into your project, you would implement it as follows:

Class Defining Layout

Response Editing: You need to use simple fields rather than properties (get / set).

using FileHelpers;

[FixedLengthRecord(FixedMode.ExactLength)]
public class NovosCotistasDTO
{
    [FieldFixedLength(5)]
    public string agencia;

    [FieldFixedLength(13)]
    public string conta;

    [FieldFixedLength(10)]
    [FieldConverter(ConverterKind.Date, "ddMMyyyy")] // Outros formatos de data são possíveis. Ex: "DD/MM/AAAA", "ddMMyyyyHHmmss", etc.
    public DateTime dataInclusao;

    [FieldFixedLength(1)]
    public string tipo;

    [FieldFixedLength(7)]
    public string idUsuario;

    [FieldFixedLength(14)]
    public string cnpj;
} 

How to Record the Records in the TXT File

Writing a log file using FileHelpers is very simple. You get a list of the objects you want to save to a file and switch to FileHelpers as follows:

public void EscrevaArquivo(string nomeDoArquivo, List<object> registros)
{
    FileHelperEngine engine = new FileHelperEngine(typeof(NovosCotistasDTO));

    engine.WriteFile(nomeDoArquivo, registros);
}

How to Read the Saved Records in the File

To get back the records you saved using this layout, just do the following:

public List<NovosCotistasDTO> LeiaArquivo(string nomeDoArquivo)
{
    var engine = new FileHelperEngine(typeof(NovosCotistasDTO));

    var linhasDoArquivo = engine.ReadFile(nomeDoArquivo);

    var listaDeRegistros = new List<NovosCotistasDTO>();

    foreach (var linha in linhasDoArquivo)
    {
        lista.Add((NovosCotistasDTO)linha);
    }

    return listaDeRegistros;
}
    
16.07.2015 / 17:34
1

There are several ways to solve it. One of my favorites is to make a override of ToString() :

public class NovosCotistasDTO
{
    public string Agencia { get; set; }
    public string Conta { get; set; }
    public DateTime DataInclusao { get; set; }
    public string Tipo { get; set; }
    public string IdUsuario { get; set; }
    public string CNPJ { get; set; }

    public override String ToString() 
    {
        return Agencia.Replace("-", "") + 
            Conta.Replace("-", "").Replace(".", "") +
            DataInclusao.ToString("dd/MM/yyyy") +
            Tipo.Substring(0, 30) +
            IdUsuario.Substring(0, 7) +
            CNPJ.Replace("-", "").Replace(".", "").Replace("/", "")
    }
}

Usage:

var cotistaDto = new NovosCotistasDTO 
{
    Agencia = "1234",
    Conta = "12345-6",
    DataInclusao = DateTime.Now,
    Tipo = "Tipo",
    IdUsuario = "Usuario",
    CNPJ = "12.345.678/0001-99"
};

String layout = cotistaDto.ToString();

Note that this solution is quite simple, and the character control is entirely manual.

Using String.Format you can do something even more sophisticated, but you need to better understand what your data conditions are like:

  • What happens when a data is greater than the mask;
  • How data is allocated in your DTO;
  • Other validation conditions.
16.07.2015 / 17:17