将 html 转换为 word

发布于 2025-01-03 02:52:57 字数 2731 浏览 0 评论 0原文

我使用 docx.dll 将 html 转换为 word。但无法转换html标签。例如:html 是 p ,其中

adfa à asdf asdf

因此,转换完成后,word 文件的内容是相同的

adfa à asdf asdf

我的代码如下

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

我该如何解决它?

I use docx.dll to convert html to word. But can't convert html tag. For example : html is a p with

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

So when convert finished, the content of word file is the same

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

My code below

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

How can I resolve it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小ぇ时光︴ 2025-01-10 02:52:57

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

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

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

Add these two lines to your code just before saving the document..

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

Note: It will just remove the html tages, but without adding any formatting.

北陌 2025-01-10 02:52:57

使用 Word 打开 HTML 文件,单击“另存为”,然后在“文件类型”下选择“Word 文档”。

Open your HTML file with Word, click on "Save as" and choose "Word document" under "File type".

墟烟 2025-01-10 02:52:57

在我的 Windows PC 上,我安装了 Microsoft Office 2016。您必须将 COM 引用添加到路径

C:\Windows\assemble\GAC_MSIL\office\15.0.0.0__71e9bce111e9429c\OFFICE.DLL

中的 .NET core 项目,您必须先保存您的 HTML 字符串作为 .HTML 文件,然后使用以下函数将该 HTML 文件加载到 Word 文档,然后将其另存为 *.docx 文件。
您可以使用下面的 C# 函数将 HTML 文本转换为 Word 文档

using System.Text;
using Microsoft.Office.Interop.Word;
using Application = Microsoft.Office.Interop.Word.Application;
using Document = Microsoft.Office.Interop.Word.Document;
using Paragraph = Microsoft.Office.Interop.Word.Paragraph;


    public void SaveHtmlAsWordFile(string htmlFilePath)
    {
        string wordFile = htmlFilePath.Replace(".html",".docx");
        // Create an instance of the Word application
        Application wordApp = new Application();

        // Open the HTML file
        Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(htmlFilePath);

        // Save the document as a .docx file
        doc.SaveAs2(wordFile, WdSaveFormat.wdFormatDocumentDefault);

        // Close the document and the Word application
        doc.Close();
        wordApp.Quit();
    }

在此处输入图像描述

在此处输入图像描述

On my Windows PC I have installed Microsoft Office 2016. You will have to add COM reference to your .NET core project at path

C:\Windows\assembly\GAC_MSIL\office\15.0.0.0__71e9bce111e9429c\OFFICE.DLL

You have to first save your HTML string as .HTML file and then use the below function to load that HTML file to word document and then save it as *.docx file.
You can use below C# function to convert HTML text to word doc

using System.Text;
using Microsoft.Office.Interop.Word;
using Application = Microsoft.Office.Interop.Word.Application;
using Document = Microsoft.Office.Interop.Word.Document;
using Paragraph = Microsoft.Office.Interop.Word.Paragraph;


    public void SaveHtmlAsWordFile(string htmlFilePath)
    {
        string wordFile = htmlFilePath.Replace(".html",".docx");
        // Create an instance of the Word application
        Application wordApp = new Application();

        // Open the HTML file
        Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(htmlFilePath);

        // Save the document as a .docx file
        doc.SaveAs2(wordFile, WdSaveFormat.wdFormatDocumentDefault);

        // Close the document and the Word application
        doc.Close();
        wordApp.Quit();
    }

enter image description here

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文