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)