Web page HTML format export excel.xls, htmlexport excel.xls

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.