Public voidServiceorderexport (stringdata) {StringBuilder SB=NewStringBuilder (); Type EntityType=NULL; ; Propertyinfo[] Entityproperties=NULL; varinput = data. Deserializeobject<structserviceorder>(); using(varContext =srvdbhelper.datacontext) {sb. Remove (0, sb. Length); varResults =context. Usp_srv_checkserviceorder (input. Serviceorderid, input. Acceptway, input. StatusCode, input. Description, input. Onelevelsortid, input. Twolevelsortid, input. Threelevelsortid, input. Ainsno, input. Acompanyname, input. Adepartmentid, input. Asectionid, input. AName, input. Cinsno, input. Ccompanyname, input. Cdepartmentid, input. Csectionid, input. Creatorname, input. Hinsno, input. Hcompanyname, input. Hdepartmentid, input. Hsectionid, input. Hname, input. CreateDate1, input. CreateDate2, input. FinishDate1, input. FinishDate2, input. Overduestatus1,input. OVERDUESTATUS2); List<Usp_SRV_CheckServiceOrderResult> Entitys =NULL; if(Input. Hname! =NULL) {Entitys= ( fromIteminchResultswhereItem. Processing Person! =NULL Selectitem). ToList (); } Else{Entitys=results. ToList (); } //Check Entity collection cannot be empty if(Entitys = =NULL|| Entitys. Count <1) { return; } //Remove all Propertie from the first entityEntityType = entitys[0]. GetType (); Entityproperties=entitytype.getproperties (); for(inti =0; i < entityproperties.length; i++) {sb. Append (Entityproperties[i]. Name); Sb. Append (","); } sb. Remove (sb.) Length-1,1); Sb. Append ("\ r \ n"); //Add all of the entity to the DataTable foreach(ObjectEntityinchentitys) { //Check that all entities are of the same type if(Entity. GetType ()! =EntityType) { Throw NewException ("the collection element type to be converted is inconsistent"); } Object[] Entityvalues =New Object[Entityproperties.length]; for(inti =0; i < entityproperties.length; i++) { Try{Entityvalues[i]= Entityproperties[i]. GetValue (Entity,NULL); Sb. Append ("\""+ HttpContext.Current.Server.HtmlDecode (Entityvalues[i]. ToString (). Replace ("\"","\"\""). Replace ("\ n", Environment.NewLine). Replace ("<BR>", Environment.NewLine)) +"\""); Sb. Append (","); } Catch{Entityvalues[i]=string. Empty; Sb. Append ("\""+ Entityvalues[i]. ToString () +"\""); Sb. Append (","); }} sb. Remove (sb.) Length-1,1); Sb. Append ("\ r \ n"); } HttpResponse resp; Resp=System.Web.HttpContext.Current.Response; Resp. Charset="GB2312"; Resp. ContentEncoding= System.Text.Encoding.GetEncoding ("GB2312"); Resp. Appendheader ("content-disposition","attachment;filename="+string. Format ("{0:YYYYMMDDHHMMSS}", DateTime.Now) +". csv"); Resp. Write (SB); Resp. End (); } }
C # Data export to Excel method summary see red part