Different header and footer FileHelpers

67 Views Asked by At

I am using the FileHelpers library to create a txt document which contains a list of employees, the document needs to be created with fixed size fields, I need to add a header and footer with a different structure than the body, for example in the header and footer it is necessary to write the total of records and the current date. How could I add these two sections?

public class Employee
{
   [FieldFixedLength(10)]
   public int Secuencial;

   //[FieldFixedLength(10)]
   //[FieldOptional]
   //public int Total;

   //[FieldFixedLength(10)]
   //[FieldConverter(ConverterKind.Date, "yyyy-MM-dd")]
   //[FieldOptional]
   //public DateTime CurrentDate;

   [FieldFixedLength(50)]
   public string Names;

   [FieldFixedLength(5)]
   public int Age;
}

The final file should look like this

  32022-09-26
1 Andres Valle 35
2 Sandra Calle 28
3 Pepe Granja  30
  32022-09-26

Code to write file:

public static void WriteFile(string outputPath, DataTable dt)
{
 var report = new FileHelperEngine<Employee>();
 var lstEmpl = new List<Employee>();
 Employee empl;

 //Employee header = new Employee();
 //header.Total = 3;
 //header.CurrentDate = DateTime.Today;

 foreach (DataRow dr in dt.Rows)
 {
   empl = new Employee();
   empl.Names = dr["names"].ToString();
   empl.Age = int.Parse(dr["age"].ToString());
   lstEmpl.Add(empl);
 }
 report.WriteFile(outputPath, lstEmpl);
//report.AppendToFile(outputPath, header);
}
1

There are 1 best solutions below

0
IamK On

You can wrap your employee object in a class, which has the header field an employee list and a footer field

public class EmployeeWrapper
{
    public string Header { get; set; }
    public List<Employee> Employees { get; set; }
    public string Footer { get; set; }
}