Web page HTML format export excel.xls, htmlexport excel.xls
1. Create an excel file
/// <Summary> /// create an Excel table /// </summary> /// <param name = "dt"> data stream </param> /// <param name = "FileName"> file name </param> public static void CreateExcel (DataTable dt, string FileName) {StringBuilder strb = new StringBuilder (); strb. append ("View Code
2. The List set is converted to DateTable. The name is specified based on the attribute [System. ComponentModel. Description ("column name")]. If no name column is specified, it is not exported.
# Convert the region List set to DateTable /// <summary> /// convert the List set to DateTable /// </summary> /// <typeparam name = "T"> generic class </typeparam> /// <param name = "items"> parameter name </param> /// <returns> </returns> private DataTable ToDataTable <T> (List <t> items) {var tb = new DataTable (typeof (T ). name); List <PropertyInfo> props = typeof (T ). getProperties (BindingFlags. public | BindingFlags. instance ). toList (); # region MyRegion // foreach (Pr OpertyInfo prop in props) // {// string name = ""; // obtain the feature value // object [] da = prop. getCustomAttributes (typeof (DescriptionAttribute), false); // if (da. length! = 0) // {// name = (da [0] as DescriptionAttribute ). description; // Type t = GetCoreType (prop. propertyType); // tb. columns. add (name, t); // else // {// name = prop. name; // props. remove (prop); //} # endregion for (int I = 0; I <props. count; I ++) {string name = ""; // obtain the feature value object [] da = props [I]. getCustomAttributes (typeof (DescriptionAttribute), false); if (da. length! = 0) {name = (da [0] as DescriptionAttribute ). description; Type t = GetCoreType (props [I]. propertyType); tb. columns. add (name, t);} else {// name = prop. name; props. remove (props [I]); I -- ;}} foreach (T item in items) {var values = new object [props. count]; for (int I = 0; I <props. count; I ++) {values [I] = props [I]. getValue (item, null);} tb. rows. add (values);} return tb;} public static bool IsNu Llable (Type t) {return! T. IsValueType | (t. IsGenericType & t. GetGenericTypeDefinition () = typeof (Nullable <>);} public static Type GetCoreType (Type t) {if (t! = Null & IsNullable (t) {if (! T. IsValueType) {return t;} else {return Nullable. GetUnderlyingType (t) ;}} else {return t ;}# endregion
View Code
3. Create a data source and call the Method
Public void UserWithdrawExcelList () {// data source. You can obtain the List <UserWithdrawQueryViewModel> userList = UserWithdrawBLL as needed. getUserWithdrawListExcel (paramdic); DataTable dt = ToDataTable <UserWithdrawQueryViewModel> (userList); # region MyRegion // DataTable dt = new DataTable (); // dt. columns. add ("username", typeof (String); // dt. columns. add ("withdrawal title", typeof (String); // dt. columns. add ("total withdrawal amount", typeof (String); // dt. columns. add ("actual amount received", typeof (String); // dt. columns. add ("withdrawal service fee", typeof (String); // dt. columns. add ("withdrawal account", typeof (String); // dt. columns. add ("withdrawal status", typeof (String); // dt. columns. add ("creation time", typeof (DateTime); // userList. tryForEach (t => // {// dt. rows. add (new object [] {// t. userName, t. withTitle, t. withTotalAmount, t. withAmount, t. countertimeout, t. bankCode, t. statusName, t. addTime //}); # endregion DataView dv = dt. defaultView; // obtain the table view dv. toTable (); // convert to DataChangeExcel. createExcel (dt, DateTime. now. toString ("yyyyMMddHHmmssffff") + ". xls ");}
View Code