1 Public StaticSystem.IO.MemoryStream exportexcel<t> (stringTitle, List<t> objlist,params string[] excelpropertynames)2 {3Npoi. Ss. Usermodel.iworkbook Workbook =NewNpoi. HSSF. Usermodel.hssfworkbook ();4Npoi. Ss. Usermodel.isheet sheet = workbook. Createsheet ("Sheet1");5 Npoi. Ss. Usermodel.irow Row;6 Npoi. Ss. Usermodel.icell cell;7 Npoi. Ss. Usermodel.icellstyle CellStyle;8 9 intRowNum =0;Ten if(!string. IsNullOrEmpty (title)) One { A #regionTitle - #regionHeading styles -CellStyle =workbook. Createcellstyle (); theCellstyle.alignment =Npoi. Ss. UserModel.HorizontalAlignment.Center; -Cellstyle.verticalalignment = Npoi. Ss. UserModel.VerticalAlignment.Center;//problem with vertical centering -Npoi. Ss. Usermodel.ifont Font =workbook. CreateFont (); -Font. Fontheightinpoints = the; + Cellstyle.setfont (font); - #endregion +row =sheet. CreateRow (rowNum); ACell = row. Createcell (0, Npoi. Ss. UserModel.CellType.String); at cell. Setcellvalue (title); -Cell. CellStyle =CellStyle; -Sheet. Addmergedregion (NewNpoi. Ss. Util.cellrangeaddress (0,0,0, Excelpropertynames.length >0? Excelpropertynames.length-1:0)); -rownum++; - #endregion - } in - if(Objlist.count >0) to { +Type type = objlist[0]. GetType (); - if(Type! =NULL) the { *System.reflection.propertyinfo[] Properties =type. GetProperties (); $ if(Properties. Length >0)Panax Notoginseng { - #regionTable header the #regionTable header Style +CellStyle =workbook. Createcellstyle (); ACellstyle.alignment =Npoi. Ss. UserModel.HorizontalAlignment.Center; the #endregion + if(Excelpropertynames.length >0) - { $row =sheet. CreateRow (rowNum); $ intCount =0; - for(intm =0; M < properties. Length; m++) - { the if(Excelpropertynames.contains (properties[m). Name)) - {WuyiCell =row. Createcell (count, Npoi. Ss. UserModel.CellType.String); the stringDisplayName =Getdisplaynamebypropertyname (Properties[m]. Name); -Cell. Setcellvalue (DisplayName = =NULL?"": displayName); WuCell. CellStyle =CellStyle; -count++; About } $ } -rownum++; - } - #endregion A + #regionTable Body the if(Excelpropertynames.length >0) - { $ for(inti =0; i < Objlist.count; i++) the { therow = sheet. CreateRow (i +rowNum); the intCount =0; the for(intj =0; J < Properties. Length; J + +) - { in if(Excelpropertynames.contains (properties[j). Name)) the { theCell =row. Createcell (count); About Objectobj =Properties[j]. GetValue (Objlist[i]); theCell. Setcellvalue (obj = =NULL?"": obj. ToString ()); theCell. CellStyle =CellStyle; thecount++; + } - } the }Bayi } the #endregion the } - } - } theSystem.IO.MemoryStream ms =NewSystem.IO.MemoryStream (); the workbook. Write (MS); the returnMS; the } - the Public Static stringGetdisplaynamebypropertyname (stringPropertyName) the { the stringresult =NULL;94 foreach(keyvaluepair<string,string> DICinchnamedictionary ()) the { the if(DIC. Key = =PropertyName) the {98result =DiC. Value; About } - Continue;101 }102 returnresult;103 }104 the Public Staticdictionary<string,string>namedictionary ()106 {107dictionary<string,string> dic =Newdictionary<string,string>();108Dic. ADD ("Adminid","numbering");109 theDic. ADD ("AdminName","User name");111 theDic. ADD ("Adminmobile","Phone number");113 theDic. ADD ("Realname","real name"); the the returndic;117}
Call is simple
1 Publicactionresult Test ()2 {3 intTotalCount;4list<adminmodel> adminmodellist = Adminbll.getpagelist (1,Ten, outtotalcount);5 if(Adminmodellist = =NULL)6 {7Adminmodellist =NewList<adminmodel>();8 }9 returnFile (Excelhelper.exportexcel<adminmodel> ("Table Header", Adminmodellist,"Adminid","AdminName","Adminmobile","Realname"). ToArray (),"Application/vnd.ms-excel","Workbook . xls");Ten}