1
  • .doc 未正确创建。使用完整的 html 标签而不是文字创建

像下面的 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();
     }
 }
4

1 回答 1

0

如果它是您想要的 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;
于 2015-05-21T06:18:22.540 回答