.Doc 文件未在带有 html 标签的 asp.net 中正确创建

.Doc file is not creating properly in asp.net coming with html tags

喜欢ms word文档中的以下数据

<div id="ctl00_ContentPlaceHolder1_design" style="width:600px">
        <table id="ctl00_ContentPlaceHolder1_rpt" border="0" width="600"> 

如何将 html 标签转换为纯内容?

aspx.cs

 protected void btnMail_Click(object sender, EventArgs e)
 {
     Response.Clear();
     try
     {
         System.IO.StringWriter stringWrite = new System.IO.StringWriter();
         System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
         design.RenderControl(htmlWrite);
         string strBuilder = stringWrite.ToString();
         string strPath = Request.PhysicalApplicationPath + "\Temp\WeeklyReport of " + Projname + ".doc";


         if (File.Exists(strPath))
         {
             var counter = 1;
             strPath = strPath.Replace(".doc", " (" + counter + ").doc");
             while (File.Exists(strPath))
             {
                 strPath = strPath.Replace("(" + counter + ").doc", "(" + (counter + 1) + ").doc");
                 counter++;
             }
         }
         var doc = DocX.Create(strPath,DocumentTypes.Document);
         doc.InsertParagraph(strBuilder);
         doc.Save();
     }
 }

如果div里面的所有文字都是你想要的,那么你可以这样做。

ASP.NET

<div runat="server" id="design" style="width:600px">
 SOME TEXT <span> text </span>
</div>

C#:

string allTextInsideDiv = design.InnerText; //You should get "SOME TEXT text"

已编辑: 在我们的进一步讨论中,您无法获得 InnerText,因为 div 中有一些 ASP.NET 服务器控件。所以解决方案是获取 HTML 代码并使用 XmlDocument 或 HtmlDocument 对象,将内容加载到其中。然后把InnerText提取出来。

示例代码:

System.IO.StringWriter stringWrite = new System.IO.StringWriter(); 
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); 
div_myDiv.RenderControl(htmlWrite); 
string myText = stringWrite.ToString().Replace("&", "&amp;");
XmlDocument xDoc = new XmlDocument(); 
xDoc.LoadXml(myText); 
string rawText = xDoc.InnerText;