Code:
PublicList<t> importexceltolist<t>() {HttpContext context=HttpContext.Current; if(Context. Request.Files.AllKeys.Length >0) { varKey = Context. request.files.allkeys[0]; varFile =context. Request.files[key]; varFolderName =path.getfilenamewithoutextension (file. FileName); varFileExtension =path.getextension (file. FileName); Workbook Book=NewWorkbook (file. InputStream); Worksheet sheet= Book. worksheets[0]; Cells Cells=sheet. Cells; varda = cells[0,1]. Value; varDataTable = cells. Exportdatatable (0,0, cells. Maxdatarow +1, cells. Maxcolumn,true); varDataList = Getlist<t>(dataTable); //you need to assign a value to an empty cell after you split the cell componentType type =typeof(T); Propertyinfo[] Property=type. GetProperties (); for(vari =0; I < DataList. Count (); i++) { varLi =Datalist[i]; foreach(varPiinchProperty ) { if(pi.) Name = ="Component") { varValue =Pi. GetValue (LI); if(Value = =NULL) { varv = pi. GetValue (Datalist[i-1]); Pi. SetValue (Li, v); } } } } returnDataList; } return NULL; } PublicList<t> getlist<t>(DataTable table) {List<T> list =NewList<t>(); T T=default(T); Propertyinfo[] Propertypes=NULL; stringTempname =string. Empty; foreach(DataRow rowinchtable. Rows) {T= activator.createinstance<t>(); Propertypes=T.gettype (). GetProperties (); foreach(PropertyInfo Proinchpropertypes) {Tempname=Pro. Name; if(table. Columns.contains (Tempname)) {ObjectValue =Row[tempname]; if(!value. ToString (). Equals ("")) { if(Pro. Name = ="newduct"|| Pro. Name = ="existingduct"|| Pro. Name = ="newaerial"|| Pro. Name = ="existingaerial") { varv =Changepropertytype (pro. Propertytype,value); Pro. SetValue (t, V,NULL); Continue; } pro. SetValue (t, value,NULL); }}} list. ADD (t); } returnList. Count = =0?NULL: List; } Private ObjectChangepropertytype (Type type,Objectvalue) { Objectdata; if(Type. Fullname.tolower (). Contains ("decimal") ) {Data=0; Data=Convert.todecimal (value); } Else{Data=""; Data=convert.tostring (value); } returndata; }
Turn into a tree structure
PrivateList<projectitem> Additemtotree (list<mergeitem>list) { varDataList =NewList<projectitem>(); varComponent =""; Propertyinfo[] Property=typeof(Mergeitem). GetProperties (); foreach(varLiinchlist) { varitem =NewProjectItem (); if(string. IsNullOrEmpty (li. subcomponent)) {component=li.component; Item. subcomponent=NULL; Item. Itemlevel="Level1"; Item. Unit=NULL; } Else{item. subcomponent=li.component; Item. Itemlevel="Level2"; Item. Unit=NULL; //build the third level foreach(varPiinchProperty ) { varScenario =""; if(pi.) Name = ="newduct") {Scenario="New Duct"; } Else if(pi.) Name = ="existingduct") {Scenario="Existing Duct"; } Else if(pi.) Name = ="newaerial") {Scenario="New Aerial"; } Else if(pi.) Name = ="existingaerial") {Scenario="Existing Aerial"; } Else { Continue; } varValue =Pi. GetValue (LI); if(Value! =NULL) { varChild =NewProjectItem (); Child.component=component; Child. subcomponent=item. subcomponent; Child. Description=Li. subcomponent; Child. Unit=Li. Unit; Child. Scenario=scenario; Child. UnitCost=Convert.todecimal (value); Child. Remark=Li. Remark; Child. Fillinstruct=Li. Fillinstruct; Child. Itemlevel="Level3"; DataList. ADD (child); } }} item.component=component; Item. Description=li.component; Item. Scenario=NULL; Item. UnitCost=NULL; Item. Remark=NULL; Item. Fillinstruct=NULL; varIsexist = DataList. Any (p = p.component = = Item.component && p.subcomponent = = Item. subcomponent &&p.description= = Item. Description && P.itemlevel = ="Level2"); if(!isexist) {DataList. ADD (item); } } returnDataList; }
aspose------Importing Excel