C# 帮助将通用 List<T> 转换为 Excel 电子表格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1831024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Help converting generic List<T> to Excel spreadsheet
提问by Kevin McPhail
I am trying to create a function that accepts a generic List<T>
and iterates the list returning an excel file byte[]
. The function needs be able to determine the objects properties. So if i pass a List<person>
and person has properties first, last, age, etc i need to be able to determine the property names in order to create the excel column headers and then i need to iterate the list to assign the property values to the column cells. Can anyone point me to some sample code for working with List<T>
in a generic function?
我正在尝试创建一个接受泛型List<T>
并迭代返回 excel 文件的列表的函数byte[]
。该函数需要能够确定对象的属性。因此,如果我通过 aList<person>
并且 person 具有属性 first、last、age 等,我需要能够确定属性名称以创建 excel 列标题,然后我需要迭代列表以将属性值分配给列细胞。任何人都可以指出一些用于List<T>
在通用函数中使用的示例代码吗?
采纳答案by Marc Gravell
Aside: for getting columns back in a known order: there isno defined order for members, except that which you create. For example (from MSDN):
旁白:用于获取列早在已知的顺序:有是没有定义的顺序为会员,除了您创建。例如(来自 MSDN):
The GetProperties method does not return properties in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which properties are returned, because that order varies.
GetProperties 方法不以特定顺序返回属性,例如字母顺序或声明顺序。您的代码不得依赖于返回属性的顺序,因为该顺序会有所不同。
If you don't need to rely on the order, either reflection or TypeDescriptor would do; for example (noting that this writes TSV text, not byte[]
- my interpretation is that the problem is getting the data, not writing the Excel):
如果您不需要依赖顺序,反射或 TypeDescriptor 都可以;例如(请注意,这是写入 TSV 文本,而不是byte[]
- 我的解释是问题在于获取数据,而不是写入 Excel):
static void WriteTsv<T>(this IEnumerable<T> data, TextWriter output)
{
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));
foreach (PropertyDescriptor prop in props)
{
output.Write(prop.DisplayName); // header
output.Write("\t");
}
output.WriteLine();
foreach (T item in data)
{
foreach (PropertyDescriptor prop in props)
{
output.Write(prop.Converter.ConvertToString(
prop.GetValue(item)));
output.Write("\t");
}
output.WriteLine();
}
}
If you needorder, you would need to either:
如果您需要订购,您需要:
- pass it in (for example, as a
params string[] propertyNames
) - use attributes on the properties
- use alphabetical
- 传入(例如,作为
params string[] propertyNames
) - 在属性上使用属性
- 使用字母
The TypeDescriptor
approach above has the advantages (over GetType().GetProperties()
) that:
上述TypeDescriptor
方法具有以下优点(超过GetType().GetProperties()
):
- it works with custom object models (
DataView
, for example, if you useIList
) - you can tweak the implementation for performance - for example HyperDescriptor(useful if you are doing this lots)
- 它适用于自定义对象模型(
DataView
例如,如果您使用IList
) - 您可以调整性能的实现 - 例如HyperDescriptor(如果您正在执行此操作,则很有用)
回答by Jeremy McGee
The easiest way is probably to convert your List to a DataTable, then convert the DataTable to an Excel spreadsheet.
最简单的方法可能是将您的 List 转换为 DataTable,然后将 DataTable 转换为 Excel 电子表格。
The second link writes the spreadsheet directly out to an ASP.NET Response, this could easily be adapted to return a byte[].
第二个链接将电子表格直接写入 ASP.NET 响应,这可以很容易地调整为返回一个字节 []。
回答by jheddings
Use an interface that is supported by your collection, such as IEnumerable
:
使用您的集合支持的接口,例如IEnumerable
:
public byte[] Process(IEnumerable input) {
foreach (var elem in input) {
foreach (PropertyInfo prop in elem.GetType().GetProperties()) {
Object value = prop.GetValue(elem, null);
// add value to byte[]
}
}
return bytes;
}