/// <summary> ///Export Features/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> protected voidBtn_export_click (Objectsender, EventArgs e) { Try { stringstrSQL =Createstrwhere (); DataTable DST= Shdonbll.getlistforexport (strSQL). tables[0]; if(DST. rows.count<=0) {Jscript.alert ( This,"query data is empty, no data export, please re-select the condition! "); return; } DataTable DS=updatedatatable (DST); for(inti =0; I < DS. Rows.Count; i++) {DataColumn DC= ds. columns[1];//I will be the first column or the column name .dc. DataType =typeof(String); stringEid = ds. rows[i]["Appliance Name"]. ToString (); stringTyid = ds. rows[i]["category"]. ToString (); if(!string. IsNullOrEmpty (Eid)) {ds. rows[i]["Appliance Name"] =Getequi (Eid); } if(!string. IsNullOrEmpty (Tyid)) {ds. rows[i]["category"] =Getzg (Tyid); } } //Datatableexcel (ds, "Test 0", ""); } Catch(Exception ex) {Throwex; } } /// <summary> ///to modify the type and record value of a column in a DataTable (correct steps: 1. Clone table structure, 2. Modify the column type, 3. Modify the record value, 4. Return the desired result)/// </summary> /// <param name= "argdatatable" >Data Table DataTable</param> /// <returns>Data Table DataTable</returns> PrivateDataTable UpdateDataTable (DataTable argdatatable) {DataTable Dtresult=NewDataTable (); //Clone table StructureDtresult =Argdatatable.clone (); foreach(DataColumn Colinchdtresult.columns) {if(Col. ColumnName = ="Appliance Name"|| Col. ColumnName = ="category") { //Modifying column typesCol. DataType =typeof(String); } } foreach(DataRow rowinchargdatatable.rows) {DataRow rownew=Dtresult.newrow (); rownew["Serial Number"] = row["Serial Number"]; rownew["Appliance Name"] = row["Appliance Name"]; rownew["Shutdown Time"] = row["Shutdown Time"]; rownew["Start Time"] = row["Start Time"]; rownew["Normal Time"] = row["Normal Time"]; rownew["Downtime Reasons"] = row["Downtime Reasons"]; rownew["category"] = row["category"]; rownew["Downtime Hours"] = row["Downtime Hours"]; rownew["Annual"] = row["Annual"]; DTRESULT.ROWS.ADD (rownew); } returnDtresult; } #regionDataTable Export to Excel/// <summary> ///DataTable Export to Excel/// </summary> /// <param name= "PData" >DataTable</param> /// <param name= "Pfilename" >Export File name</param> /// <param name= "Pheader" >Export Header to | split</param> Public Static voidDatatableexcel (System.Data.DataTable PData,stringPfilename,stringPheader) {System.Web.UI.WebControls.DataGrid Dgexport=NULL; //Current ConversationSystem.Web.HttpContext Curcontext =System.Web.HttpContext.Current; //IO for exporting and returning Excel filesSystem.IO.StringWriter Strwriter =NULL; System.Web.UI.HtmlTextWriter HTMLWriter=NULL; if(PData! =NULL) { stringuseragent = curcontext.request.servervariables["http_user_agent"]. ToLower (); if(Useragent.indexof ("Firefox") == -1)//Firefox BrowserPfilename =Httputility.urlencode (Pfilename, System.Text.Encoding.UTF8); CurContext.Response.AddHeader ("content-disposition","attachment; Filename="+ Pfilename +". xls"); CurContext.Response.ContentType="Application/vnd.ms-excel"; Strwriter=NewSystem.IO.StringWriter (); HTMLWriter=NewSystem.Web.UI.HtmlTextWriter (Strwriter); //To resolve a possible paging situation in dgdata, you need to redefine a DataGrid with no pagingDgexport =NewSystem.Web.UI.WebControls.DataGrid (); Dgexport.datasource=Pdata.defaultview; Dgexport.allowpaging=false; Dgexport.showheader=true;//Show titleDgexport.databind (); string[] Arrheader = Pheader.split ('|'); stringStrheader ="<table border=\ "1\" style=\ "background-color:gray;font-weight:bold;\" ><tr>"; foreach(stringJinchArrheader) {Strheader+="<td>"+ j.tostring () +"</td>"; } Strheader+="</tr></table>"; //Back to clientDgexport.rendercontrol (HTMLWriter); stringStrmeta ="<meta http-equiv=\ "content-type\" content=\ "Application/ms-excel; charset=utf-8\" />"; CurContext.Response.Write (Strmeta+ Strheader +strwriter.tostring ()); CurContext.Response.End (); } }
DataTable changes in the type method with data column +datatable export Excel feature