Directly attach code
1 /// <summary> 2 /// Save the List as Excel 3 /// </summary> 4 /// <typeparam name = "T"> Save the class type </typeparam> 5 // <param name = "lt"> source data to be saved </param> 6 /// <param name = "fileName"> File name </param> 7 // <param name = "fields"> corresponding to the field name of the class </param> 8 /// <param name = "titles"> name of the column in Excel </param> 9 public static void Save <T> (List <T> lt, string fileName, string [] fields, string [] titles) 10 {11 if (lt = null | lt. count = 0) 12 {13 throw new ArgumentNullException ("data is empty"); 14} 15 16 var sb = new StringBuilder (); 17 PropertyInfo [] props = typeof (T ). getProperties (BindingFlags. public | BindingFlags. instance); 18 const string next = "\ t"; 19 20 foreach (var title in titles) 21 {22 sb. append (title ). append (next); 23} 24 25 var propertys = new List <PropertyInfo> (); 26 27 foreach (var field in fields) 28 {29 foreach (PropertyInfo prop in props) 30 {31 if (prop. name. equals (field) 32 {33 propertys. add (prop); 34 break; 35} 36} 37} 38 39 sb. append (Environment. newLine); 40 41 foreach (T item in lt) 42 {43 foreach (var property in propertys) 44 {45 object value = property. getValue (item, null); 46 47 if (property. propertyType. baseType = typeof (Enum) 48 {49 sb. append (GPMSKernel. unility. enums. getEnumDescription (value); 50} 51 else if (property. propertyType = typeof (Boolean) 52 {53 if (bool) value) 54 {55 sb. append ("yes"); 56} 57 else58 {59 sb. append ("no"); 60} 61} 62 else63 {64 sb. append (value); 65} 66 67 sb. append (next); 68} 69 70 sb. append (Environment. newLine); 71} 72 73 fileName = string. format ("1_01__1_12.16.xls", DateTime. now. toString ("yyMMddHHmmss"), fileName); 74 HttpContext. current. response. contentType = "application/octet-stream"; 75 // notify the browser to download the file instead of opening 76 HttpContext. current. response. addHeader ("Content-Disposition", "attachment; filename =" + HttpUtility. urlEncode (fileName, Encoding. UTF8); 77 HttpContext. current. response. binaryWrite (Encoding. UTF8.GetBytes (sb. toString (); 78 HttpContext. current. response. flush (); 79 HttpContext. current. response. end (); 80}
This method downloads the List from the Web server to a local file and saves it as an Excel file, where lt is the data source to be saved and fileName is the corresponding file name, you can directly write it as the name of the class to be saved,
Fields corresponds to the field name of the class to be saved. This must be correct. Otherwise, this field cannot be found in the class. titles is the name of the column in the file to be saved, fields and titles must correspond to each other, and the number must be equal.
Special processing is performed before saving. For example, if an enumeration exists in the class to be saved, it will be saved to Excel in English. This is generally not what you want to see, note [Description ("***")] before each enumerated value.
GPMSKernel. unility. enums. getEnumDescription can obtain the Description value of the enumeration. If it is a Boolean value, it is saved as "True" or "False, I converted this type to "yes" or "no". If there are special classes in the class to be saved, we need to rewrite the ToString method in this special class, the ToString method will be used to save the data ....