2017-07-25 68 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).*?>", "")); 

,它为我工作得很好。 其解答我的问题