Export Object List content in a Text file in a tabular order using C#

164 Views Asked by At

i have an object list and i Would like to export it in a text file. I would like that the properties name are the column header.

I 've done this

public static void Write(IList<ValidationResultAttribute> dt, string filePath)
        {
            int i = 0;
            StreamWriter sw = null;
            sw = new StreamWriter(filePath, false);

            PropertyInfo[] properties = typeof(ValidationResultAttribute).GetProperties();
            // write columns header
            foreach (PropertyInfo property in properties)
            {
                sw.Write(property.Name + "  ");
            }
            sw.WriteLine();


            // write value
            foreach (ValidationResultAttribute res in dt)
            {
                PropertyInfo[] prop = typeof(ValidationResultAttribute).GetProperties();

                foreach (PropertyInfo property in prop)
                {
                    sw.Write(property.GetValue(res, null) + "  ");
                }
                sw.WriteLine();
            }
            sw.Close();
        }
    }

but I've this output

PresentationName    SlideName   ShapeName   RunIndexs   Attribute   Rule        Fail    Pass  
pptTest.pptx        Slide1      Rectangle 3 FontSize    Value       22          1       0  
pptTest.pptx        Slide2      TextBox 3   FontSize    Between     20and 72    1       0  

there is a way to format the output txt file (value under the column)?

2

There are 2 best solutions below

0
On

Do following code changes to create tab delimited file. This file is easy to parse and easy to read.

  1. sw.Write(property.Name + " "); change this to sw.Write(property.Name + "\t");

  2. sw.Write(property.GetValue(res, null) + " "); change this to sw.Write(property.GetValue(res, null) + "\t");

0
On

You can use string.format to get the desired result. Works also with sw.Write(format, args)

sw.Write("[{0,-20}|{1,10}]", "UnitPrice", 3.4457M);

will write

[UnitPrice           |    3,4457]

A negative value behind the format specifier mean left align a positiv value means right align.

There is one pitfall, This methods will not truncate your data, so

    sw.Write("[{0,-20}|{1,10}]", "ThisStringIsLongerThanExpected", 3.4457M);

will result in

[ThisStringIsLongerThanExpected|    3,4457]

So choose the values large enough or trim your strings to fit.

In your case you could calculate the length based on which is longer the properties name or it's value.

        var values = new List<KeyValuePair<string, object>();
        PropertyInfo[] properties = typeof(ValidationResultAttribute).GetProperties();

        foreach (PropertyInfo property in properties)
        {
            values.Add(property.Name, property.GetValue(res, null);
        }

        foreach(var value in values)
        {
            var length = Math.Max(value.Key.Length, value.Value.ToString().Length);
            var format = "{0,-" + length.ToString() + "} ";
            sw.Write(format, value.Key);
        }
        sw.WriteLine();

        foreach(var value in values)
        {
            var length = Math.Max(value.Key.Length, value.Value.ToString().Length);
            var format = "{0,-" + length.ToString() + "} ";
            sw.Write(format, value.Value);
        }
        sw.WriteLine();