Recently, I was doing Excel export and found this method. Although it was not written by myself, it is worth adding to my favorites. But I forgot to extract it from it and did not write it to the original author. Sorry!
1 # region export Excel 2 /// <summary> 3 // list to convert able 4 /// </Summary> 5 /// <Param name = "reslist"> </param> 6 // <returns> </returns> 7 public datatable dataout (list <Excel> reslist) 8 {9 10 datatable tempdt = new datatable (); 11 system. reflection. propertyinfo [] P = reslist [0]. getType (). getproperties (); 12 foreach (system. reflection. propertyinfo PI in P) 13 {14 tempdt. columns. add (Pi. name); 15} 16 for (INT I = 0; I <reslist. count; I ++) 17 {18 arraylist templist = new arraylist (); 19 // write a record in ilist to arraylist20 foreach (system. reflection. propertyinfo PI in P) 21 {22 object oo = pi. getvalue (reslist [I], null); 23 templist. add (OO); 24} 25 26 object [] ITM = new object [p. length]; 27 // traverse the arraylist to put data 28 for (Int J = 0; j <templist. count; j ++) 29 {30 ITM. setvalue (templist [J], J); 31} 32 // put the content of object [] into ableable33 tempdt. loaddatarow (ITM, true); 34} 35 // return datatable36 return tempdt; 37}
View code
C # list to able