Export data to Excel 2007 multiple sheet pages (NPOI) (Web version)

Source: Internet
Author: User
Tags rowcount

Background code:

public static string export2007<t> (Ilist<t> sourcedata) {if (SourceData = = null) throw new            ArgumentNullException ("SourceData");            Iworkbook workbook = new Xssfworkbook ();            var properties = typedescriptor.getproperties (typeof (T));            The number of records exported by a sheet const int pageSize = 20000;            Total records var rowCount = Sourcedata.count;            Total sheet number var PageCount = (RowCount + pageSize-1)/pageSize;            var count = 1; while (Count <= pagecount) {var list = Count = = 1? Sourcedata.skip (0). Take (pageSize). ToList (): Sourcedata.skip ((count-1) *pagesize). Take (pageSize).                ToList (); if (list. Any ()) {var sheet = workbook.                    Createsheet (String.Format ("sheet{0}", count)); var row = sheet.                    CreateRow (0); for (var i = 0; i < properties. CounT i++) {var cell = row.                        Createcell (i); Cell. Setcellvalue (String.IsNullOrEmpty (properties[i). Description)? Properties[i]. Name:properties[i].                    Description); } for (var i = 0; i < list. Count; i++) {row = sheet.                        CreateRow (i + 1); for (var j = 0; J < properties. Count; J + +) {var cell = row.                            Createcell (j); var value = Properties[j].                            GetValue (List[i]); Cell. Setcellvalue (value = = null?) String.Empty: (value is DateTime?) ((DateTime) value). ToString ("Yyyy-mm-dd hh:mm"): Value.                        ToString ()));            }}} count++; } var dir = httpcontext.cuRrent.            Server.MapPath (String.Format ("~/resources/{0}/", DateTime.Now.ToString ("YyyyMMdd")); if (!            Directory.Exists (dir)) {directory.createdirectory (dir);            } var fileName = dir + DateTime.Now.ToString ("YYYYMMDDHHMMSS") + ". xlsx";            var fileStream = new FileStream (FileName, FileMode.Create, FileAccess.Write); Workbook.            Write (FileStream);        return fileName; }

Front Code:

Public ActionResult exportexecl (int? pid)        {            if (!pid. HasValue) return null;            var list = _userinfobo.getanswerbyuser (PID. Value);            if (null = = list) return null;            var newlist = list. Select (question = new Resultexecl ()            {              //Load Data            }). ToList ();            var FilePath = excelutil.export2007 (newlist);            var fileName = Path.getfilename (FilePath);            Return File (FilePath, "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);        }


Export data to Excel 2007 multiple sheet pages (NPOI) (Web version)

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.