簡體   English   中英

.doc文件未在帶有html標記的asp.net中正確創建

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

  • .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();
     }
 }

如果這是您想要的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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM