2017-07-25 79 views
0

我想在導出到Excel的過程中從動態生成的HTML中刪除超鏈接。 我試了很多谷歌搜索,但我仍然找不到解決方案。 我的代碼是無法從導出到Excel的動態生成的HTML中刪除超鏈接

string CompanyName = string.Empty; 
CompanyName = Session["CompanyName"].ToString(); 
CompanyName = CompanyName.Replace(" ", "_"); 
string FileName = CompanyName.Trim() + "-Weekly_CashFlow_Statement_" + 
    DateTime.Now.ToString("ddMMyyyyhhmmss") + ".xls"; 


HttpContext.Current.Response.AppendHeader(
    "Content-Disposition", 
    "attachment; filename=" + FileName); 
HttpContext.Current.Response.Charset = ""; 
HttpContext.Current.Response.ContentType = "application/ms-excel"; 

this.Page.EnableViewState = false; 
System.IO.StringWriter tw = new System.IO.StringWriter(); 
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); 
tblCashFlow.RenderControl(hw); 
HttpContext.Current.Response.Write(tw.ToString()); 
HttpContext.Current.Response.End(); 

回答

0

我只是改變了我的代碼

HttpContext.Current.Response.Write(tw.ToString()); 

to 

HttpContext.Current.Response.Write(Regex.Replace(tw.ToString(), "</?(a|A).*?>", "")); 

,它爲我工作得很好。 其解答我的問題