Project Description
Allow to read and write CSV file and fixe length field file ( like mainframe file for example ).


This Project are base on mperdeck/LINQtoCSV the Original Project.
I make this project because I needed more functionality.

     var OrderList= slq.Order_.OrderBy(t => t.Order_Date).ThenBy(t => t.Order_Number);
     Write(OrderList,  "c:\Export\Order.csv", new ExportFileDescription() { FileCultureName = "fr-FR", SeparatorChar = ';', FirstLineHasColumnNames = true });

 public static void Write<T>(IQueryable<T> query, Stream stream, ExportFileDescription FileDescription)
        {
            StreamWriter tw = new StreamWriter(stream);
            using (FileContext cc = new FileContext())
            {
                cc.Write<T>(query, tw, FileDescription, pource);
            }
        }
 public static void Write<T>(IQueryable<T> query, string file, ExportFileDescription FileDescription)
        {
            using (FileContext cc = new FileContext())
            {
                cc.Write<T>(query, file, FileDescription, pource);
            }
        }

[FileColumn(FieldIndex = 3, Property = "Total", UsePropertyAsName=true , OutputFormat="C" )]
[FileColumn(FieldIndex = 5, Property = "PrixNet", UsePropertyAsName = true, MaxLength = 9, OutputFormat = "0000000.00", WithOutSeparator = true)]
    [FileColumn(FieldIndex = 6, Property = "DateFin", UsePropertyAsName = true, MaxLength = 8, OutputFormat = "yyyyMMdd", FillChar = '0')]

Sample
[FileColumn(FieldIndex = 01, Property = "A", MaxLength = 34, UsePropertyAsName = true)]
[FileColumn(FieldIndex = 02, Property = "B", MaxLength = 5, UsePropertyAsName = true, TextAlign = Align.Right,  FillChar = '0')]
[FileColumn(FieldIndex = 03, Property = "C", UsePropertyAsName = true, MaxLength = 9, OutputFormat = "0000000.00", WithOutSeparator = true)]
[FileColumn(FieldIndex = 04, Property = "D", MaxLength = 8, Name = "F", OutputFormat = "yyyyMMdd", FillChar = '0')]
[FileColumn(FieldIndex = 05, Property = "E", UsePropertyAsName = true, MaxLength = 2, OutputFormat = "00")]
 public class Test
    {
       public string A { get; set; }
       public int B { get; set; }
       public decimal C { get; set; }
       public DateTime D { get; set; }
       public short E { get; set; }
      
    }

static void Main(string[] args)
        {
            cree();
            FileContext cc = new FileContext();
            IEnumerable<Test> tt = cc.Read<Test>(Path.Combine(m_Path, "test.csv"), new MainFrameFileDescription(';'));
            cc.Write<Test>(tt, Path.Combine(m_Path, "testRet.txt"), new MainFrameFileDescription());
        }

        static void cree()
        {
            HashSet<Test> lists = new HashSet<Test>();
            lists.Add( new Test() { A ="Vache", B= 5, C=25.4m, D = new DateTime(2014,05,12), E = 3 });
            lists.Add(new Test() { A = "train", B = 70, C = 100m, D = new DateTime(2013, 05, 12), E = 5 });
            lists.Add(new Test() { A = "voiture", B = 10, C = 6.01m, D = new DateTime(2012, 07, 18), E = 6 });
            lists.Add(new Test() { A = "Chat", B = 6, C = 658.5m, D = new DateTime(2014, 03, 1), E = 0 });
            lists.Add(new Test() { A = "Chien", B = 54, C = 4.56m, D = new DateTime(2014, 11, 25), E = 1 });
            FileContext cc = new FileContext();
            cc.Write<Test>(lists.AsEnumerable(), Path.Combine(m_Path, "test.csv"), new MainFrameFileDescription(';'));

        }

Result
test.csv
Vache;5;000002540;20140512;03
train;70;000010000;20130512;05
voiture;10;000000601;20120718;06
Chat;6;000065850;20140301;00
Chien;54;000000456;20141125;01

testRet.txt
Vache                             000050000025402014051203
train                             000700000100002013051205
voiture                           000100000006012012071806
Chat                              000060000658502014030100
Chien                             000540000004562014112501

Last edited Aug 4, 2014 at 7:35 AM by Xolen, version 5