我有一个对象列表,我想将它导出到一个文本文件中。我希望属性名称是列标题。
我已经这样做了
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文件(列下的值)?