. NET core Npoi-based Excel export classes that support customizing which fields to export

Source: Internet
Author: User

<summary>///export Excel///</summary>//<param name= "lists" &GT;&LT;/PARAM&G          T          <param name= "Head" > English Chinese Column name control </param>//<param name= "Workbookfile" > Save path </param>        public static void Getexcel<t> (list<t> lists, dictionary<string,string> Head, String workbookfile)                {try {xssfworkbook workbook = new Xssfworkbook (); using (MemoryStream ms = new MemoryStream ()) {var sheet = workbook.                    Createsheet (); var headerrow = sheet.                    CreateRow (0);                    bool H = false;                    int j = 1;                    Type type = typeof (T); Propertyinfo[] Properties = type.                    GetProperties (); foreach (T item in lists) {var dataRow = sheet.                        CreateRow (j);        int i = 0;                foreach (PropertyInfo column in properties) {if (!h) {if (head. Keys.contains (column. Name) {Headerrow.createcell (i). Setcellvalue (Head[column. Name] = = null? Column. Name:head[column. Name].                                    ToString ()); Datarow.createcell (i). Setcellvalue (column. GetValue (item, NULL) = = null? "": Column. GetValue (item, NULL).                                ToString ());                                } else {i-= 1;                                }} else { if (head. Keys.contains (column. Name) {Datarow.createcell (i). Setcellvalue (column. GetvaLue (item, NULL) = = null? "": Column. GetValue (item, NULL).                                ToString ());                                } else {i-= 1;                        }} i++;                        } h = true;                    j + +; } workbook.                    Write (MS);                        using (FileStream fs = new FileStream (Workbookfile, FileMode.Create, FileAccess.Write)) { byte[] data = Ms.                        ToArray (); Fs. Write (data, 0, data.                        Length); Fs.                    Flush ();                    } sheet = null;                    HeaderRow = null;                workbook = null; }} catch (Exception ee) {string see = ee.            Message; }       } 

  

. NET core Npoi-based Excel export classes that support customizing which fields to export

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.