2012-02-06 115 views
2

我使用docx.dll將html轉換爲word。但不能轉換html標籤。例如:HTML是一種p將html轉換爲文字

<p><em><strong>adfa à asdf asdf</strong></em></p>

所以,當轉換完成後,Word文件的內容是相同的

<p><em><strong>adfa à asdf asdf</strong></em></p>

我下面

[System.Web.Services.WebMethod] 
    public static void eprt_tml(int ptcn_id) 
    { 
     DataTable tml_tbl = DBclass.TruyVanTraVeTable1("select course_id, vname, vcontent from testimonial where contact_id='" + ptcn_id + "'"); 
     string course_name = DBclass.TruyVanTraVeGiaTri("select vname from course where id='"+tml_tbl.Rows[0]["course_id"]+"'"); 
     DataTable ptcn_tbl = DBclass.TruyVanTraVeTable1("select first_name, last_name, salutation_id, title from contact where id='"+ptcn_id+"'"); 
     string ptcn_name = ptcn_tbl.Rows[0]["last_name"].ToString() + " " + ptcn_tbl.Rows[0]["first_name"].ToString(); 
     DocX g_document; 

     try 
     { 

      // Store a global reference to the loaded document. 
      g_document = DocX.Load(@"D:\Project\CRM1\tml\tml_tpt.docx"); 
      /* 
      * The template 'InvoiceTemplate.docx' does exist, 
      * so lets use it to create an invoice for a factitious company 
      * called "The Happy Builder" and store a global reference it. 
      */ 
      g_document = crt_from_tpl(DocX.Load(@"D:\Project\CRM1\tml\tml_tpt.docx"), course_name, tml_tbl.Rows[0]["vname"].ToString(), tml_tbl.Rows[0]["vcontent"].ToString(), ptcn_name, ptcn_tbl.Rows[0]["title"].ToString()); 
      // Save all changes made to this template as Invoice_The_Happy_Builder.docx (We don't want to replace InvoiceTemplate.docx). 
      g_document.SaveAs(@"D:\Project\CRM1\tml\Invoice_The_Happy_Builder.docx"); 
     } 

      // The template 'InvoiceTemplate.docx' does not exist, so create it. 
     catch (FileNotFoundException) 
     { 

     } 

    } 

    //Create tml from template 
    [System.Web.Services.WebMethod] 
    private static DocX crt_from_tpl(DocX template, string course_name, string vname, string vcontent, string ptcn_name, string ptcn_title) 
    { 
     template.AddCustomProperty(new CustomProperty("static_title", "Ứng Dụng Thực Tiễn Thành Công")); 
     template.AddCustomProperty(new CustomProperty("tmlname", vname)); 
     template.AddCustomProperty(new CustomProperty("tmlcontent", vcontent)); 
     template.AddCustomProperty(new CustomProperty("ptcnname", ptcn_name)); 
     template.AddCustomProperty(new CustomProperty("ptcntitle", ptcn_title)); 
     template.AddCustomProperty(new CustomProperty("coursename", course_name)); 
     return template; 
    } 

碼我怎樣才能解決這個問題?

+0

感謝ParPar,但我的代碼仍然運行良好,我只想替換或刪除html標記。有任何工具可以做到這一點? – Hainlp 2012-02-06 16:04:03

回答

0

只是保存文檔前添加以下兩行代碼..

g_document.ReplaceText(@"<p><em><strong>",""); 
g_document.ReplaceText(@"</strong></em></p>",""); 

注:它只是將刪除HTML每日新聞,但不添加任何格式。

+0

我認爲OP想要一個比這更通用的解決方案。 – jadarnel27 2012-02-06 16:21:29

+0

@ jadarnel27我這麼認爲,但是您是否檢查過Hainlp對ParPar的評論回覆基本上將HTML轉換爲DOC本身不是一個好主意,因爲ASP.NET將從其自己的服務器端標記(而不是HTML標記)中生成HTML。 – Flowerking 2012-02-06 16:25:14

+0

我的應用程序:用戶通過CKeditor在我的網站發佈文章。然後用戶想要將這篇文章導出到.docx文件並使用它進行打印。所以不能使用ReplaceText來替換html標籤。 – Hainlp 2012-02-07 00:05:30

0

用Word打開你的HTML文件,點擊「另存爲」並選擇「文件類型」下的「Word文檔」。