c# - 将 html 转换为 word

标签 c# asp.net docx

我使用 docx.dll 将 html 转换为 word。但不能转换 html 标签。例如:html 是一个 p

<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;
    }

我该如何解决?

最佳答案

在保存文档之前将这两行添加到您的代码中..

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

注意:它只会删除 html 标签,但不会添加任何格式。

关于c# - 将 html 转换为 word,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9162366/

相关文章:

c# - 获取使用大端格式 C# 进行编码的指南

javascript - SlickGrid:判断行是否被选中

c# - 根据分数和校园将学生分组 - ASP.NET、VB 和 SQL

python - 如何通过 docx 库或 xml 将单元格文本包装在表格中?

c# - 如何将我的 python 模块及其依赖项(例如 Numpy/Scipy)打包到一个文件中,例如用于 C# 调用的 dll?

c# - 重复创建点

asp.net - Docker 无法使用 "error MSB1001"在 Azure Pipelines 中构建

asp.net - 保护 ASP .Net Web API 以便与移动应用程序一起使用

xml - 默认 WordML 单位度量?像素或点或英寸

php - 我可以仅使用 PHP 在 .doc 和 .docx 之间转换吗?