【问题标题】:Export Object List content in a Text file in a tabular order using C#使用 C# 以表格顺序导出文本文件中的对象列表内容
【发布时间】:2015-05-27 09:57:04
【问题描述】:

我有一个对象列表,我想将它导出到一个文本文件中。 我希望属性名称是列标题。

我已经做到了

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();
        }
    }

但我有这个输出

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  

有没有办法格式化输出txt文件(列下的值)?

【问题讨论】:

    标签: c# text-files export-to-text


    【解决方案1】:

    您可以使用 string.format 来获得所需的结果。也适用于sw.Write(format, args)

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

    会写

    [UnitPrice           |    3,4457]
    

    格式说明符后面的负值表示左对齐,正值表示右对齐。

    有一个陷阱,这个方法不会截断你的数据,所以

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

    会导致

    [ThisStringIsLongerThanExpected|    3,4457]
    

    所以选择足够大的值或修剪你的字符串以适应。

    在您的情况下,您可以根据属性名称或其值较长来计算长度。

            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();
    

    【讨论】:

      【解决方案2】:

      执行以下代码更改以创建制表符分隔文件。该文件易于解析,易于阅读。

      1. sw.Write(property.Name + " ");将此更改为sw.Write(property.Name + "\t");

      2. sw.Write(property.GetValue(res, null) + " ");将此更改为sw.Write(property.GetValue(res, null) + "\t");

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多