导出到excel期间无法从动态生成的HTML中删除超链接 [英] Unable to remove hyperlink from dynamic generated HTML during export to excel

查看:153
本文介绍了导出到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();





我尝试过:



我通过谷歌搜索尝试了很多东西,但直到现在都无法做到这一点



 protected void btnExportToExcel_Click(object sender,ImageClickEventArgs e)
{
try
{
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);
RemoveLink(tblCashFlow);
tblCashFlow.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
catch(Exception Ex)
{
throw Ex;
}
}





 public void RemoveLink(System.Web.UI.HtmlControls .HtmlTable html)
{
try
{
foreach(html.Rows中的System.Web.UI.HtmlControls.HtmlTableRow单元格)
{
/ /cell.CssClass =;
for(int i = cell.Controls.Count - 1; i> = 0; i--)
{
Control control = cell.Controls [i];
if(control.GetType()== typeof(System.Web.UI.HtmlControls.HtmlTableCell))
{
string str =((System.Web.UI.HtmlControls.HtmlTableCell) (对照))的标识。;
int startind = str.IndexOf('>');
if(startind> 0)
{
int endindex = str.LastIndexOf(< / a>);
int length = endindex< 0? 1:endindex - startind;
str = str.Substring(startind + 1,length - 1);
cell.Controls.Remove(control);
System.Web.UI.LiteralControl ctrl = new System.Web.UI.LiteralControl();
ctrl.Text = str;
cell.Controls.Add(ctrl);
}
else
{
cell.Controls.Remove(control);
System.Web.UI.LiteralControl ctrl = new System.Web.UI.LiteralControl();
ctrl.Text = str;
cell.Controls.Add(ctrl);
}
}
}
}
}
catch(exception ex)
{

}
}

解决方案



我刚从

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

to

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

它适用于我


I want to remove hyperlink from dynamic generated HTML during Export to excel. I tried lot by googling but still I could not find solution. my code is

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();



What I have tried:

I tried so many things via googling but unable to do this till now

protected void btnExportToExcel_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                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);
                RemoveLink(tblCashFlow);
                tblCashFlow.RenderControl(hw);
                HttpContext.Current.Response.Write(tw.ToString());
                HttpContext.Current.Response.End();
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }



public void RemoveLink(System.Web.UI.HtmlControls.HtmlTable html)
       {
           try
           {
               foreach (System.Web.UI.HtmlControls.HtmlTableRow cell in html.Rows)
               {
                   //cell.CssClass = "";
                   for (int i = cell.Controls.Count - 1; i >= 0; i--)
                   {
                       Control control = cell.Controls[i];
                       if (control.GetType() == typeof(System.Web.UI.HtmlControls.HtmlTableCell))
                       {
                           string str = ((System.Web.UI.HtmlControls.HtmlTableCell)(control)).ID;
                           int startind = str.IndexOf('>');
                           if (startind > 0)
                           {
                               int endindex = str.LastIndexOf("</a>");
                               int length = endindex < 0 ? 1 : endindex - startind;
                               str = str.Substring(startind + 1, length - 1);
                               cell.Controls.Remove(control);
                               System.Web.UI.LiteralControl ctrl = new System.Web.UI.LiteralControl();
                               ctrl.Text = str;
                               cell.Controls.Add(ctrl);
                           }
                           else
                           {
                               cell.Controls.Remove(control);
                               System.Web.UI.LiteralControl ctrl = new System.Web.UI.LiteralControl();
                               ctrl.Text = str;
                               cell.Controls.Add(ctrl);
                           }
                       }
                   }
               }
           }
           catch (Exception ex)
           {

           }
       }

解决方案

Hi,
I just changed my code from

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

to

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

and it works fine for me


这篇关于导出到excel期间无法从动态生成的HTML中删除超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆