Sunday, 15 July 2012

c# - Help converting generic List<T> to Excel spreadsheet -


I am trying to create a function that is a normal list & lt; T & gt; accepts and rewrites the list returning an excel file byte [] . Function object must be able to determine properties So if I have a list & lt; Person & gt; and I need to keep the assets of the person in the first, the last, the age, etc., then I should be able to determine the property names to create the superior column headers and then I need it column Repeat the list to assign property values ​​to cells. Does anyone give me a list in a normal function & lt; T & gt; Does point to some sample code to work with?

On one side: to get back the column in a known order: there is There is no defined order for members, except for what you create (for example):

The GetProperties method does not return properties in a specific order, such as alphabetical or announcement order. Your code should not depend on the order in which the properties are returned because the order changes.

If you do not have to trust the order, then what will the image or typewriter do? For example (Given that it writes TSV text, byte [] - my interpretation is that the problem is getting data, Excel is not typing):

< Pre> still zero WriteTsv & lt; T & gt; (This IEnumerable & lt; T & gt; data, text recovery output) {PropertyDescriptorCollection props = TypeDescriptor.GetProperties (typeof (t)); Foreign Currency (Property Descriptor Prop Prop) {output.Write (prop.DisplayName); // headers output. Type ("\ t"); } Output.WriteLine (); Foreign Currency (T Items in Data) {foreach (Property Descriptor Prop in Exponent) {output.Write (prop.Converter.ConvertToString (prop.GetValue (item))); Output.Write ("\ t"); } Output.WriteLine (); }}

If you want the requirement command, then you will need it either:

  • Pass it (
  • Use attributes on properties
  • Use the alphabet
P> TypeDescriptor The advantages of the approach given above are (Gettype). GetProperties () ):

  • This custom object model (< Code> DataView , for example, if you use IList )
  • You can zoom in to implement implementation - for example (useful if you're doing this much)

No comments:

Post a Comment